Skip to content

Instantly share code, notes, and snippets.

@alice-xu
Created February 25, 2015 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alice-xu/04898e8fd17e7b4b75ef to your computer and use it in GitHub Desktop.
Save alice-xu/04898e8fd17e7b4b75ef to your computer and use it in GitHub Desktop.
TCP state monitoring script for Zabbix
#!/usr/bin/env bash
#
# tcp_stats.sh:
# TCP state monitoring script for Zabbix
#
POLLING_TIME=$( date '+%s' )
NETSTAT_TCP_RESULT=$( netstat -na|grep '^tcp' )
if [ $? -ne 0 ]; then
echo 'Error: Could not get netstat!'
fi
ESTABLISHED=$( echo "${NETSTAT_TCP_RESULT}" | grep 'ESTABLISHED' | wc -l )
SYN_SENT=$( echo "${NETSTAT_TCP_RESULT}" | grep 'SYN_SENT' | wc -l )
SYN_RECV=$( echo "${NETSTAT_TCP_RESULT}" | grep 'SYN_RECV' | wc -l )
FIN_WAIT1=$( echo "${NETSTAT_TCP_RESULT}" | grep 'FIN_WAIT1' | wc -l )
FIN_WAIT2=$( echo "${NETSTAT_TCP_RESULT}" | grep 'FIN_WAIT2' | wc -l )
TIME_WAIT=$( echo "${NETSTAT_TCP_RESULT}" | grep 'TIME_WAIT' | wc -l )
CLOSED=$( echo "${NETSTAT_TCP_RESULT}" | grep 'CLOSED' | wc -l )
CLOSE_WAIT=$( echo "${NETSTAT_TCP_RESULT}" | grep 'CLOSE_WAIT' | wc -l )
LAST_ACK=$( echo "${NETSTAT_TCP_RESULT}" | grep 'LAST_ACK' | wc -l )
LISTEN=$( echo "${NETSTAT_TCP_RESULT}" | grep 'LISTEN' | wc -l )
CLOSING=$( echo "${NETSTAT_TCP_RESULT}" | grep 'CLOSING' | wc -l )
UNKNOWN=$( echo "${NETSTAT_TCP_RESULT}" | grep 'UNKNOWN' | wc -l )
{
echo "\"${MY_HOSTNAME}\" script.tcp_stats.established ${POLLING_TIME} ${ESTABLISHED}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.syn_sent ${POLLING_TIME} ${SYN_SENT}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.syn_recv ${POLLING_TIME} ${SYN_RECV}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.fin_wait1 ${POLLING_TIME} ${FIN_WAIT1}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.fin_wait2 ${POLLING_TIME} ${FIN_WAIT2}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.time_wait ${POLLING_TIME} ${TIME_WAIT}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.closed ${POLLING_TIME} ${CLOSED}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.close_wait ${POLLING_TIME} ${CLOSE_WAIT}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.last_ack ${POLLING_TIME} ${LAST_ACK}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.listen ${POLLING_TIME} ${LISTEN}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.closing ${POLLING_TIME} ${CLOSING}"
echo "\"${MY_HOSTNAME}\" script.tcp_stats.unknown ${POLLING_TIME} ${UNKNOWN}"
} | zbx_bulk_sender > /dev/null 2>&1
echo 'Info: netstat OK.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment