zabbix—自动发现端口并监控
生活随笔
收集整理的這篇文章主要介紹了
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++))doprintf '\n\t\t{'printf "\"{#TCP_PORT}\":\"${portarray[$i]}\"}"if [ $i -lt $[$length-1] ];thenprintf ','fidone 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
2、創建自動發現規則
?3、創建監控項原型
?4、創建圖形原型
?5、創建觸發器
6、在主機中關聯此模板
7、查看結果
醬紫就配置完成端口自動掃描并監控完成了,醬紫就可以通過監控端口實現監控服務是否正常了<^^>
監控指定端口范圍內的端口
scripts]#vim discovery.sh#!/usr/bin/python __author__ = 'Yan' import os import jsondata = {} 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}'] = porttcp_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(客戶端)轉載于:https://www.cnblogs.com/yaun1498078591/p/10782253.html
總結
以上是生活随笔為你收集整理的zabbix—自动发现端口并监控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FixedThreadPool吞掉了异常
- 下一篇: thymeleaf 学习笔记