Search code examples
apizabbix

How to search Host Groups in Zabbix API


I want to list all Host Groups that match some search criteria.

I've tried that:

data = '{"jsonrpc": "2.0",                
         "method": "hostgroup.get",       
         "params": {                      
             "output": "extend",          
              "search": {                 
                 "name": [                
                     "' + group_name + '"
                 ]                        
            },                            
         },                               
         "id":' + str(msg_id) + ',        
         "auth": "' + auth + '"           
       }'

But that is not a correct syntax.

I also tried this:

data = '{"jsonrpc": "2.0",                
     "method": "hostgroup.get",       
     "params": {                      
         "output": "extend",          
          "filter": {                 
             "name": [                
                 "' + group_name + '"
             ]                        
        },                            
     },                               
     "id":' + str(msg_id) + ',        
     "auth": "' + auth + '"           
   }'

This one works, but it only matches exactly the group name. And, so, it always returns 1 or 0 matches.

I tried adding the "options":"searchWildcardsEnabled" option in this last query, but it didn't make a difference in the result (i.e. it didn't produce multiple groups as output).


Solution

  • I've found the correct way. I'll post it here in case anyone else needs it later.

    data = '{"jsonrpc": "2.0",                
         "method": "hostgroup.get",       
         "params": {                      
             "output": "extend",          
              "search": {                 
                 "name": [                
                     "' + group_name + '"
                 ]                        
            }                             
         },                               
         "id":' + str(msg_id) + ',        
         "auth": "' + auth + '"           
       }'
    

    You don't need to specify the wildcard, it's default. Also, you don't need to put the % inside your query.