API request filter host.groups

Dears,

Task: to query for hosts with have same pattern in hostgroups.
Tried several approaches that does not work.

Anybody faced such issue?

Thanks,
/A

curl -k -s -u collector:pass -H 'Accept: application/json' -H 'X-HTTP-Method-Override: GET' -X POST \
'https://host:5665/v1/objects/hosts' -d '{ "attrs": [ "__name", "address", "vars", "notes"], "filter": "\"ALL-PRODUCTION\" in host.groups", "pretty": true }'
[works]


curl -k -s -u collector:pass -H 'Accept: application/json' -H 'X-HTTP-Method-Override: GET' -X POST \
'https://host:5665/v1/objects/hosts' -d '{ "attrs": [ "__name", "address", "vars", "notes"], "filter": "\"*PRODUCTION\" in host.groups", "pretty": true }'
{
"results": []
}


curl -k -s -u collector:pass -H 'Accept: application/json' -H 'X-HTTP-Method-Override: GET' -X POST \
'https://host:5665/v1/objects/hosts' -d '{ "attrs": [ "__name", "address", "vars", "notes"], "filter": "match(\"*PRODUCTION*\",host.groups)", "pretty": true }'
{
"results": []
}


curl -k -s -u collector:pass -H 'Accept: application/json' -H 'X-HTTP-Method-Override: GET' -X POST \
'https://host:5665/v1/objects/hosts' -d '{ "attrs": [ "__name", "address", "vars", "notes"], "filter": "host.groups==*PRODUCTION*", "pretty": true }'
{
"error": 404.0,
"status": "No objects found."
}


curl -k -s -u collector:pass -H 'Accept: application/json' -H 'X-HTTP-Method-Override: GET' -X POST \
'https://host:5665/v1/objects/hosts' -d '{ "attrs": [ "__name", "address", "vars", "notes"], "filter": "host.groups==\"*PRODUCTION*\"", "pretty": true }'
{
"results": []
}

Environment:

$ icinga2 -V
icinga2 - The Icinga 2 network monitoring daemon (version: 2.11.3-1)
System information:
Platform: Red Hat Enterprise Linux Server
Platform version: 7.8 (Maipo)
Kernel: Linux
Kernel version: 3.10.0-1062.18.1.el7.x86_64
Architecture: x86_64

Hello,

i assume your host_groups is an array. Then the only filter that will work is like "\"linux-servers\" in host.groups" }', No regex or wildcard is allowed/useable here.
If you want to do a regex or match you have to create a (string) variable like vars.string_host_groups = groups.to_string().

Regards,
Carsten

1 Like