zabbix—自动发现端口并监控

自动批量检查agent开放的端口

PS:若是服务器上的应用都是固定的,不会随机产生的均可以使用自动发现端口来监控;python

    若是服务器会随机出现端口且每次启动程序都会改变,能够采用第二种方法,来监控指定的端口json

约定

zabbix全部执行的脚本统一放置在 /etc/zabbix/scripts 目录下

#cd  /etc/zabbix/
#mkdir  scripts
#chown root:zabbix -R /etc/zabbix/scripts/
#chmod 755 /etc/zabbix/scripts/

 zabbix-agent操做

一、脚本内容vim

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

 二、添加key值bash

cat /etc/zabbix/zabbix_agentd.conf
添加以下内容:
UnsafeUserParameters=1
UserParameter=tcpportlisten,/etc/zabbix/scripts/discovery.sh

 三、重启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"
        }
    ]
}

说明:因为端口太多,这里只是部分端口app

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

Web端的配置

一、建立模板:Template Ports Discoveryspa

1548381455331-287.png

 

二、建立自动发现规则rest

1548381964762-229.png

 

 三、建立监控项原型code

1548382172332-289.png

 

 四、建立图形原型

1548382439980-357.png

 

 五、建立触发器

1548382627832-488.png

 

六、在主机中关联此模板

1547034089241-731.png

七、查看结果

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(客户端)
相关文章
相关标签/搜索