zabbix強大地方在于有強大的api,zabbix 的api可以拿到zabbix大部分數據,目前我所需的數據基本可以通過api獲取,以下是通過zabbix api獲取的主機信息python代碼,其他數據也如此類推,api使用方法可參見官網文檔:
#!/usr/bin/env python#-*- coding: utf-8 -*-import jsonimport urllib2from urllib2 import URLErrorfrom login import zabbix_logint=zabbix_login()def hostid_get(): data = json.dumps( { "jsonrpc": "2.0", "method": "host.get", "params": { "output": "extend", "groupids":14, "filter":{"flags": "4" }, }, "auth":t.user_login(), "id": 1, }) request = urllib2.Request(t.url, data) for key in t.header: request.add_header(key, t.header[key]) try: result = urllib2.urlopen(request) except URLError as e: if hasattr(e, 'reason'): print 'zabbix server is faile' print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'zabbix server not request.' print 'Error code: ', e.code else: response = json.loads(result.read()) result.close() hostid=[] hostname=[] for host in response['result']: hostid.append(host['hostid']) hostname.append(host['name']) return hostid,hostname if __name__ == "__main__": a,b=hostid_get() i=0 n=len(b) for i in range(n): print a[i],b[i]
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答