本篇文章為大家展示了怎么在Python中對(duì)zabbix api進(jìn)行調(diào)用,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對(duì)外擴(kuò)展宣傳的重要窗口,一個(gè)合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺(tái),成都創(chuàng)新互聯(lián)面向各種領(lǐng)域:成都宴會(huì)酒店設(shè)計(jì)等網(wǎng)站設(shè)計(jì)、營銷型網(wǎng)站解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。前提準(zhǔn)備:
1.使用python requests模塊
2.了解json
3.zabbix api的具體調(diào)用建議先瀏覽一下官網(wǎng)
先上代碼:
import requests,json # #url一定要正確,IP地址換成自己zabbix服務(wù)器的 zbx_url = "http://192.168.60.130:3080/zabbix/api_jsonrpc.php" #在post請(qǐng)求頭部必須要有 'Content-Type': 'application/json-rpc' headers = {'Content-Type': 'application/json-rpc'} #傳遞json 數(shù)據(jù)到api;登錄 login = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1 } #首次登陸不用在json字段中寫 auth,否則會(huì)有相關(guān)的報(bào)錯(cuò) #將數(shù)據(jù)發(fā)送到api ret = requests.post(zbx_url, data=json.dumps(login), headers=headers) #對(duì)結(jié)果進(jìn)行序列化 ret = ret.json() auth = ret['result'] #獲取問題主機(jī)json data = { "jsonrpc": "2.0", "method":"trigger.get", "params": { # output表示輸出結(jié)果包含參數(shù)有哪些 "output": [ "triggerid", "description", "status", "value", "priority", "lastchange", "recovery_mode", "hosts", "state", ], "selectHosts": "hosts", # 需包含主機(jī)ID信息,以便于根據(jù)主機(jī)ID查詢主機(jī)信息 "selectItems":"items", "filter": { # 篩選條件 "value": 1,#value值為1表示有問題 "status": 0#status為0表示已啟用的trigger }, }, "auth":auth,#這里的auth就是登錄后獲取的 'id':'1'#這個(gè)id可以隨意 } #將查詢數(shù)據(jù)發(fā)送到zabbix-server ret = requests.post(zbx_url,data=json.dumps(data),headers=headers) respone_result = ret.json()['result']#對(duì)結(jié)果進(jìn)行json序列化 print(respone_result)
下面簡單介紹一下上訴代碼:
要調(diào)用zabbix api獲取數(shù)據(jù),首先要獲得auth這一串字符用戶后續(xù)的內(nèi)容獲取,auth可以看做是一種你與zabbix-server之間的"暗號(hào)";
登錄的json內(nèi)容之所以這樣寫是zabbix官方規(guī)定的,json字符串里面千萬不能使用tab鍵。
login = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", #根據(jù)自己的情況填 "password": "zabbix" #根據(jù)自己的條件填寫 }, "id": 1 }
獲取問題主機(jī)的json字符串建議先瀏覽一下官網(wǎng)的說明,要強(qiáng)調(diào)的是output和filter這兩個(gè)key,output就是zabbix api返回來的內(nèi)容,filter相當(dāng)于是過濾條件:
"filter": { # 篩選條件 "value": 1, #value值為1表示有問題 "status": 0 #status為0表示已啟用的trigger },
上訴代碼表示 value=1 and status=0,是一種與關(guān)系,很像查數(shù)據(jù)庫表時(shí)候的過濾操作。
強(qiáng)烈建議先大概瀏覽一下官網(wǎng)文檔
PS:Python通過Zabbix API獲得數(shù)據(jù)的方法
Zabbix API查詢:https://www.zabbix.com/documentation/2.0/manual/appendix/api/api
import json,urllib2 from urllib2 import Request, urlopen, URLError, HTTPError #url and url header #zabbix的api 地址,用戶名,密碼,這里修改為自己實(shí)際的參數(shù) zabbix_url="http://10.16.2.40/zabbix/api_jsonrpc.php" zabbix_header = {"Content-Type":"application/json"} zabbix_user = "admin" zabbix_pass = "password" auth_code = "" #auth user and password #用戶認(rèn)證信息的部分,最終的目的是得到一個(gè)SESSIONID #這里是生成一個(gè)json格式的數(shù)據(jù),用戶名和密碼 auth_data = json.dumps( { "jsonrpc":"2.0", "method":"user.login", "params": { "user":zabbix_user, "password":zabbix_pass }, "id":0 }) # create request object request = urllib2.Request(zabbix_url,auth_data) for key in zabbix_header: request.add_header(key,zabbix_header[key]) try: result = urllib2.urlopen(request) #對(duì)于出錯(cuò)新的處理 except HTTPError, e: print 'The server couldn\'t fulfill the request, Error code: ', e.code except URLError, e: print 'We failed to reach a server.Reason: ', e.reason else: response=json.loads(result.read()) print response result.close() #判斷SESSIONID是否在返回的數(shù)據(jù)中 if 'result' in response: auth_code=response['result'] else: print response['error']['data'] # request json #用得到的SESSIONID去通過驗(yàn)證,獲取主機(jī)的信息(用http.get方法) if len(auth_code) <> 0: host_list=[] get_host_data = json.dumps( { "jsonrpc":"2.0", "method":"host.get", "params":{ "output": "extend", }, "auth":auth_code, "id":1, }) # create request object request = urllib2.Request(zabbix_url,get_host_data) for key in zabbix_header: request.add_header(key,zabbix_header[key]) # get host list try: result = urllib2.urlopen(request) except URLError as e: if hasattr(e, 'reason'): print 'We failed to reach a server.' print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'The server could not fulfill the request.' print 'Error code: ', e.code else: response = json.loads(result.read()) result.close() #將所有的主機(jī)信息顯示出來 for r in response['result']: # print r['hostid'],r['host'] host_list.append(r['hostid']) #顯示主機(jī)的個(gè)數(shù) print "Number Of Hosts: ", len(host_list) #返回所有hostid==10251的主機(jī),并只查詢name包含“CPU Usage”字段的item,并按照name排序 get_item_data = json.dumps({ "jsonrpc": "2.0", "method": "item.get", "params": { "output": "extend", "hostids": "10251" "search": { #"key_": 'perf_counter[\Processor Information(_Total)\% Processor Time]' "name": "CPU Usage" }, "sortfield": "name" }, "auth": auth_code, "id": 1 }) request = urllib2.Request(zabbix_url,get_item_data) for key in zabbix_header: request.add_header(key,zabbix_header[key]) result = urllib2.urlopen(request) try: result = urllib2.urlopen(request) response = json.loads(result.read()) for r in response['result']: print r['itemid'],r['hostid'] result.close() except: pass #通過hostid獲取相應(yīng)的graphid get_graph_data = json.dumps({ "jsonrpc": "2.0", "method": "graphitem.get", "params": { "output": "extend", "expandData": 1, "itemids": "33712" }, "auth": auth_code, "id": 1 }) request = urllib2.Request(zabbix_url,get_graph_data) for key in zabbix_header: request.add_header(key,zabbix_header[key]) result = urllib2.urlopen(request) try: result = urllib2.urlopen(request) response = json.loads(result.read()) for r in response['result']: print r['itemid'],r['graphid'] result.close() except: pass
上述內(nèi)容就是怎么在Python中對(duì)zabbix api進(jìn)行調(diào)用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。