pythonzabbixzabbix-api

How to ask Zabbix about problem description?


I have to display all current problems in my infrastructure (like in Zabbix dashboard). I would like it to look like this:

Date     Host          Problem info    
19.03    hostsap1      Lack of free swap space
18.03    hostsmb2      Zabbix_agentd is not running!

I use problem.get

problemlist = zapi.do_request('problem.get',
                                  {
                                     "output": "extend", 
                                      "selectAcknowledges": "extend", 
                                      "recent": "true", 
                                      "sortfield": ["eventid"],
                                      "sortorder": "DESC" 
                                  })

and I have the answer:

{
   'eventid': '25644', 
   'source': '0', 
   'object': '0', 
   'objectid': '147717', 
   'clock': '2447665140', 
   'ns': '193586738', 
   'r_eventid': '0', 
   'r_clock': '0', 
   'r_ns': '0', 
   'correlationid': '0', 
   'userid': '0', 
   'acknowledges': []
}, 
[...]

How to ask zabbix about host name and most importantly about problem description like "Lack of free swap space"?


Solution

  • This snippet should do the trick:

    zapi = ZabbixAPI(url=zabbixServer, user=zabbixUser, password=zabbixPass)
    
    problems = zapi.problem.get()
    
    for problem in problems:
        trigger = zapi.trigger.get (triggerids=problem['objectid'], selectHosts='extend')
        interface = zapi.hostinterface.get(hostids=trigger[0]['hosts'][0]['hostid'])
        group = zapi.hostgroup.get(hostids=trigger[0]['hosts'][0]['hostid'])
    
        enabled = "Enabled"
        if (trigger[0]['hosts'][0]['status'] == "1"):
            enabled = "Disabled"
    
        print "Group:{}; Host:{}; IP:{}; Problem:{}; {}".format(group[1]['name'],
                                                               trigger[0]['hosts'][0]['host'],
                                                               interface[0]['ip'],
                                                               trigger[0]['description'],
                                                               enabled )
    

    Of course, you can omit the group and hostinterface api call if you don't need them