File: watch_tcp

package info (click to toggle)
boinc 7.14.2+dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,132 kB
  • sloc: cpp: 163,589; php: 113,173; ansic: 49,284; pascal: 35,620; xml: 17,864; java: 13,521; python: 6,551; sh: 4,082; perl: 1,843; makefile: 1,796; objc: 1,543; sql: 959; csh: 126; lisp: 47
file content (119 lines) | stat: -rwxr-xr-x 3,920 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/tcsh
# 
# 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