赞
踩
[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
备注: 创建自定义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_agent 自定义的key 和 tcp_status.sh 监控脚本 来填写键值
类型: zabbix客户端
更新间隔: 60 (单位为秒)
应用集: TCP Status
创建触发器,如图所示:
三分钟内的平均ESTABLISHED 连接数大于1000 就触发告警
严重性: 一般严重
表达式: {Template TCP Connection Status:tcp.status[ESTABLISHED].avg(3m)}>1000
同样也可以 直接导入 模板,模板私聊小编获取
[root@aly root ]# /etc/init.d/zabbix_agent restart
给主机关联 链接的模板
私聊小编,回复 关键字 "tcp" 即可获得本文的监控项脚本,键值定义配置文件,zabbix tcp 监控模板文件哦
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。