zabbix (11) 监控TCP连接数

  对TCP的监控可以采用ss、netstat、/proc/net/tcp这三个不同的方案来实现。其中ss是最快的

(1)ss命令

[root@manager1 script_py 10:38:44]#time ss state all|awk '{++S[$2]} END{for( i in S) {printf "%-11s %s\n",i,S[i]} }'
LISTEN      47
ESTAB       287
State       1
TIME-WAIT   5
UNCONN      80

real    0m0.036s
user    0m0.024s
sys    0m0.011s

(2)netstat命令

[root@manager1 script_py 10:42:04]#time netstat -an |awk '/^tcp/{++S[$NF]}END{for(i in S){printf "%-11s %s\n",i,S[i]}}'
LISTEN      18
ESTABLISHED 2
TIME_WAIT   6

real    0m0.007s
user    0m0.005s
sys    0m0.002s

(3)读取/proc/net/tcp 文件,其中第四列为TCP连接的各个状态,如:

[root@manager1 script_py 10:44:38]#cat /proc/net/tcp|awk '{++S[$4]} END{for( i in S) {printf "%-11s %s\n",i,S[i]} }'
01          2
0A          12
st          1
06          6

  第四列各个状态的意思如下:

00  "ERROR_STATYS",

01  "TCP_ESTABLISHED"

02  "TCP_SYN_SENT"

03  "TCP_SYN_RECV"

04  "TCP_FIN_WAIT1"

05  "TCP_FIN_WAIT2"

06  "TCP_TIME_WAIT"

07  "TCP_CLOSE"

08  "TCP_CLOSE_WAIT"

0A  "TCP_LISTEN"

0B  "TCP_CLOSING"

添加自定义键:

[root@manager1 script_py 10:51:24]#cat /etc/zabbix/zabbix_agentd.d/tcp_connection.conf

UserParameter=tcp_connection.errorstatus,awk '($4=="00"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.established,awk '($4=="01"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.synsent,awk '($4=="02"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.synrecv,awk '($4=="03"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.finwait1,awk '($4=="04"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.finwait2,awk '($4=="05"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.timewait,awk '($4=="06"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.close,awk '($4=="07"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.close_wait,awk '($4=="08"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.last_ack,awk '($4=="09"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.listen,awk '($4=="0A"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'
UserParameter=tcp_connection.closing,awk '($4=="0B"){print $4}' /proc/net/tcp|awk '{++S[$1]}END {for(i in S){print S[i]}}'

 

 

posted @ 2019-05-20 10:52  北漂-boy  阅读(725)  评论(0编辑  收藏  举报