zabbix 端口自動發現

比較實用針對基礎服務運行狀態檢測,自動端口發現加入監控與告警,稍作修改

腳本

python port_discover.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

# 使用python2 commands模塊

import re
import commands
import json

DROP_LIST = ['22','25','111']
# 排除端口

def filterList():
    DROP_str = "|".join(DROP_LIST)
    CMD="sudo netstat -pntl | awk '{print $4,$7}'|grep  [0-9] |egrep -vw '%s'" % (DROP_str)
    Result_Str = commands.getoutput(CMD)
    #print (Result_Str)
    tmp_list = Result_Str.split("\n") #每行加入列表
    new_dict = {}
    for line in tmp_list:
       # print (line)
       PORT_REG = re.search(r"(127.0.0.1:|:::|0.0.0.0:)(\d+).+\d+/(\S+)",line)
       if PORT_REG is not None:
           match_line =  (PORT_REG.groups())
           new_dict[ match_line[-1]]  =  match_line[-2]
    return new_dict

if __name__ == "__main__":
    Results = filterList()

    #格式化成適合zabbix lld的json數據
    ports = []
    for key  in  Results:
        ports += [{'{#PNAME}':key,'{#PPORT}':Results[key]}]
    print json.dumps({'data':ports},sort_keys=True,indent=4,separators=(',',':'))

返回zabbix數據格式

{
    "data":[
        {
            "{#PNAME}":"zabbix_proxy",
            "{#PPORT}":"10051"
        },
        {
            "{#PNAME}":"mysqld",
            "{#PPORT}":"3306"
        }
    ]
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章