利用zabbix api 帶入 組ID 和 監控項目鍵值 獲取所有主機的值
阿新 • • 發佈:2017-08-12
api 調用 zabbix
def chaxun(groupid,items_value): 是自己寫的,帶入組ID 和 監控項目鍵值,返回 主機和值的結果。 做了判斷,如果這個主機沒有想要的監控項目,就去除掉。
#!/usr/bin/env python #coding=utf-8 import urllib.request import json import re url = ‘http://XXXXXXXXXXXXXXXX/api_jsonrpc.php‘ username = ‘Admin‘ password = ‘XXXXX‘ #登陸 def requestJson(url,values): data = json.dumps(values).encode(‘utf-8‘) req = urllib.request.Request(url, data, {‘Content-Type‘: ‘application/json-rpc‘}) response = urllib.request.urlopen(req, data) a = response.read().decode(encoding=‘utf-8‘) output = json.loads(a) try: message = output[‘result‘] except: message = output[‘error‘][‘data‘] print(message) quit() return output[‘result‘] ##登陸的API def authenticate(url, username, password): values = {‘jsonrpc‘: ‘2.0‘, ‘method‘: ‘user.login‘, ‘params‘: { ‘user‘: username, ‘password‘: password }, ‘id‘: ‘0‘ } idvalue = requestJson(url,values) return idvalue auth=authenticate(url,username,password) ##查詢組ID {‘groupid‘: ‘8‘, ‘name‘: ‘Switch‘} def groups(auth): values = { "jsonrpc":"2.0", "method":"hostgroup.get", "params":{ "output":["groupid","name"], }, ‘auth‘: auth, ‘id‘: ‘1‘ } output = requestJson(url,values) return output ##查詢主機 def hosts(auth,a): values = { "jsonrpc":"2.0", "method":"host.get", "params":{ "output":["groupid","name"], "groupids": a, }, ‘auth‘: auth, ‘id‘: ‘1‘ } output = requestJson(url,values) return output ##查詢主機項目 def items(auth,a): values = { "jsonrpc":"2.0", "method":"item.get", "params":{ "output": ["itemids", "key_"], "hostids": a, }, ‘auth‘: auth, ‘id‘: ‘1‘ } output = requestJson(url,values) return output ##查詢項目的歷史值 ‘lastvalue‘: ‘-14760.0000‘ def his(auth,itemids): values = { "jsonrpc":"2.0", "method":"item.get", "params":{ "output": "extend", "history": 0, "itemids": itemids, "sortfield": "itemid", "sortorder": "DESC", "limit": 1 }, ‘auth‘: auth, ‘id‘: ‘1‘ } output = requestJson(url,values) return output ##查詢觸發項目值和監控項目 def trigger(auth): values = { "jsonrpc":"2.0", "method": "trigger.get", "params": { "output": [ "description", ], "filter": { "hostid": "10108", }, "selectItems":"", "sortfield": "hostname", "sortorder": "DESC" }, ‘auth‘: auth, ‘id‘: ‘1‘ } output = requestJson(url,values) return output def chaxun(groupid,items_value): host = hosts(auth, groupid) ##獲取所有主機 host_name = [] host_id = [] for i in host: host_id.append(i[‘hostid‘]) host_name.append(i[‘name‘]) hostall = dict(zip(host_id, host_name)) ## 所有主機 ‘10118‘: ‘雲主機:10.8.8.65‘,‘10119‘: ‘雲主機:10.8.8.66‘, host_names = [] host_ids = [] for i in host_id: ##篩選, 有監控項目的 符合要求的主機ID,和主機名字 host_id_value = items(auth, i) ## [{‘itemid‘: ‘29272‘, ‘key_‘: ‘BiosDate‘},{‘itemid‘: ‘29272‘, ‘key_‘: ‘BiosDate‘}] for index, value in enumerate(host_id_value): for k, v in value.items(): if items_value in v: ## items_value host_ids.append(host_id_value[index][‘itemid‘]) host_names.append(i) host_names_list = [] for i in host_names: ## 用符合要求的主機 獲取它的名字 for k, v in hostall.items(): if i in k: host_names_list.append(v) history_value = [] for i in host_ids: ##查詢主機ID的最新歷史值 history = his(auth, i) history_value.append(history[0][‘lastvalue‘]) host_value = dict(zip(host_names_list, history_value)) return host_value #返回主機和值的對應關系 ret = chaxun(5, "ProcThreadCount.[2]")# 組ID 和 想要查詢的監控項目 print(ret)
本文出自 “何全” 博客,請務必保留此出處http://hequan.blog.51cto.com/5701886/1955480
利用zabbix api 帶入 組ID 和 監控項目鍵值 獲取所有主機的值