Search code examples
pythonjsonzabbix

get host list from zabbix api with python


Please help me, give some ideas or some code)) I have the lists all hosts, and list all keys who exist in zabbix server. So, when i select needed key, how i can show which server have this key? In my example, how i can take list hosts, who have key vmware? Thanks My example:

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

Solution

  • 1.) select item ids with key vmware

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

    2.) then select hosts with these item ids

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