zabbix—自动发现端口并监控

自动批量检查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"
        }
    ]
}

说明:由于端口太多,这里只是部分端口

 ps:172.18.243.7  #客户端IP
tcpportlisten   # /etc/zabbix/zabbix_agentd.confkey值

Web端的配置

1、创建模板:Template Ports Discovery

1548381455331-287.png

 

2、创建自动发现规则

1548381964762-229.png

 

 3、创建监控项原型

1548382172332-289.png

 

 4、创建图形原型

1548382439980-357.png

 

 5、创建触发器

1548382627832-488.png

 

6、在主机中关联此模板

1547034089241-731.png

7、查看结果

1547083136264-114.png

酱紫就配置完成端口自动扫描并监控完成了,酱紫就可以通过监控端口实现监控服务是否正常了<^^>

监控指定端口范围内的端口

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!

友情提示:Web端提示Value should be a J.SON object
原因为python脚本中使用了netstat -p参数,权限问题,zabbix_agentd是zabbix用户启动的,默认不能执行netstat -p等命令,导致从服务器取到的自动发现脚本为空。
解决办法:
chmod +s /bin/netstat(客户端)
posted @ 2019-04-28 09:53  琼兔  阅读(1869)  评论(0编辑  收藏  举报