Search code examples
pythonzabbix

Zabbix API returning incorrect value when using host.get(groupids)


I am trying to use Zabbix's API to return all of the hosts belonging to a particular host group. Reading the documentation on host.get, I thought that the groupids parameter would be exactly what I needed to perform this task. It states: "Return only hosts that belong to the given groups".

The problem is that no matter what group I specify, the function returns every single host in all of the host groups. I have a workaround for it but that involves painstaking configuration of each host's visible name.

Here is just a small example I created to reproduce the issue. For reference, I am using Zabbix 2.2.3 and PyZabbix 0.6.

from pyzabbix import ZabbixAPI

zapi = ZabbixAPI("http://zabbix_URL/zabbix")
zapi.login("username", "password")

for host in zapi.host.get(filter={'groupids': '9'}):
    print host

I don't beleive that it is an error in my syntax(though it may be) because if I change the parameters to the following, it returns the exact host that I request.

for host in zapi.host.get(filter={'hostid': '10084'}):

Does anyone know if this is a bug in the API or am I just not understanding the groupids parameter?


Solution

  • It seems that "groupids" should be specified outside of "filter".

    Indeed, if I put the following inside "params", it returns all hosts as you described:

    {
        "output": "extend",
        "filter" : { "groupids": [ "14" ] }
    }
    

    But if I specify "groupids" directly, the Zabbix API only returns hosts for the specified group:

    {
        "output": "extend",
        "groupids": [ "14" ]
    }