python實(shí)現(xiàn)Zabbix-API監(jiān)控
做運(yùn)維的朋友應(yīng)該知道,公司IDC機(jī)房經(jīng)常有上架、下架、報(bào)修和報(bào)廢的服務(wù)器。如果服務(wù)器數(shù)量很多的時(shí)候很容易造成監(jiān)控遺漏。

大的互聯(lián)網(wǎng)公司把監(jiān)控系統(tǒng)和CMDB(資產(chǎn)管理系統(tǒng)|配置管理數(shù)據(jù)庫(kù)系統(tǒng))集成在一起,當(dāng)上架一臺(tái)新機(jī)器的時(shí)候CMDB里面會(huì)記錄相關(guān)的信息,Zabbix根據(jù)CMDB里面信息自動(dòng)Link相關(guān)的模塊,添加|刪除監(jiān)控。很多小的公司沒(méi)有資產(chǎn)管理系統(tǒng),但作為監(jiān)控的負(fù)責(zé)人應(yīng)該每天知道上架了哪些新的機(jī)器,確保能添加到Zabbix監(jiān)控里面。
首先給大家說(shuō)一下腳本思路:
1)通過(guò)Nmap工具掃描網(wǎng)段,掃描出已經(jīng)使用的IP地址。
2)通過(guò)Nmap檢測(cè)已經(jīng)掃描IP的3389或者22端口是否開(kāi)放,可以判斷那些事windows機(jī)器,那些是Linux機(jī)器。
3)Linux下面通過(guò)ssh + hostname命令找出Linux主機(jī)名。
4)Windows下面通過(guò)nmblookup -A 命令找出Windows主機(jī)名。
5)用Python腳本讀掃描結(jié)果文件,把主機(jī)名寫(xiě)到列表里面。
6)用Zabbix python API 調(diào)用已經(jīng)監(jiān)控的主機(jī)名,寫(xiě)到列表里面。
7)兩個(gè)列表取交集,用for循環(huán)判斷哪些主機(jī)名沒(méi)有監(jiān)控。
8)發(fā)郵件通知監(jiān)控負(fù)責(zé)人。
下面我分享一下我寫(xiě)的Python寫(xiě)的腳本,其中scan_machine.sh是我調(diào)用的用Shell寫(xiě)的關(guān)于Nmap掃描的腳本,scan_hostname.log是Nmap掃描的結(jié)果,里面內(nèi)容是IP 主機(jī)名。
#!/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,每臺(tái)會(huì)收到關(guān)于那些主機(jī)沒(méi)有添加監(jiān)控的信息。

總結(jié):
1)Zabbix API相關(guān)信息可以查看官方詳細(xì)資料。
2)通過(guò)該腳本可以知道那些主機(jī)沒(méi)有添加監(jiān)控,希望對(duì)大家有幫助,如果有更好的解決方法歡迎多多交流。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python獲取linux和windows系統(tǒng)指定接口的IP地址的步驟及代碼
這篇文章主要介紹了python獲取linux和windows系統(tǒng)指定接口的IP地址,本文分步驟通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
Python中使用jpype調(diào)用Jar包中的實(shí)現(xiàn)方法
這篇文章主要介紹了Python中使用jpype調(diào)用Jar包中的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
pytorch?液態(tài)算法實(shí)現(xiàn)瘦臉效果
在PS中,我們可以利用液化工具對(duì)人像進(jìn)行形變處理,例如瘦臉、瘦腿、放大眼睛等一系列的常規(guī)操作。今天我們來(lái)了解一下這些操作的算法原理,并用pytorch來(lái)實(shí)現(xiàn)瘦臉效果2021-11-11
Python實(shí)現(xiàn)使用request模塊下載圖片demo示例
這篇文章主要介紹了Python實(shí)現(xiàn)使用request模塊下載圖片,結(jié)合完整實(shí)例形式分析了Python基于requests模塊的流傳輸文件下載操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05

