#! /bin/csh 
# 
# by jeffc 
# 
# Usage :
# tcpstat [-t | -i interval] -w
#
# Options:
#	-t	given current counter totals
#	-i interval	sample at interval seconds and give counter rate
#   -w watch_mode
# Default is -i 5
#
#

set usage = "usage: watch_tcp [-t | -i interval] -w"
set get_current_totals = 0
set get_current_rates  = 1
set interval = 5
set watch_mode = 0
set drop_limit = 3

while ($#argv)
    if ("$argv[1]" == "-t") then
        set get_current_totals = 1
        set get_current_rates = 0
    else if ("$argv[1]" == "-i") then
        set get_current_rates = 1
        set get_current_totals = 0
        shift
        set interval = $argv[1]
    else if ("$argv[1]" == "-w") then
        set watch_mode = 1
    endif

    shift
end

set now = `date '+%y:%m:%d:%H:%M:%S'`
set TMPFILE = /tmp/tcpstat.tmp.${now}.$$
set DFILE = /tmp/tcpstat.tmp.delta.${now}.$$
set DROPFILE = /tmp/DROPS

if($get_current_totals) then
	netstat -i -s -P tcp | sed -g s/=/\ / > $TMPFILE

	echo ""
	echo "handshake queue max     " `/usr/sbin/ndd -get /dev/tcp tcp_conn_req_max_q0`
	echo "ESTABLISHED queue max   " `/usr/sbin/ndd -get /dev/tcp tcp_conn_req_max_q`
	echo ""
	awk '{if($1 == "tcpOutDataSegs") print $1"	"$2}' $TMPFILE
	awk '{if($3 == "tcpOutDataBytes") print $3"	"$4}' $TMPFILE
	awk '{if($3 == "tcpPassiveOpens") print $3"	"$4}' $TMPFILE
	awk '{if($1 == "tcpListenDrop") print $1"	"$2}' $TMPFILE
	awk '{if($3 == "tcpListenDropQ0") print $3"	"$4}' $TMPFILE
	awk '{if($1 == "tcpHalfOpenDrop") print $1"	"$2}' $TMPFILE

	\rm $TMPFILE

else 

	netstat -i -s -P tcp | sed -g s/=/\ / > $TMPFILE
	sleep $interval
	netstat -i -s -P tcp | sed -g s/=/\ / > $DFILE

	set ODS = `awk '{if($1 == "tcpOutDataSegs") print $2}' $TMPFILE`
	set ODS5 = `awk '{if($1 == "tcpOutDataSegs") print $2}' $DFILE`
	set ODB = `awk '{if($3 == "tcpOutDataBytes") print $4}' $TMPFILE`
	set ODB5 = `awk '{if($3 == "tcpOutDataBytes") print $4}' $DFILE`
	set PO  = `awk '{if($3 == "tcpPassiveOpens") print $4}' $TMPFILE`
	set PO5  = `awk '{if($3 == "tcpPassiveOpens") print $4}' $DFILE`
	set LD = `awk '{if($1 == "tcpListenDrop") print $2}' $TMPFILE`
	set LD5 = `awk '{if($1 == "tcpListenDrop") print $2}' $DFILE`
	set LDQ0 = `awk '{if($3 == "tcpListenDropQ0") print $4}' $TMPFILE`
	set LDQ05 = `awk '{if($3 == "tcpListenDropQ0") print $4}' $DFILE`
	set HOD = `awk '{if($1 == "tcpHalfOpenDrop") print $2}' $TMPFILE`
	set HOD5 = `awk '{if($1 == "tcpHalfOpenDrop") print $2}' $DFILE`

    @ ODS_rate = ($ODS5 - $ODS)  / $interval
    @ ODB_rate = ($ODB5 - $ODB)    / $interval
    @ PO_rate  = ($PO5 - $PO)    / $interval
    @ LD_rate  = ($LD5 - $LD)    / $interval 
    @ LDQ0_rate = ($LDQ05 - $LDQ0)  / $interval
    @ HOD_rate  = ($HOD5 - $HOD)    / $interval

    if($watch_mode) then
        echo INFO $now TCP Out B/s : $ODB_rate  Listen drops/s : $LD_rate
        if($LD_rate) then
            if(-f $DROPFILE) then
                set drop_count = `cat $DROPFILE`
                @ drop_count++
                if($drop_count > $drop_limit) then
                    echo CRITICAL $now TCP dropping connections for $drop_count checks
                endif
                echo $drop_count > $DROPFILE
            else
                echo 1 > $DROPFILE
            endif
        else
            if(-f $DROPFILE) then
                rm $DROPFILE
        endif
    else
        echo ""
        echo "handshake queue max     " `/usr/sbin/ndd -get /dev/tcp tcp_conn_req_max_q0`
        echo "ESTABLISHED queue max   " `/usr/sbin/ndd -get /dev/tcp tcp_conn_req_max_q`
        echo ""
        echo tcpOutDataSegs"	"$ODS_rate  per second 
        echo tcpOutDataBytes" "$ODB_rate  per second 
        echo tcpPassiveOpens"	"$PO_rate  per second
        echo tcpListenDrop"   "$LD_rate  per second
        echo tcpListenDropQ0"	"$LDQ0_rate  per second
        echo tcpHalfOpenDrop"	"$HOD_rate  per second
    endif

	\rm $TMPFILE
	\rm $DFILE
endif