[root@monitor scripts]# vim /usr/local/scripts/tcp_connections.sh
#!/bin/bash
stat() {
netstat -an | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
}
case $1 in
TIME_WAIT)
stat |grep ‘TIME_WAIT’ |awk ‘{print $2}’
;;
CLOSE_WAIT)
stat | grep ‘CLOSE_WAIT’ |awk ‘{print $2}’
;;
FIN_WAIT1)
stat | grep ‘FIN_WAIT1’ |awk ‘{print $2}’
;;
ESTABLISHED)
stat | grep ‘ESTABLISHED’ |awk ‘{print $2}’
;;
SYN_RECV)
stat |grep ‘SYN_RECV’ |awk ‘{print $2}’
;;
LAST_ACK)
stat |grep ‘LAST_ACK’ |awk ‘{print $2}’
;;
LISTEN)
stat |grep ‘LISTEN’ |awk ‘{print $2}’
;;
*)
echo “Usage: TIME_WAIT CLOSE_WAIT FIN_WAIT1 ESTABLISHED SYN_RECV LAST_ACK LISTEN”
;;
esac
测试脚本是否可用
[root@monitor scripts]# chmod +x tcp_connections.sh
[root@monitor scripts]# ./tcp_connections.sh ESTABLISHED
125
编辑zabbix_agentd配置
[root@monitor scripts]# vim /usr/local/etc/zabbix_agentd.conf.d/monitor_tcp_connections.conf
UserParameter=tcp.time_wait,/usr/local/scripts/tcp_connections.sh TIME_WAIT
UserParameter=tcp.close_wait,/usr/local/scripts/tcp_connections.sh CLOSE_WAIT
UserParameter=tcp.fin_wait1,/usr/local/scripts/tcp_connections.sh FIN_WAIT1
UserParameter=tcp.established,/usr/local/scripts/tcp_connections.sh ESTABLISHED
UserParameter=tcp.syn_recv,/usr/local/scripts/tcp_connections.sh SYN_RECV
UserParameter=tcp.last_ack,/usr/local/scripts/tcp_connections.sh LAST_ACK
UserParameter=tcp.listen,/usr/local/scripts/tcp_connections.sh LISTEN
重启服务
[root@monitor scripts]# /etc/init.d/zabbix_agentd restart
Shutting down zabbix_agentd: [确定]
Starting zabbix_agentd: [确定]
测试监控是否有数据
[root@monitor scripts]# zabbix_get -s localhost -k tcp.established
126
然后在web里创建模版,方便以后多台添加
填写模版名称
创建监控项
创建图形

等一会儿数据图形就会出现