我试图用hostid -> itemid -> history API捕获以下序列,但它没有向我返回任何内容。我需要这个脚本来返回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'])输出
什么都没有回报我
期望输出
'host','28689','84'
'host','28689','82'
'host','28689','85'
'host','28689','83'发布于 2019-10-29 14:05:35
您的代码中有一些问题(静态itemid、history.get中缺少的参数等等),我将尝试总结所有内容。
您是通过静态主机组id进行筛选的,因此我假设您有多个主机,并且需要每个主机的一个特定项的值,如下所示:
输出应该类似于:
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实现:
groupFilter = {'name': 'MyHostGroup'}
itemFilter = {'name': 'ICMP Loss'}
# Get the hostgroup id by its name
hostgroups = zapi.hostgroup.get(filter=groupFilter, output=['groupids', 'name'])
# Get the hosts of the hostgroup by hostgroup id
hosts = zapi.host.get(groupids=hostgroups[0]['groupid'])
for host in (hosts):
# Get the item info (not the values!) by item name AND host id
items = zapi.item.get(filter=itemFilter, host=host['host'], output='extend', selectHosts=['host', 'name'])
# for loop - for future fuzzy search, otherwise don't loop and use items[0]
for item in items:
# Get item values
values = zapi.history.get(itemids=item['itemid'], time_from=fromTimestamp, time_till=tillTimestamp, history=item['value_type'])
for historyValue in values:
print( ......... ) # format here your output, values are stored in historyValue['value']https://stackoverflow.com/questions/58600238
复制相似问题