#!/bin/bash
#===============================================#
# Description : Check the TCP-connection #
#===============================================#
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKOWN=3
CONN_TMP="/tmp/netstat.tmp"
CONN=`netstat -ant | awk '/^tcp/ && !/LISTEN/{a[$NF]++}END{for(i in a) print i,a[i]}'`
echo $CONN > $CONN_TMP
EST=`cat $CONN_TMP | awk '/ESTABLISHED/{ print $2 }'`
TIME_WAIT=`cat $CONN_TMP | awk '/TIME_WAIT/{ print $2 }'`
SYN_RECV=`cat $CONN_TMP | awk '/SYN_RECV/{ print $2 }'`
FIN_WAIT1=`cat $CONN_TMP | awk '/FIN_WAIT1/{ print $2 }'`
FIN_WAIT2=`cat $CONN_TMP | awk '/FIN_WAIT2/{ print $2 }'`
LAST_ACK=`cat $CONN_TMP | awk '/LAST_ACK/{ print $2 }'`
CLOSE_WAIT=`cat $CONN_TMP | awk '/CLOSE_WAIT/{ print $2 }'`
if ! [ `uname` = "Linux" ] ;then
echo "This plugin is support Linux system,not support another system now"
echo "This plugin will check TCP connections "
fi
if [ "$EST" -gt 5000 ];then
echo "ESTABLISHED is too many; ESTABLISHED: $EST ; TIME_WAIT: $TIME_WAIT ; SYN_RECV: $SYN_RECV ; FIN_WAIT1: $FIN_WAIT1 ; FIN_WAIT2: $FIN_WAIT2 ; LAST_ACK: $LAST_ACK ; CLOSE_WAIT: $CLOSE_WAIT"
exit $STATE_CRITICAL
else
echo "ESTABLISHED is normal; ESTABLISHED: $EST ; TIME_WAIT: $TIME_WAIT ; SYN_RECV: $SYN_RECV ; FIN_WAIT1: $FIN_WAIT1 ; FIN_WAIT2: $FIN_WAIT2 ; LAST_ACK: $LAST_ACK ; CLOSE_WAIT: $CLOSE_WAIT"
exit $STATE_OK
fi
for a in $EST $TIME_WAIT $SYN_RECV $FIN_WAIT1 $FIN_WAIT2 $LAST_ACK $CLOSE_WAIT
do
if [[ $a =~ "^[0-9]{1,}$" || $a = "" ]]
echo "UNKOWN `uname` no yet support by this plugin"
exit $STATE_UNKOWN
fi
done