How to get netstat stats on interval (Linux)

Document ID : KB000047922
Last Modified Date : 14/02/2018
Show Technical Document Details

Introduction

At times when troubleshooting probes, especially hub probe, you might be asked to get netstat looking for certain states e.g FIN_WAIT_2. The following script runs through cron every minute to collect netstat output.




Procedure

1- Create a script with following contents:

#!/bin/bash
DATE=`date`
CMD=$(netstat -tnl)
ARG=" -tn | grep FIN"
LOG=/nimsoft/netstats.log
?
echo ?"--------------------------------------" >> $LOG
echo $DATE >> $LOG
/bin/netstat -an |grep FIN >> $LOG
echo ?"--------------------------------------" >> $LOG
echo >> $LOG
?
2- Make script executable - assuming script name is get_netstats.sh
# chmod +x?get_netstats.sh

3- Install in cron
# crontab -e

4- Enter in edit mode and enter following
*/1 ? ? * ? ? ? * ? ? ? * ? ? ? * ? ? ? /root/get_netstats.sh

5- Save and exit editor