输出带zabbix-api值的历史记录

Output with zabbix-api value history

我正在尝试使用 zabbix hostid -> itemid -> history API 捕获以下序列,但它没有 return 对我产生任何影响。我需要这个脚本来return 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'])

输出

没什么return我

期望输出

'host','28689','84'
'host','28689','82'
'host','28689','85'
'host','28689','83'

您的代码存在一些问题(静态 itemidhistory.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 实现:

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