Linux netstat Recipe

  1. Create diag_netstat.sh:
    #!/bin/sh
    OUTPUTFILE="diag_netstat_$(hostname)_$(date +"%Y%m%d_%H%M%S").log"
    INTERVAL="${1:-30}" # First argument or a default of 30 seconds
    while true; do
      echo "diag: $(date +"%Y%m%d %H%M%S %N %Z") iteration" >> "${OUTPUTFILE}" 2>&1
      netstat -antop >> "${OUTPUTFILE}" 2>&1
      sleep ${INTERVAL}
    done
  2. Make it executable:
    chmod +x diag_netstat.sh
  3. Start it (default interval of 30 seconds or specify an alternate interval as the first argument):
    nohup ./diag_netstat.sh &
  4. Reproduce the problem
    1. If at any point you need to reduce this disk usage of the output file during the test, truncate the file:
      cat /dev/null > diag_netstat*log
  5. Stop the script:
    pkill -f diag_netstat
  6. Upload diag_netstat_* and nohup.out