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

2024-06-28 18:51:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚开始使用Zabbix API。我使用Zabbix API在python脚本中获取监控数据,在此期间,我无法在任何历史记录中找到某些ItemId的值。任何使用过Zabbix API的人都可以告诉我解决方案。我还可以在哪里找到ItemId的值,我已经在History\u uint、History\u string、History\u 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']))

Tags: fromidoutputgetiftimenotitem
1条回答
网友
1楼 · 发布于 2024-06-28 18:51:48

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

尝试删除time_fromtime_till子句,并从中进行调试

相关问题 更多 >