0

I'm having trouble creating the output from Zabbix API tables. I would like to output NAME(hosts), icmpping, icmploss for example.

So I would like something like this:

hostname ip ping loss status
test.server.1 1.1.1.1 0ms 0% up
test.server.2 8.8.8.8 1ms 33% up

For now I have this: From:

Code example

1

Thanks for you help

vimuth
  • 5,064
  • 33
  • 79
  • 116

1 Answers1

0

The item.get api returns the item's definition (item id, type, key name, interval etc), not the values.

You need to call item.get to get the item id you need, then use the id for a history.get call

Try this script I wrote a while ago:

#!/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:])
Simone Zabberoni
  • 2,024
  • 1
  • 10
  • 15
  • didnt manage it to work `❯ python3 item.get4.py -H "zabbix-agent" -I "Zabbix agent ping" -f "26/6/2022 16:00" -t "27/6/2022 23:59" Traceback (most recent call last): File "/home/samukas/Projetos/api-zabbix/item.get4.py", line 68, in main(sys.argv[1:]) File "/home/samukas/Projetos/api-zabbix/item.get4.py", line 50, in main items = zapi.item.get(filter=f, host=args.H, output='extend') TypeError: ZabbixAPISubClass.__getattr__..method() got an unexpected keyword argument 'filter'` – Samuka Sampaio Jul 26 '22 at 10:11