从zabbix api获取主机列表与python

时间:2014-10-23 13:55:06

标签: python json zabbix

请帮助我,提出一些想法或一些代码)) 我列出了所有主机,并列出了zabbix服务器中存在的所有密钥。 那么,当我选择所需的密钥时,我如何显示哪个服务器有这个密钥? 在我的例子中,我如何能够获得具有密钥vmware的列表主机?谢谢 我的例子:

data = {
                    "jsonrpc": "2.0",
                    "method": "host.get",
                    "params": {
                            "output": "extend"
                    },
                    "auth": authId,
                    "id": 2
            }
            request = requests.post(path, data=json.dumps(data), headers={'content-type':'application/json-rpc'})
            result = []
            request_res = request.json()['result']
            for item in request_res:
                    result.append(item['hostid'])
            data = {
                    "jsonrpc": "2.0",
                    "method": "item.get",
                    "params": {
                            "output": "extend",
                            "hostids": hosts_list,
                            "sortfield": "name"
                    },
                    "auth": authId,
                    "id": 1
                    }
            request = requests.post(path, data=json.dumps(data), headers={'content-type':'application/json-rpc'})
            request_res = request.json()['result']
            keys_list = []
            for items in request_res:
                    keys_list.append(items['key_'])
            keys_list = dict.fromkeys(keys_list).keys()
            data = {
                    "jsonrpc": "2.0",
                    "method": "host.get",
                    "params": {
                            "filter":{
                                    "key_" : "vmware"
                            },
                            "output": "extend",
                    },
                    "auth": authId,
                    "id": 2
            }
            request = requests.post(path, data=json.dumps(data), headers={'content-type':'application/json-rpc'})
            request_res = request.json()['result']
            print request_res

1 个答案:

答案 0 :(得分:1)

1。)使用密钥vmware选择项目ID

                "method": "item.get",
                "params": {
                        "output": ["itemid"],
                        "search":{
                                "key_" : "vmware"
                        }                            
                }

2.)然后选择包含这些项目ID的主机

                "method": "host.get",
                "params": {
                        "output": "extend",
                        "itemids": [<itemsids from previous step>]                            
                },