一、编写 zabbix agent TCP监控脚本
[root@aly etc]# vim /usr/local/etc/tcp_status.sh
#!/bin/bash
#this script is used to get tcp and udp connetion status
#tcp status
metric=$1
tmp_file=/tmp/tcp_status.txt
/bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' > $tmp_file
case $metric in
CLOSED)
output=$(awk '/CLOSED/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
LISTEN)
output=$(awk '/LISTEN/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
SYN_RECV)
output=$(awk '/SYN_RECV/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
SYN_SENT)
output=$(awk '/SYN_SENT/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
ESTABLISHED)
output=$(awk '/ESTABLISHED/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
TIME_WAIT)
output=$(awk '/TIME_WAIT/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
CLOSING)
output=$(awk '/CLOSING/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
CLOSE_WAIT)
output=$(awk '/CLOSE_WAIT/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
LAST_ACK)
output=$(awk '/LAST_ACK/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
FIN_WAIT1)
output=$(awk '/FIN_WAIT1/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
FIN_WAIT2)
output=$(awk '/FIN_WAIT2/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
*)
echo -e "e[033mUsage: sh $0 [CLOSED|CLOSING|CLOSE_WAIT|SYN_RECV|SYN_SENT|FIN_WAIT1|FIN_WAIT2|LISTEN|ESTABLISHED|LAST_ACK|TIME_WAIT]e[0m"
esac
并赋予权限:
[root@aly etc]# chmod 777 /usr/local/bin/tcp_status.sh
[root@aly etc]# chmod 777 /tmp/tcp_status.txt
二、修改agent配置文件
备注: 创建自定义key ,需要修改被监控端agent 的配置文件
[root@aly etc]# vim /usr/local/zabbix_agentd.conf
把 UnsafeUserParameters=0 修改为 UnsafeUserParameters=1 (是否允许自定义KEY)
创建自定义KEY
[root@aly etc]# vim /usr/local/etc/zabbix_agentd.conf
#添加一行
UserParameter=tcp.status[*],/usr/local/etc/tcp_status.sh $1
三、登录到zabbix上 创建模板
![c98caccef362abf25b73fde047560d49.png](https://i-blog.csdnimg.cn/blog_migrate/d4aff4e95df220c9997d0fe8287760bb.jpeg)
![0dbd5e9c0bb9669a4abcd928c644e3e7.png](https://i-blog.csdnimg.cn/blog_migrate/cb27945ec6a9a0e09347783baddb5eaf.jpeg)
创建监控项,如图所示:
根据zabbix_agent 自定义的key 和 tcp_status.sh 监控脚本 来填写键值
类型: zabbix客户端
更新间隔: 60 (单位为秒)
应用集: TCP Status
![bba4a062e78e37baed2fbef4d078eaee.png](https://i-blog.csdnimg.cn/blog_migrate/a0cfff915885cf63fa9f7b9dfe4cc539.jpeg)
![e3fc55139987a28097c3c77bca0fbc30.png](https://i-blog.csdnimg.cn/blog_migrate/f867ffedde976bec3e77a5fd7e1f8a4d.jpeg)
创建触发器,如图所示:
三分钟内的平均ESTABLISHED 连接数大于1000 就触发告警
严重性: 一般严重
表达式: {Template TCP Connection Status:tcp.status[ESTABLISHED].avg(3m)}>1000
![ebce5e40e7b54a1a682d995d5f84338a.png](https://i-blog.csdnimg.cn/blog_migrate/0ea396243d9e6b18ec531843b8f294f2.jpeg)
同样也可以 直接导入 模板,模板私聊小编获取
![6b9f0b9f8354175dcfbc7449df81984c.png](https://i-blog.csdnimg.cn/blog_migrate/78d88734d7ba71d62fc74e3148d97472.jpeg)
![2b508898faa8f92c823b8b7da9112229.png](https://i-blog.csdnimg.cn/blog_migrate/5aed4127cb2a2dfd1c31a0ac4fd7c1c0.jpeg)
四、 客户机重启zabbix_agent
[root@aly root ]# /etc/init.d/zabbix_agent restart
给主机关联 链接的模板
![cbfa32d9bb9fb583fb3c86230ab93d85.png](https://i-blog.csdnimg.cn/blog_migrate/4a02d767b3cc1a537668fb10208e7aca.jpeg)
五、验证
![5b576b077c5559c752061954b9d69b7e.png](https://i-blog.csdnimg.cn/blog_migrate/8c54c80269bb0982cda82b7f4f89f308.jpeg)
![21de1102a57860966fe1246ca987cd69.gif](https://i-blog.csdnimg.cn/blog_migrate/3b09a7f3a54b2fb2b6269e0cc9bdd9e3.gif)
私聊小编,回复 关键字 "tcp" 即可获得本文的监控项脚本,键值定义配置文件,zabbix tcp 监控模板文件哦