forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp-connection-state-counter
executable file
·77 lines (62 loc) · 1.97 KB
/
tcp-connection-state-counter
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
#!/bin/bash
# @Function
# show count of tcp connection stat.
#
# @Usage
# $ ./tcp-connection-state-counter
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-2.x/docs/shell.md#-tcp-connection-state-counter
# @author Jerry Lee (oldratlee at gmail dot com)
# @author @sunuslee (sunuslee at gmail dot com)
set -eEuo pipefail
# NOTE: DO NOT declare var PROG as readonly, because its value is supplied by subshell.
PROG="$(basename "$0")"
readonly PROG_VERSION='2.5.0-dev'
################################################################################
# util functions
################################################################################
usage() {
cat <<EOF
Usage: ${PROG} [OPTION]...
show count of tcp connection stat.
Example:
${PROG}
Options:
-h, --help display this help and exit
-V, --version display version information and exit
EOF
exit
}
progVersion() {
echo "$PROG $PROG_VERSION"
exit
}
################################################################################
# parse options
################################################################################
for a; do
[[ "-h" == "$a" || "--help" == "$a" ]] && usage
done
for a; do
[[ "-V" == "$a" || "--version" == "$a" ]] && progVersion
done
################################################################################
# biz logic
################################################################################
# On MacOS, netstat need to using -p tcp to get only tcp output.
uname | grep Darwin -q && option_for_mac="-ptcp"
netstat -tna ${option_for_mac:-} | awk 'NR > 2 {
++s[$NF]
}
END {
# get max length of stat and count
for(v in s) {
stat_len = length(v)
if(stat_len > max_stat_len) max_stat_len = stat_len
count_len = length(s[v])
if (count_len > max_count_len) max_count_len = count_len
}
for(v in s) {
printf "%-" max_stat_len "s %" max_count_len "s\n", v, s[v]
}
}' | sort -nr -k2,2