|
原理:
netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}'
TIME_WAIT 79
ESTABLISHED 6
LISTEN 3
mkdir -p /usr/local/zabbix/share/zabbix/alertscripts
cd !$
vim check_tcp.sh
#!/bin/bash
-------------------------------------------------------------------------------
FileName: check_tcp.sh
Revision: 1.0
Date: 2017/04/25
-------------------------------------------------------------------------------
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
;;
synrecv)
output=$(awk '/SYN_RECV/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
synsent)
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
;;
timewait)
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
;;
closewait)
output=$(awk '/CLOSE_WAIT/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
lastack)
output=$(awk '/LAST_ACK/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
finwait1)
output=$(awk '/FIN_WAIT1/{print $2}' $tmp_file)
if [ "$output" == "" ];then
echo 0
else
echo $output
fi
;;
finwait2)
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|closewait|synrecv|synsent|finwait1|finwait2|listen|established|lastack|timewait]\e[0m"
esac
赋予脚本执行权限 chmod a+x check_tcp.sh
定义key vim /usr/local/zabbix/etc/zabbix_agentd.conf 最后添加(如果agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定义了其他key的包含目录 Include=/etc/zabbix/zabbix_agentd.d/, 可以在 /etc/zabbix/zabbix_agentd.d/ 目录新建一个文件 tcp-status-params.conf, 再写入如下内容,注意修改位置)
#获取tcp链接状态,注意脚本位置
UserParameter=tcp.status
,/usr/local/zabbix/share/zabbix/alertscripts/check_tcp.sh $1
直接传参数的话命令行使用
echo "UserParameter=tcp.status
,/usr/local/zabbix/share/zabbix/alertscripts/check_tcp.sh \$1" >> /usr/local/zabbix/etc/zabbix_agentd.conf
重启zabbix客户端
cd /usr/local/zabbix/sbin/
service zabbix_agentd restart
systemctl restart zabbix_agentd.service centos 7命令
此时可以在服务器验证取值是否正常 -s 指定客户端IP,获值正确就添加模板
/usr/local/zabbix/bin/zabbix_get -s 10.10.61.3 -p10050 -k "tcp.status[established]"
登录zabbix web页面配置
登录Zabbix 的web界面,选择 Configuration > Templates , 在主界面的右上角有个 Import 按钮,用来导入模板模板如下(可以在Windows下新建一个文本文档,写入如下内容,修改后缀,模板导入提示txt错误时点开计算机-组织-文件夹和搜索选项-查看-去掉隐藏已知文件类型的扩展名以后再重命名模板名
)
2.0
2014-12-04T09:41:57Z
Templates
Template TCP Connection Status
Template TCP Connection Status
Templates
TCP Status
CLOSED
0
0
tcp.status[closed]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
CLOSE_WAIT
0
0
tcp.status[closewait]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
CLOSING
0
0
tcp.status[closing]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
ESTABLISHED
0
0
tcp.status[established]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
FIN_WAIT1
0
0
tcp.status[finwait1]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
FIN_WAIT2
0
0
tcp.status[finwait2]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
LAST_ACK
0
0
tcp.status[lastack]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
LISTEN
0
0
tcp.status[listen]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
SYN_RECV
0
0
tcp.status[synrecv]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
SYN_SENT
0
0
tcp.status[synsent]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
TIME_WAIT
0
0
tcp.status[timewait]
60
90
365
0
3
0
0
0
0
1
0
0
0
TCP Status
{Template TCP Connection Status:tcp.status[timewait].last()}>10000
There are too many TCP TIME_WAIT status
0
4
0
TCP Status
900
200
0.0000
100.0000
1
1
0
1
0
0.0000
0.0000
0
0
0
0
0
0
C80000
0
2
0
Template TCP Connection Status
tcp.status[closed]
1
0
00C800
0
2
0
Template TCP Connection Status
tcp.status[closewait]
2
0
0000C8
0
2
0
Template TCP Connection Status
tcp.status[closing]
3
0
C800C8
0
2
0
Template TCP Connection Status
tcp.status[established]
4
0
00C8C8
0
2
0
Template TCP Connection Status
tcp.status[finwait1]
5
0
C8C800
0
2
0
Template TCP Connection Status
tcp.status[finwait2]
6
0
C8C8C8
0
2
0
Template TCP Connection Status
tcp.status[lastack]
7
0
960000
0
2
0
Template TCP Connection Status
tcp.status[listen]
8
0
009600
0
2
0
Template TCP Connection Status
tcp.status[synrecv]
9
0
000096
0
2
0
Template TCP Connection Status
tcp.status[synsent]
10
0
960096
0
2
0
Template TCP Connection Status
tcp.status[timewait]
再把模板添加到需要监控的主机(粉红色是已经添加后显示的模板名)
添加完成查看图形,等待出图
部分图片未上传,请查看笔记
http://note.youdao.com/noteshare?id=4692b94dd32bf879b40a78faffbf902a&sub=95F2E92DCC354278832781E6D1F3FB48
|
|
|