Ad Widget

Collapse

Can't figure out error in Pyzabbix script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Molusco
    Junior Member
    • Dec 2019
    • 20

    #1

    Can't figure out error in Pyzabbix script

    Hi, Im tying this script that appears in the book "Mastering Zabbix" but it trows me an error "KeyError: 'items'" in the for loop close to the end of the script, I Can't figure out what is wrong, can you help me understand? thanks.

    #!/usr/bin/python
    from pyzabbix import ZabbixAPI
    import sys
    import csv
    from datetime import datetime
    appname = sys.argv[1]
    timeformat="%d/%m/%y %H:%M"

    zh = ZabbixAPI("http://localhost/zabbix")
    zh.login(user="admin", password="zabbix")
    applications = zh.application.get(output="shorten", filter={"name":
    [appname]})

    items = zh.item.get(output="count", applicationids=[x['applicationid']
    for x in applications])

    triggers = zh.trigger.get(output="refer", itemids=[x['itemid'] for x
    in items])

    events = zh.event.get(triggerids=[j['triggerid'] for j in triggers])

    def gethostname(hostid=''):
    return zh.host.get(hostids=hostid, output=['host'])[0]['host']
    def getitemname(itemid=''):
    return zh.item.get(itemids=itemid, output=['name'])[0]['name']
    def gettriggername(triggerid=''):
    return zh.trigger.get(triggerids=triggerid,expandDescript ion="1", output=['description'])[0]['description']

    eventstable = []
    triggervalues = ['OK', 'problem', 'unknown']

    for e in events:
    eventid = e['eventid']
    event = zh.event.get(eventids=eventid,
    selectHosts="refer",
    selectItems="refer",
    selectTriggers="refer",
    output="extend")
    host = gethostname(event[0]['hosts'][0]['hostid'])
    item = getitemname(event[0]['items'][0]['itemid']) <-----------------------------------------KeyError: 'items'
    trigger = gettriggername(event[0]['triggers'][0]['triggerid'])
    clock = datetime.fromtimestamp(int(event[0]['clock'])).strftime(timeformat)
    value = triggervalues[int(event[0]['value'])]
    eventstable.append({"Host": host,
    "Item": item,
    "Trigger": trigger,
    "Status": value,
    "Time" : clock
    })
    print(eventstable)
  • 1berto
    Senior Member
    • Sep 2018
    • 182

    #2
    Probably there is no events...
    What do you have if ...
    print( events )
    Before the for ?

    Comment

    Working...