2

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
Karol
  • 127
  • 1
  • 10

2 Answers2

0

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:])
Karol
  • 127
  • 1
  • 10
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) –  Aug 07 '20 at 11:56
-1

You can just specify the field you need like this

answer = z.do_request('apiinfo.version')

Reply In JSON:

{u'jsonrpc': u'2.0', u'result': u'3.0.2', u'id': u'1'}

Reply by python script:

print "Version:",answer['result']
Version: 3.0.2
Andrew 76868
  • 394
  • 1
  • 4
  • 14
  • But what method should he use to have a value ITEM.VALUE1? – Karol Aug 03 '20 at 11:25
  • 1
    @Karol you don't have to specify any method except print. Just print(answer['result']) – Andrew 76868 Aug 03 '20 at 11:29
  • I don't think we understand each other. 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 – Karol Aug 03 '20 at 12:15
  • @Karol did you find solution? – pharask Aug 05 '20 at 06:35
  • I implement the method history.get. I'm still working on it – Karol Aug 05 '20 at 12:07