0

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"?

Karol
  • 127
  • 1
  • 10

1 Answers1

0

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

Simone Zabberoni
  • 2,024
  • 1
  • 10
  • 15
  • Thank You so much. It good looking, but I have a "IndexError: list index out of range". I'm working about these problem. – Karol Mar 20 '20 at 11:39
  • This script is just a snip, there is no error checking. Index error probaly refers to one of the API calls returning an empty result. Try to remove the interface and host stuff, also some debug prints should help your debug – Simone Zabberoni Mar 20 '20 at 12:46
  • I'm not done yet, but I see it goes well. I added a condition: if not len(trigger) == 0: and I haven't errors like list index out of range – Karol Mar 20 '20 at 13:46
  • Thank You So much! That's exactly what I meant. I add condition "if len(trigger) != 0:" and it wark's :) – Karol Mar 20 '20 at 14:22