创建收集数据的脚本
#!/bin/bash
# TCP连接数及状态
# PORT 是指返回端口连接情况
if [ $# -ne 1 ];then
echo -e "\033[32mUsage: sh $0 {ESTABLISHED|LISTEN|TIME_WAIT|CLOSED|CLOSE_WAIT|CLOSING|FIN_WAIT1|FIN_WAIT2|LAST_ACK|SYN_RECV|SYN_SENT|PORT}\033[0m"
exit 1
fi
case $1 in
#socket已经建立连接
ESTABLISHED)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "ESTABLISHED")
echo $result
;;
#监听状态
LISTEN)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "LISTEN")
echo $result
;;
#表示收到了对方的FIN报文,并发送出了ACK报文,等待2MSL后就可回到CLOSED状态
TIME_WAIT)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "TIME_WAIT")
echo $result
;;
#socket没有被使用,无连接
CLOSED)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "CLOSED")
echo $result
;;
#等待关闭连接
CLOSE_WAIT)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "CLOSE_WAIT")
echo $result
;;
#服务器端和客户端都同时关闭连接
CLOSING)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "CLOSING")
echo $result
;;
#套接字已关闭,连接正在关闭
FIN_WAIT1)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "FIN_WAIT1")
echo $result
;;
#连接已关闭,套接字正在等待从远程端关闭
FIN_WAIT2)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "FIN_WAIT2")
echo $result
;;
#远端关闭,当前socket被动关闭后发送FIN报文,等待对方ACK报文
LAST_ACK)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "LAST_ACK")
echo $result
;;
#接收到SYN报文
SYN_RECV)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "SYN_RECV")
echo $result
;;
#已经发送SYN报文
SYN_SENT)
result=$(netstat -an | awk '/^tcp/ {print $0}'|grep -wc "SYN_SENT")
echo $result
;;
#端口连接数前5的值,这是为了让告警发邮件时拿到的数据
PORT)
result=$(netstat -ant | grep ESTABLISHED | grep -v "^$" | awk '{print $4}' | cut -d: -f2 | sort | uniq -c | awk '{print $1" "$2}' | sort -k 1 -nr | head -5 | awk '{print "[port: "$2"\tnum: "$1"]" }')
echo $result
;;
*)
echo -e "\033[32mUsage: sh $0 {ESTABLISHED|LISTEN|TIME_WAIT|CLOSED|CLOSE_WAIT|CLOSING|FIN_WAIT1|FIN_WAIT2|LAST_ACK|SYN_RECV|SYN_SENT}\033[0m"
esac
agentd增加自定义监控项
UserParameter=tcp.state[*],/usr/local/zabbix/etc/zabbix_agentd.conf.d/tcp_state.sh $1
测试下脚本是否正常
[root@zabbix-server ~]# zabbix_get -s 192.168.10.101 -k tcp.state[ESTABLISHED]
450
[root@zabbix-server ~]# zabbix_get -s 192.168.10.101 -k tcp.state[PORT]
[port: 2379 num: 74] [port: 8080 num: 61] [port: 6443 num: 13] [port: 2380 num: 6] [port: 39414 num: 2]
zabbix后台创建模板,配置监控项,懒得再走一遍了,直接上内容
创建个图形,套用模板收集数据
看收集后的数据