zabbix的平常监控-自动发现端口并监测(服务器开启的全部端口或监控指定端口)(十三)

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

注:此方法给监控磁盘IO(即十二)篇过程同样;python

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

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

 

约定:

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

chown root:zabbix -R /etc/zabbix/scripts/ chmod 750 /etc/zabbix/scripts/ chmod 550 /etc/zabbix/scripts/iostat.sh

1、agent操做:

一、脚本内容

[root@agent scripts]# cat check_port1.sh #!/usr/bin/env python #coding:utf-8 import os, json port_list=[] port_dict={"data":None} cmd='''''netstat -tnlp|egrep -i "$1"|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort |uniq 2>/dev/null''' local_ports=os.popen(cmd).readlines() for port in local_ports: pdict={} pdict["{#TCP_PORT}"]=port.replace("\n", "") port_list.append(pdict) port_dict["data"]=port_list jsonStr = json.dumps(port_dict, sort_keys=True, indent=4) print jsonStr [root@agent scripts]# 
[root@agent scripts]#chmod +s /usr/bin/netstat

二、添加key值

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

 

三、重启agent服务

systemctl restart zabbix-agent.service

 

 

2、zabbix-server端操做

一、建立模板:Template Ports Discovery

二、建立自动发现规则

 三、建立监控项原型

 

 

 四、建立图形原型

 

 五、建立触发器服务器

此方法能够批量为多个监控端口添加相同的阈值!!!app

 

 

 3、结果

 

 

 

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

原博文地址:http://blog.chinaunix.net/uid-29680017-id-5768335.htmltcp

一、脚本内容ui

[root@agent scripts]#vim prot.py #!/usr/bin/python __author__ = 'Yan' import os import json data = {} tcp_list = [] port_list = [] t = ['3306','8080','2002','2003','10066'] tt = [] command = "sudo 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.append(i) 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

 

 二、执行结果url