Ian Wienand | 2bbc9bb | 2019-02-11 12:25:38 +1100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # lib/tcpdump |
| 4 | # Functions to start and stop a tcpdump |
| 5 | |
| 6 | # Dependencies: |
| 7 | # |
| 8 | # - ``functions`` file |
| 9 | |
| 10 | # ``stack.sh`` calls the entry points in this order: |
| 11 | # |
| 12 | # - start_tcpdump |
| 13 | # - stop_tcpdump |
| 14 | |
| 15 | # Save trace setting |
| 16 | _XTRACE_TCPDUMP=$(set +o | grep xtrace) |
| 17 | set +o xtrace |
| 18 | |
| 19 | TCPDUMP_OUTPUT=${TCPDUMP_OUTPUT:-$LOGDIR/tcpdump.pcap} |
| 20 | |
| 21 | # e.g. for iscsi |
| 22 | # "-i any tcp port 3260" |
| 23 | TCPDUMP_ARGS=${TCPDUMP_ARGS:-""} |
| 24 | |
| 25 | # start_tcpdump() - Start running processes |
| 26 | function start_tcpdump { |
| 27 | # Run a tcpdump with given arguments and save the packet capture |
| 28 | if is_service_enabled tcpdump; then |
| 29 | if [[ -z "${TCPDUMP_ARGS}" ]]; then |
| 30 | die $LINENO "The tcpdump service requires TCPDUMP_ARGS to be set" |
| 31 | fi |
| 32 | touch ${TCPDUMP_OUTPUT} |
| 33 | run_process tcpdump "/usr/sbin/tcpdump -w $TCPDUMP_OUTPUT $TCPDUMP_ARGS" root root |
| 34 | fi |
| 35 | } |
| 36 | |
| 37 | # stop_tcpdump() stop tcpdump process |
| 38 | function stop_tcpdump { |
| 39 | stop_process tcpdump |
| 40 | } |
| 41 | |
| 42 | # Restore xtrace |
| 43 | $_XTRACE_TCPDUMP |