PS:若是服務器上的應用都是固定的,不會隨機產生的均可以使用自動發現端口來監控;python
若是服務器會隨機出現端口且每次啓動程序都會改變,能夠採用第二種方法,來監控指定的端口json
zabbix全部執行的腳本統一放置在 /etc/zabbix/scripts 目錄下 #cd /etc/zabbix/ #mkdir scripts #chown root:zabbix -R /etc/zabbix/scripts/ #chmod 755 /etc/zabbix/scripts/
一、腳本內容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_get -s 172.18.243.7 -p 10050 -k tcpportlisten { "data": [ { "{#TCP_PORT}": "10000" }, { "{#TCP_PORT}": "10050" }, { "{#TCP_PORT}": "9400" }, { "{#TCP_PORT}": "9500" } ] }
說明:因爲端口太多,這裏只是部分端口app
一、建立模板:Template Ports Discoveryspa
二、建立自動發現規則rest
三、建立監控項原型code
四、建立圖形原型
五、建立觸發器
六、在主機中關聯此模板
七、查看結果
醬紫就配置完成端口自動掃描並監控完成了,醬紫就能夠經過監控端口實現監控服務是否正常了<^^>
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!