def Chaxun (groupid,items_value): Write it yourself, bring in the group ID and monitor the item key value, return the result of the host and the value. Make a judgment, if this host does not want to monitor the item, go to get rid of.
#!/usr/bin/env python#coding=utf-8import urllib.requestimport jsonimport reurl = ' 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) &nBsp; quit () return output[' result ']# #登陆的APIdef authenticate (url, Username, password): values = {' jsonrpc ': ' 2.0 ', ' method ': ' User.login ', ' params ': { ' user ': username, ' Password ': password }, ' id ': ' 0 ' } idvalue = requestjson (Url,values) return idvalueauth=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## Query host Project 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 ' &Nbsp; } 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 outputdef 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)) ## all hosts ' 10118 ': ' Cloud Host: 10.8.8.65 ', ' 10119 ': ' Cloud Host: 10.8.8.66 ', host_names = [] host_ids = [] for i in host_id: # #筛选, The host ID that has the compliance of the Monitoring project, and the host name host_id_value = items (auth, i) &NBsp;## [{' 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: ## use a host that meets the requirements get its name 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] ") # group id and The monitoring item you want to query print (ret)
This article is from the "what-all" blog, please be sure to keep this source http://hequan.blog.51cto.com/5701886/1955480
Get the values of all hosts using the Zabbix API with group ID and monitoring item key values