I have just started using Zabbix API. I'm using Zabbix API to fetch monitored data in a python script, during which I can't find the value for some itemids in any of the history records. Anyone who has worked with Zabbix API could please tell me the solution. Where else could I find the value of the ItemId, which I already searched and can't find in History_uint, history_string, history_text,etc.

Here is my code:

from pyzabbix import ZabbixAPI
import mysql.connector as sql
from datetime import datetime
import time

ZABBIX_SERVER = 'http:XXXXXXXXXXXXXXx'
zapi = ZabbixAPI(ZABBIX_SERVER)
zapi.login('hgfjkj', 'mgmkvc')
con = sql.connect(user='XXXXXXXX', password='XXXXXXX',
                  host='XXXXXXXXX', database='XXXXXx')
cursor = con.cursor()

a = input("enter host name : ")

cursor.execute(f"select * from hosts WHERE name='" + a + "'")
result1 = cursor.fetchall()

print(f"hostid = " + str(result1[0][0]))

b = input("enter item name : ")
cursor.execute(f"select * from items where name='" + b + "' and hostid=" + str(result1[0][0]))
result2 = cursor.fetchall()

print("itemid =" + str(result2[0][0]))
item_id = result2[0][0]

# Create a time range
time_till = int(time.mktime(datetime.now().timetuple()))
time_from = int(time_till - 60 * 60 * 4)  # 4 hours

# Query item's history (integer) data
history = zapi.history.get(itemids=[item_id],
                           time_from=time_from,
                           time_till=time_till,
                           output='extend',
                           limit='10',
                           )

# If nothing was found, try getting it from history (float) data
if not len(history):

    history = zapi.history.get(itemids=[item_id],
                               time_from=time_from,
                               time_till=time_till,
                               output='extend',
                               limit='10',
                               history=0,
                               )
if not len(history):
    history = zapi.history.get(itemids=[item_id],
                               time_from=time_from,
                               time_till=time_till,
                               output='extend',
                               limit='10',
                               history=1,
                               )
if not len(history):
    history = zapi.history.get(itemids=[item_id],
                               time_from=time_from,
                               time_till=time_till,
                               output='extend',
                               limit='10',
                               history=2,
                               )
if not len(history):
    history = zapi.history.get(itemids=[item_id],
                               time_from=time_from,
                               time_till=time_till,
                               output='extend',
                               limit='10',
                               history=4,
                               )
if not len(history):
    print("no history found")
# Print out each datapoint
for point in history:
    print("{0}: {1}".format(datetime.fromtimestamp(int(point['clock']))
                            .strftime("%x %X"), point['value']))
1

There are 1 best solutions below

0
On BEST ANSWER

You shouldn't connect to the database directly: you can retrieve

  • the host ID with host.get
  • the trigger/s ID, and the latest value, with item.get.

Try removing time_from and time_till clauses, and debug from there.