带有zabbixapi值历史记录的输出

2024-06-28 19:12:47 发布

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

我试图用zabbixhostid -> itemid -> historyAPI捕捉以下序列,但它没有返回任何内容。我需要这个脚本来返回ZABBIX收集的最后一个值,包括item id+hostname

脚本

from zabbix.api import ZabbixAPI
from datetime import datetime
import time

zapi = ZabbixAPI(url='http://192.168.1.250/zabbix', user='Admin', password='zabbix')

fromTimestamp = int(time.mktime(datetime.now().timetuple()))
tillTimestamp = int(fromTimestamp - 60 * 60 * 1)  # 1 hours

# Get only the host of the specified hostgroup
hosts =  zapi.host.get(groupids='15',output='extend')

for host in hosts:
    items = zapi.item.get(itemid='28689', host=host['host'], output='extend' )
    for item in items:
        values = zapi.history.get(itemids=item['itemid'], time_from=fromTimestamp, time_till=tillTimestamp, output='extend')        
        for historyValue in values:
            print(host['host'],item['itemid'],historyValue['value'])

输出

什么也不能回报我

期望输出

^{pr2}$

Tags: infromimporthostforoutputgetdatetime
1条回答
网友
1楼 · 发布于 2024-06-28 19:12:47

您的代码中有一些问题(静态itemid,在history.get中缺少参数等等),我将尝试总结所有问题。在

您正在按静态主机组id进行筛选,因此我假设您有多个主机,并且您需要每个主机的特定项的值,如下所示:

  • 主机组:MyHostGroup
  • 成员:host01、host02、host03
  • 利息项目:ICMP损失

输出应该类似于:

Timestamp  Hostname   ItemID   ICMP Loss
xxxxxx1    host01     10011    0
xxxxxx2    host01     10011    10
xxxxxx3    host01     10011    10
xxxxxx4    host01     10011    15

xxxxxx1    host02     10026    100
xxxxxx2    host02     10026    100
xxxxxx3    host02     10026    100
xxxxxx4    host02     10026    100

xxxxxx1    host03     10088    0
xxxxxx2    host03     10088    10
xxxxxx3    host03     10088    0
xxxxxx4    host03     10088    0

一个工作的python实现:

^{pr2}$

相关问题 更多 >