動批量檢查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