自动批量检查agent开放的端口
PS:如果服务器上的应用都是固定的,不会随机产生的都可以使用自动发现端口来监控;
如果服务器会随机出现端口且每次启动程序都会改变,可以采用第二种方法,来监控指定的端口
约定
zabbix所有执行的脚本统一放置在 /etc/zabbix/scripts 目录下
#cd /etc/zabbix/
#mkdir scripts
#chown root:zabbix -R /etc/zabbix/scripts/
#chmod 755 /etc/zabbix/scripts/
zabbix-agent操作
1、脚本内容
scripts]# vim discovery.sh
#!/bin/bash
portarray=(`netstat -tnlp|egrep -i "$1"|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort|uniq`)
length=${#portarray[@]}
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
do
printf '\n\t\t{'
printf "\"{#TCP_PORT}\":\"${portarray[$i]}\"}"
if [ $i -lt $[$length-1] ];then
printf ','
fi
done
printf "\n\t]\n"
printf "}\n"
scripts]#chmod +s /usr/bin/netstat
2、添加key值
cat /etc/zabbix/zabbix_agentd.conf
添加如下内容:
UnsafeUserParameters=1
UserParameter=tcpportlisten,/etc/zabbix/scripts/discovery.sh
3、重启agent服务
#systemctl restart zabbix-agent.service
zabbix-server端操作
# zabbix_get -s 172.18.243.7 -p 10050 -k tcpportlisten
{
"data": [
{
"{#TCP_PORT}": "10000"
},
{
"{#TCP_PORT}": "10050"
},
{
"{#TCP_PORT}": "9400"
},
{
"{#TCP_PORT}": "9500"
}
]
}
说明:由于端口太多,这里只是部分端口
Web端的配置
1、创建模板:Template Ports Discovery
2、创建自动发现规则
3、创建监控项原型
4、创建图形原型
5、创建触发器
6、在主机中关联此模板
7、查看结果
酱紫就配置完成端口自动扫描并监控完成了,酱紫就可以通过监控端口实现监控服务是否正常了<^^>
监控指定端口范围内的端口
scripts]#vim discovery.sh
#!/usr/bin/python
__author__ = 'Yan'
import os
import json
data = {}
tcp_list = []
port_list = []
t = ['10050','10081','10087','10096','10097','10000','10073','10099','120000','17000','18050','18051','18081','22','28050','3000','32000','5500','80']
tt = []
command = " netstat -tnlp|egrep -i tcp|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort|uniq"
lines = os.popen(command).readlines()
for line in lines:
port = line.split()
port_list.append(port[0])
for i in port_list:
if i in t:
tt.appendinformation
for port in list(set(tt)):
port_dict = {}
port_dict['{#TCP_PORT}'] = port
tcp_list.append(port_dict)
data['data'] = tcp_list
jsonStr = json.dumps(data, sort_keys=True, indent=4)
print jsonStr
~
#scripts]#chmod +xdiscovery.sh
# /scripts]# ./discovery.sh
{
"data": [
{
"{#TCP_PORT}": "80"
},
{
"{#TCP_PORT}": "22"
},
{
"{#TCP_PORT}": "12000"
},
{
"{#TCP_PORT}": "10050"
}
]
}
OK!
来源:oschina
链接:https://my.oschina.net/u/4332567/blog/3556700