Search code examples
pythonzabbixzabbix-api

Get problem description in Python and Zabbix API


I use trigger.get method to get all problems in my Zabbix monitoring:

trigger = zapi.trigger.get (triggerids=problem['objectid'], selectHosts='extend')

and it works propertly. But I get response for example:

'description': '{HOST.NAME} ( {ITEM.VALUE1} ) Lack of free swap space'

or

'description': 'DBM {HOST.NAME} ORA_ERR_DETAILS: {ITEM.VALUE1}'

and now I would like to get value "ITEM.VALUE1"

What method should he use and how?

In a more descriptive way:

My program return value:

'{HOST.NAME} ( {ITEM.VALUE1} ) Lack of free swap space' 

I can get the '{HOST.NAME}', but I can't get the '{ITEM.VALUE1}'.

I would like to have:

Host005.domain.com ( 42.52 % ) Lack of free swap space

Solution

  • Thanks to the hint from Simone, the problem is resolved!

    A well-functioning code:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    """
    Get history values for specific items in a time range:
    
    # ./getItemHistoryByName.py -H some-host  -I "ICMP response time" -f "26/6/2018 16:00" -t "27/6/2018 23:59"
    ItemID: 77013 - Item: ICMP response time - Key: icmppingsec
    1530021641 26/06/2018 16:00:41 Value: 0.1042
    1530021701 26/06/2018 16:01:41 Value: 0.0993
    1530021762 26/06/2018 16:02:42 Value: 0.1024
    1530021822 26/06/2018 16:03:42 Value: 0.0966
    [cut]
    """
    
    from zabbix.api import ZabbixAPI
    import sys, argparse
    import time
    import datetime
    
    
    zabbixServer    = 'http://yourserver/zabbix/'
    zabbixUser      = 'someuser'
    zabbixPass      = 'somepass'
    
    
    def main(argv):
        parser = argparse.ArgumentParser()
        parser.add_argument('-H', required=True, metavar='Hostname')
        parser.add_argument('-I', required=True, metavar='Item Name')
        parser.add_argument('-f', required=True, metavar='From Timestamp')
        parser.add_argument('-t', required=True, metavar='Till Timestamp')
    
        args = parser.parse_args()
    
    
        zapi = ZabbixAPI(url=zabbixServer, user=zabbixUser, password=zabbixPass)
    
        fromTimestamp = time.mktime(datetime.datetime.strptime(args.f, "%d/%m/%Y %H:%M").timetuple())
        tillTimestamp = time.mktime(datetime.datetime.strptime(args.t, "%d/%m/%Y %H:%M").timetuple())
    
    
        f  = {  'name' : args.I  }
        items = zapi.item.get(filter=f, host=args.H, output='extend' )
    
        for item in items:
            print "ItemID: {} - Item: {} - Key: {}".format(item['itemid'], item['name'], item['key_'])
    
            values = zapi.history.get(itemids=item['itemid'], time_from=fromTimestamp, time_till=tillTimestamp, history=item['value_type'])
    
            for historyValue in values:
                currentDate = datetime.datetime.fromtimestamp(int(historyValue['clock'])).strftime('%d/%m/%Y %H:%M:%S')
    
                print "{} {} Value: {}".format(historyValue['clock'], currentDate, historyValue['value'])
    
    if __name__ == "__main__":
       main(sys.argv[1:])