(Zabbix API)(Python 脚本)无法在任何历史记录中找到某些 itemid 的值(history_uint、history_string、...等)

(Zabbix API)(Python Script) Can't find the value for some itemid in any of the history(history_uint, history_string, ...etc)

我刚刚开始使用 Zabbix API。我正在使用 Zabbix API 在 python 脚本中获取监控数据,在此期间我无法在任何历史记录中找到某些 itemid 的值。任何使用过 Zabbix API 的人都可以告诉我解决方案。我还能在哪里找到 ItemId 的值,我已经搜索过但在 History_uint、history_string、history_text 等

中找不到

这是我的代码:

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']))

你不应该直接连接到数据库:你可以检索

尝试删除 time_fromtime_till 子句,然后从那里进行调试。