一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python實現Zabbix-API監控

python實現Zabbix-API監控

2021-04-02 00:49CTO老王 Python

這篇文章主要為大家詳細介紹了python實現Zabbix-API監控,具有一定的參考價值,感興趣的小伙伴們可以參考一下

python實現Zabbix-API監控

 

  做運維的朋友應該知道,公司idc機房經常有上架、下架、報修和報廢的服務器。如果服務器數量很多的時候很容易造成監控遺漏。
       大的互聯網公司把監控系統和cmdb(資產管理系統|配置管理數據庫系統)集成在一起,當上架一臺新機器的時候cmdb里面會記錄相關的信息,zabbix根據cmdb里面信息自動link相關的模塊,添加|刪除監控。很多小的公司沒有資產管理系統,但作為監控的負責人應該每天知道上架了哪些新的機器,確保能添加到zabbix監控里面。
      首先給大家說一下腳本思路:
1)通過nmap工具掃描網段,掃描出已經使用的ip地址。
2)通過nmap檢測已經掃描ip的3389或者22端口是否開放,可以判斷那些事windows機器,那些是linux機器。
3)linux下面通過ssh + hostname命令找出linux主機名。
4)windows下面通過nmblookup -a 命令找出windows主機名。
5)用python腳本讀掃描結果文件,把主機名寫到列表里面。
6)用zabbix python api 調用已經監控的主機名,寫到列表里面。
7)兩個列表取交集,用for循環判斷哪些主機名沒有監控。
8)發郵件通知監控負責人。

    下面我分享一下我寫的python寫的腳本,其中scan_machine.sh是我調用的用shell寫的關于nmap掃描的腳本,scan_hostname.log是nmap掃描的結果,里面內容是ip 主機名。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python#create by:sfzhang 20140820#coding=utf-8import os,sysimport jsonimport urllib2import datetime,timefrom urllib2 import urlerror
nmap_cmd = "/shell/machine/scan_machine.sh"def runcmd(command):
    global mail_cmd
    mail_cmd = '''mail -s "report on not monitor hosts of zabbix" shifeng_zhang88 < /shell/machine/result/result.txt'''
    return os.system(command)runcmd(nmap_cmd)def nmap_host():
    hostiplst = []
    hostnamelst = []
    f = file('/shell/machine/result/scan_hostname.log')
    for line in f.readlines():
        hostip = line.split()[0]
        hostname = line.split()[1]
        hostiplst.append(hostip)
        hostnamelst.append(hostname)
    hostnamelst.sort()
    #print hostiplst
    return hostnamelst
    f.close()def zabbix_host():
    zabbixhostlst= []
    #based url and required header
    url = "http://192.168.161.128/api_jsonrpc.php"
    header = {"content-type": "application/json"}
    #request json
    data = json.dumps(
    
    "jsonrpc": "2.0",
      "method": "host.get",
      "params":{
          "output":["hostid","name"],
         "filter":{"host":""}
    },   
    #auth id
    "auth":"zabbix auth id",
    "id": 1,
    })
    #create request object
    request = urllib2.request(url,data)
    for key in header:   
    request.add_header(key,header[key])
    #get host list
    try:
    result = urllib2.urlopen(request)
    except urlerror as e:
    print "the server could not fulfill the request.",e.reason    else:
        reponse = json.loads(result.read())
    result.close()
    #print "number of hosts:",len(reponse['result'])
    for host in reponse['result']:
            #print "host id:",host['hostid'],"host name:",host['name']
            zbxhosts=host['name']
            zabbixhostlst.append(zbxhosts)
        zabbixhostlst.sort()
        return zabbixhostlst    def main():
    nmaphostlst = nmap_host() 
    zbxhostlst = zabbix_host() 
    diff = list(set(nmaphostlst) ^ set(zbxhostlst)) 
    content = "\n"
    nomonitorlst = []
    if len(diff) != 0
        for host in diff: 
            if host in nmaphostlst: 
                nomonitorlst.append(host)
                
    else
        sys.exit()
    #print zbxhostlst
    string = '\n'.join(nomonitorlst)
    f = file('/shell/machine/result/result.txt','w')
    f.write(string)
    f.flush()
    f.close()
    runcmd(mail_cmd)if __name__ == "__main__"
    main()

 

   把腳本添加到crontab,每臺會收到關于那些主機沒有添加監控的信息。

python實現Zabbix-API監控

 

 總結:

     1)zabbix api相關信息可以查看官方詳細資料,看不懂英文的可以參考下面這篇文檔。http://paperplane.ruhoh.com/zabbix/intro-to-zabbix-api/

     2)通過該腳本可以知道那些主機沒有添加監控,希望對大家有幫助,如果有更好的解決方法歡迎多多交流。     

原文鏈接:https://www.cnblogs.com/Javame/p/9660253.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲一欧洲中文字幕在线 | 国产在线观看人成激情视频 | 紧身牛仔裤美女被啪啪久久网 | 波多野结衣女教师在线观看 | 国内精品在线播放 | 亚洲精品成人456在线播放 | 欧美高清在线不卡免费观看 | 国产极品久久 | 日韩免费观看成第15集 | 无人区国产大片 | 亚洲色域网 | 国产精品免费_区二区三区观看 | 免费一级欧美片在线观免看 | 精品国产免费第一区二区三区日韩 | 日本中文字幕在线观看视频 | ass巨大胖女人sias | www.色姐姐 | 污黄在线观看 | 美女秘密网站 | 亚洲欧美精品一区天堂久久 | 日本艳鉧动漫1~6完整版在 | 操大肥b | 欧亚专线欧洲m码可遇不可求 | 日本不卡一区二区三区在线观看 | 情趣内衣情趣玩具play | 日本一本二本三区免费 | freesex1718处xx| 国产精品久久久久久久久久久搜索 | 国产成+人+综合+欧美 亚洲 | 鬼吹灯天星术在线高清观看 | 成人一区二区丝袜美腿 | 8x在线永久成人影院 | 欧美亚洲国产精品久久第一页 | 国产伦精品一区二区三区女 | 国产精品久久久久久久久久久久 | 91免费精品国自产拍在线可以看 | 国产精品视频二区不卡 | 久久88综合 | 免费看国产精品久久久久 | 天天操天天射天天色 | 精品视频在线播放 |