-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport-record
executable file
·89 lines (74 loc) · 2.69 KB
/
port-record
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
# set -x
port=$1
proc=`pgrep -f $2`
if [ -z "$port" -o -z "$proc" ]; then
echo "usage: <port> <process>"
exit 1
fi
now=$(date +'%s')
report_file="record-port-${port}-${now}.log"
# 4096
page_size=$(getconf PAGE_SIZE)
echo "info: /proc/sys/net/ipv4/tcp_mem"
echo "--------------------------------"
cat /proc/sys/net/ipv4/tcp_mem
gawk -v PAGE_SIZE=$page_size '{ printf("%.2f MB %.2f MB %.2f MB\n",
($1 * PAGE_SIZE) / (1024*1024),
($2 * PAGE_SIZE) / (1024*1024),
($3 * PAGE_SIZE) / (1024*1024)) }' /proc/sys/net/ipv4/tcp_mem
echo "info: /proc/net/sockstat"
echo "------------------------"
cat /proc/net/sockstat
gawk -v PAGE_SIZE=$page_size '/^TCP:/ { printf("%.2f MB\n",
($11 * PAGE_SIZE) / (1024 * 1024)) }' /proc/net/sockstat
echo "info: /proc/sys/net/ipv4/tcp_max_orphans"
cat /proc/sys/net/ipv4/tcp_max_orphans
max_orphans=$(cat /proc/sys/net/ipv4/tcp_max_orphans)
echo "info: /proc/net/snmp"
cat /proc/net/snmp
echo "------------------------"
echo "info: looping for port:$port process:$proc $2"
while [ 1 ]
do
MEM=`ps -o rss= -p $proc | tr -d ' '`
time=$(date +'%s')
date=$(date --rfc-3339=seconds)
dropped=$(tc -s -d qdisc ls dev eth0 | gawk '/dropped/ { print $7 }'| tr -d ,)
backlog=$(tc -s -d qdisc ls dev eth0 | awk '/backlog/ { print $2,$3 }')
listener=$(ss -m -l sport = :$port | awk '/LISTEN/ { printf("%s %s ",$2,$3) } /mem/ { printf("%s\n", $1) }')
filenr=$(gawk '{ printf("%s:%s:%s\n", $1, $2, $3) }' /proc/sys/fs/file-nr)
# inuse orphan tw alloc mem
sockstat=$(gawk -v PAGE_SIZE=$page_size '/^TCP:/ { printf("%s:%s:%s:%s:%s %dMB",
$3, $5, $7, $9, $11, ($11 * PAGE_SIZE) / (1024*1024)) }' /proc/net/sockstat)
ss -4 -n -a "dport = :$port or sport = :$port" | gawk \
-v MEM=$MEM \
-v PORT=$port \
-v PID=$proc \
-v TIME=$time \
-v DATE="$date" \
-v BACKLOG="$backlog" \
-v DROPPED=$dropped \
-v LISTENER="$listener" \
-v FILENR="$filenr" \
-v SOCKSTAT="$sockstat" \
'
BEGIN {
state["LISTEN"] = 0
state["SYN-SENT"] = 0
state["SYN-RECV"] = 0
state["FIN-WAIT-1"] = 0
state["FIN-WAIT-2"] = 0
state["ESTAB"] = 0
state["CLOSE-WAIT"] = 0
state["TIME-WAIT"] = 0
}
$1 != "State" { state[$1] ++ }
END {
printf("%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d", TIME, LISTENER, BACKLOG, DROPPED, SOCKSTAT, FILENR, PORT, MEM, PID);
for (s in state) {
printf("\t%s:%d", s, state[s]);
}
printf("\n");
} '
sleep 2
done | tee -a $report_file