您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Python中對zabbix api進行調用,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
前提準備:
1.使用python requests模塊
2.了解json
3.zabbix api的具體調用建議先瀏覽一下官網
先上代碼:
import requests,json # #url一定要正確,IP地址換成自己zabbix服務器的 zbx_url = "http://192.168.60.130:3080/zabbix/api_jsonrpc.php" #在post請求頭部必須要有 'Content-Type': 'application/json-rpc' headers = {'Content-Type': 'application/json-rpc'} #傳遞json 數據到api;登錄 login = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1 } #首次登陸不用在json字段中寫 auth,否則會有相關的報錯 #將數據發送到api ret = requests.post(zbx_url, data=json.dumps(login), headers=headers) #對結果進行序列化 ret = ret.json() auth = ret['result'] #獲取問題主機json data = { "jsonrpc": "2.0", "method":"trigger.get", "params": { # output表示輸出結果包含參數有哪些 "output": [ "triggerid", "description", "status", "value", "priority", "lastchange", "recovery_mode", "hosts", "state", ], "selectHosts": "hosts", # 需包含主機ID信息,以便于根據主機ID查詢主機信息 "selectItems":"items", "filter": { # 篩選條件 "value": 1,#value值為1表示有問題 "status": 0#status為0表示已啟用的trigger }, }, "auth":auth,#這里的auth就是登錄后獲取的 'id':'1'#這個id可以隨意 } #將查詢數據發送到zabbix-server ret = requests.post(zbx_url,data=json.dumps(data),headers=headers) respone_result = ret.json()['result']#對結果進行json序列化 print(respone_result)
下面簡單介紹一下上訴代碼:
要調用zabbix api獲取數據,首先要獲得auth這一串字符用戶后續的內容獲取,auth可以看做是一種你與zabbix-server之間的"暗號";
登錄的json內容之所以這樣寫是zabbix官方規定的,json字符串里面千萬不能使用tab鍵。
login = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", #根據自己的情況填 "password": "zabbix" #根據自己的條件填寫 }, "id": 1 }
獲取問題主機的json字符串建議先瀏覽一下官網的說明,要強調的是output和filter這兩個key,output就是zabbix api返回來的內容,filter相當于是過濾條件:
"filter": { # 篩選條件 "value": 1, #value值為1表示有問題 "status": 0 #status為0表示已啟用的trigger },
上訴代碼表示 value=1 and status=0,是一種與關系,很像查數據庫表時候的過濾操作。
強烈建議先大概瀏覽一下官網文檔
PS:Python通過Zabbix API獲得數據的方法
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 地址,用戶名,密碼,這里修改為自己實際的參數 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 #用戶認證信息的部分,最終的目的是得到一個SESSIONID #這里是生成一個json格式的數據,用戶名和密碼 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) #對于出錯新的處理 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是否在返回的數據中 if 'result' in response: auth_code=response['result'] else: print response['error']['data'] # request json #用得到的SESSIONID去通過驗證,獲取主機的信息(用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() #將所有的主機信息顯示出來 for r in response['result']: # print r['hostid'],r['host'] host_list.append(r['hostid']) #顯示主機的個數 print "Number Of Hosts: ", len(host_list) #返回所有hostid==10251的主機,并只查詢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獲取相應的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
上述內容就是怎么在Python中對zabbix api進行調用,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。