Python通过Zabbix API获得数据
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                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 responseresult.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 objectrequest = urllib2.Request(zabbix_url,get_host_data)for key in zabbix_header:request.add_header(key,zabbix_header[key])# get host listtry:result = urllib2.urlopen(request)except URLError as e:if hasattr(e, 'reason'):print 'We failed to reach a server.'print 'Reason: ', e.reasonelif hasattr(e, 'code'):print 'The server could not fulfill the request.'print 'Error code: ', e.codeelse: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獲取相應的graphidget_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
參考:http://itnihao.blog.51cto.com/1741976/1343524
?
轉載于:https://www.cnblogs.com/dreamer-fish/p/5485869.html
總結
以上是生活随笔為你收集整理的Python通过Zabbix API获得数据的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 基于 SWTBot 进行 Eclipse
- 下一篇: 加载的图片还会有未来吗?
