本文共 2725 字,大约阅读时间需要 9 分钟。
计划将ZABBIX的一些状态可以在另一个应用的显示GRAPH及链接。
故而在网上找了几个文档,作了一个测试。
https://www.zabbix.com/documentation/2.4/manual/api/reference/graph/get
http://www.xue163.com/334/1654/3340800.html
#!/usr/bin/env python#coding: utf-8import requestsimport jsonclass zabbixtools: def __init__(self): self.url = "http://10.1.1.1/api_jsonrpc.php" self.headers = { "Content-Type": "application/json"} self.authID = self.user_login() def user_login(self): data = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "guest", "password": "xxxxxxxx" }, "id": 0 } try: response = requests.post( self.url, data=json.dumps(data), headers=self.headers).json() except requests.RequestException as e: print(e) else: authID = response['result'] return authID def host_get(self,hostname): data = { "jsonrpc": "2.0", "method": "host.get", "params": { "output": "extend", "filter": { "host": [hostname,] } }, "auth": self.authID, "id": 1 } try: response = requests.post( self.url, data=json.dumps(data), headers=self.headers).json() except requests.RequestException as e: print(e) else: hostID = response['result'][0]['hostid'] return hostID def graph_get(self,hostid): data = { "jsonrpc": "2.0", "method": "graph.get", "params": { "output": "extend", "hostids": hostid, "sortfield": "name" }, "auth": self.authID, "id": 2 } try: response = requests.post( self.url, data=json.dumps(data), headers=self.headers).json() except requests.RequestException as e: print(e) else: graphID = [] for item in response['result']: graph_dict = {} graph_dict[item['graphid']] = item['name'] graphID.append(graph_dict) #print graphID return graphID def main(): test = zabbixtools() hostID = test.host_get("cnsz032955") graphID = test.graph_get(hostID) for graph_item in graphID: for key in graph_item: print graph_item[key],": http://10.1.1.1/charts.php?hostid={hostID}&graphid={graphID}".format(hostID=hostID, graphID=key)if __name__ == "__main__": main()
载图:
转载地址:http://bbzel.baihongyu.com/