一、在被监控端上设置
主要命令:
输出内容:
TIME_WAIT 100
CLOSE_WAIT 5
FIN_WAIT1 6
ESTABLISHED 161
SYN_RECV 1
LAST_ACK 1
LISTEN 11
下面以监控TIME_WAIT的数量为例子(先来准备下脚本)
在zabbix_server被监控的节点上编辑zabbix的配置文件
#在256行添加下列内容
UnsafeUserParameters=1
#在265行添加以下内容
UserParameter=TIME_WAIT,/bin/bash /usr/local/shell/zabbix/tcp_connections.sh TIME_WAIT
重启客户端
二、zabbix_server 端设置
首先看是否能否取到值
如果能获取到数值,那么恭喜你成功了。
访问zabbix web界面,创建模板
创
建图形
将模板应用到对应的机器上,绘图结果如下:
到此,zabbix自定义监控某主机的tcp连接数搞定。
#!/bin/bash
sed -i "s#\# UnsafeUserParameters=0#UnsafeUserParameters=1#g" /usr/local/etc/zabbix_agentd.conf
echo "Include=/usr/local/etc/zabbix_agentd.conf.d/" >>/usr/local/etc/zabbix_agentd.conf
mkdir -p /usr/local/shell/zabbix
cat << EOF >/usr/local/shell/zabbix/tcp_connections.sh
#!/bin/bash
netstat -an |awk '/^tcp/ {++S[\$NF]} END {for(a in S) print a, S[a]}'|grep \$1|cut -d' ' -f2
EOF
cat << EOF >/usr/local/shell/zabbix/zabbix_tcp.sh
#!/bin/bash
netstat -s|grep "connections established"|awk '{print\$1}'
EOF
chmod 755 /usr/local/shell/zabbix/zabbix_tcp.sh
mkdir -p /usr/local/etc/zabbix_agentd.conf.d
cat << EOF >/usr/local/etc/zabbix_agentd.conf.d/tcp
#!monitor tcp
UserParameter=TIME_WAIT,/bin/bash /usr/local/shell/zabbix/tcp_connections.sh TIME_WAIT
UserParameter=CLOSE_WAIT,/bin/bash /usr/local/shell/zabbix/tcp_connections.sh CLOSE_WAIT
UserParameter=ESTABLISHED,/bin/bash /usr/local/shell/zabbix/tcp_connections.sh ESTABLISHED
UserParameter=FIN_WAIT1,/bin/bash /usr/local/shell/zabbix/tcp_connections.sh FIN_WAIT1
UserParameter=LAST_ACK,/bin/bash /usr/local/shell/zabbix/tcp_connections.sh LAST_ACK
UserParameter=LISTEN,/bin/bash /usr/local/shell/zabbix/tcp_connections.sh LISTEN
UserParameter=SYN_RECV,/bin/bash /usr/local/shell/zabbix/tcp_connections.sh SYN_RECV
UserParameter=tcpconnect,/usr/local/shell/zabbix/zabbix_tcp.sh
EOF
service zabbix_agentd restart
几个监控项的键值就是TIME_WAIT、CLOSE_WAIT、ESTABLISHED、FIN_WAIT1、LAST_ACK、LISTEN、SYN_RECV、tcpconnect