python自動(dòng)統(tǒng)計(jì)zabbix系統(tǒng)監(jiān)控覆蓋率的示例代碼
腳本主要功能:
1)通過(guò)zabbix api接口采集所有監(jiān)控主機(jī)ip地址;
2)通過(guò)cmdb系統(tǒng)(藍(lán)鯨)接口采集所有生產(chǎn)主機(jī)IP地址、主機(jī)名、操作系統(tǒng)、電源狀態(tài);
3)以上2步返回?cái)?shù)據(jù)對(duì)比,找出未監(jiān)控主機(jī)ip地址,生成csv文件;
4)發(fā)送郵件。
腳本如下:
#!/usr/bin/python #coding:utf-8 import requests import json import re import time import csv from collections import Counter import smtplib from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication # 從cmdb系統(tǒng)獲取虛擬化生產(chǎn)主機(jī)ip def getCmdbProdHost(): url1 = 'http://paas.xxxx.com/api/c/compapi/v2/cc/search_inst/' data1 = { "bk_app_secret": "**********************", "bk_app_code": "bk_cmdb", "bk_username": "admin", "bk_obj_id": "host", "page": { "start": 0, "limit": 2000, "sort": "bk_inst_id" }, "fields": { "host": [ "bk_host_id", "bq_hostname", "bk_host_innerip", "bq_hosttype", "powerState", "bq_osname" ] } } r1 = requests.post(url1, json=data1) response_dict1 = r1.json() #print(response_dict1) prodip_dict = {} testip = "10.210.xx|10.210.xx|10.210.xx|10.210.xx|xx.xx.xx" #測(cè)試網(wǎng)段ip for i in response_dict1.get('data')["info"]: if i["bq_hosttype"] == "t2" and i["powerState"] == "poweredOn" and not re.search("UAT", i["bq_hostname"]) and not re.match(testip, i["bk_host_innerip"]): prodip_dictkey = i["bk_host_innerip"] #prodip_dictvalue = i["bq_hostname"] prodip_dictvalue = [i["bq_hostname"], i["bq_osname"], i["powerState"]] prodip_dict[prodip_dictkey] = prodip_dictvalue return prodip_dict #獲取zabbix系統(tǒng)登錄認(rèn)證 def getZabToken(url, post_headers, url_user, url_password): post_data = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": url_user, "password": url_password }, "id": 1 } ret = requests.post(url, data=json.dumps(post_data), headers=post_headers) return json.loads(ret.text).get("result") def getZabHost(url,post_headers,token): data = { "jsonrpc": "2.0", "method": "host.get", "params": { "output": [ "hostid", "host" ], "selectInterfaces": [ "interfaceid", "ip" ] }, "id": 2, "auth": token, } request = requests.post(url, headers=post_headers, data=json.dumps(data)) dict = json.loads(request.content) zab_ip = [] for i in dict['result']: zab_ip.append(i['host']) return zab_ip def compare(zabhostlist, cmdbhostdict): zabbixiplist = Counter(zabhostlist) cmdbiplist = Counter(list(cmdbhostdict.keys())) nomonip = {} for i in list((cmdbiplist - zabbixiplist).elements()): nomonip_value = cmdbhostdict[i] nomonip_key = i nomonip[nomonip_key] = nomonip_value print(nomonip) return nomonip class writeToCsv(object): def __init__(self,data,info): self.data = data self.info = info def write_to_csv(self): rows = self.data info = self.info csvfile = "zabbix未監(jiān)控生產(chǎn)系統(tǒng)IP列表" + info + time.strftime('_%Y%m%d%H%M%S', time.localtime(time.time())) + ".csv" # print(csvfile) # 創(chuàng)建文件對(duì)象 f = open(csvfile, 'w', newline='') # 通過(guò)文件創(chuàng)建csv對(duì)象 csv_write = csv.writer(f) # writerow: 按行寫(xiě)入, writerows: 是批量寫(xiě)入 # 寫(xiě)入數(shù)據(jù) 取列表的第一行字典,用字典的key值做為頭行數(shù)據(jù) # csv_write.writerow(rows[0].keys()) csv_write.writerow(["未監(jiān)控生產(chǎn)IP", "主機(jī)名", "操作系統(tǒng)", "電源狀態(tài)"]) # 循環(huán)里面的字典,將value作為數(shù)據(jù)寫(xiě)入進(jìn)去 ip = list(rows.keys()) hostname = list(rows.values()) for row in range(len(ip)): csv_write.writerow([ip[row], hostname[row][0], hostname[row][1], hostname[row][2]]) # 關(guān)閉打開(kāi)的文件 f.close() print("讀寫(xiě)完成:",csvfile) return csvfile def sendmail(csvfile,receiver): sender = 'xxx@xxx.com' smtpserver = 'xx.xx.xx.xx' username = 'xxx@xxx.com' password = '******' mail_title = 'zabbix未監(jiān)控生產(chǎn)主機(jī)IP地址' # 創(chuàng)建一個(gè)帶附件的實(shí)例 message = MIMEMultipart() message['From'] = sender message['To'] = ','.join(receiver) message['Subject'] = Header(mail_title, 'utf-8') # 郵件正文內(nèi)容 message.attach(MIMEText('每日自動(dòng)統(tǒng)計(jì)監(jiān)控覆蓋率', 'plain', 'utf-8')) # 構(gòu)造附件 att1 = MIMEApplication(open(csvfile, 'rb').read()) # 打開(kāi)附件 att1.add_header('Content-Disposition', 'attachment', filename=csvfile) # 為附件命名 message.attach(att1) smtpObj = smtplib.SMTP_SSL() # 注意:如果遇到發(fā)送失敗的情況(提示遠(yuǎn)程主機(jī)拒接連接),這里要使用SMTP_SSL方法 smtpObj.connect(smtpserver) smtpObj.login(username, password) smtpObj.sendmail(sender, message['To'].split(','), message.as_string()) print("郵件發(fā)送成功?。?!") smtpObj.quit() if __name__ == '__main__': url = 'http://xx.xx.xx.xx/api_jsonrpc.php' #zabbix監(jiān)控系統(tǒng)接口地址 post_headers = {'Content-Type': 'application/json'} url_user = "Admin" url_passwd = "******" auth = getZabToken(url,post_headers,url_user,url_passwd) zabhostlist = getZabHost(url,post_headers,auth) #獲取zabbix監(jiān)控主機(jī)ip地址列表 cmdbhostdict = getCmdbProdHost() #獲取cmdb主機(jī)地址列表 #zabbix監(jiān)控主機(jī)和cmdb主機(jī)做比較 data = compare(zabhostlist, cmdbhostdict) #導(dǎo)出csv文件 info = '統(tǒng)計(jì)' write = writeToCsv(data, info) resp = write.write_to_csv() receiver = ['hushanshan2@bngrp.com'] #y郵件接收人,多人用逗號(hào)區(qū)分開(kāi) sendmail(resp, receiver)
到此這篇關(guān)于python自動(dòng)統(tǒng)計(jì)zabbix系統(tǒng)監(jiān)控覆蓋率的文章就介紹到這了,更多相關(guān)python統(tǒng)計(jì)zabbix內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python將一個(gè)CSV文件里的數(shù)據(jù)追加到另一個(gè)CSV文件的方法
今天小編就為大家分享一篇Python將一個(gè)CSV文件里的數(shù)據(jù)追加到另一個(gè)CSV文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07ORM Django 終端打印 SQL 語(yǔ)句實(shí)現(xiàn)解析
這篇文章主要介紹了ORM Django 終端打印 SQL 語(yǔ)句實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08python驗(yàn)證公網(wǎng)ip與內(nèi)網(wǎng)ip的實(shí)現(xiàn)示例
本文主要介紹了python驗(yàn)證公網(wǎng)ip與內(nèi)網(wǎng)ip的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07shelve 用來(lái)持久化任意的Python對(duì)象實(shí)例代碼
這篇文章主要介紹了shelve 用來(lái)持久化任意的Python對(duì)象實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10Python HTTP客戶(hù)端自定義Cookie實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Python HTTP客戶(hù)端自定義Cookie實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04解決Scrapy安裝錯(cuò)誤:Microsoft Visual C++ 14.0 is required...
下面小編就為大家?guī)?lái)一篇解決Scrapy安裝錯(cuò)誤:Microsoft Visual C++ 14.0 is required...的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10如何從PyTorch中獲取過(guò)程特征圖實(shí)例詳解
特征提取是圖像處理過(guò)程中常需要用到的一種方法,其效果好壞對(duì)模型的泛化能力有至關(guān)重要的影響,下面這篇文章主要給大家介紹了關(guān)于如何從PyTorch中獲取過(guò)程特征圖的相關(guān)資料,需要的朋友可以參考下2023-01-01