【问题标题】:Output with zabbix-api value history输出 zabbix-api 值历史
【发布时间】:2019-10-29 01:05:01
【问题描述】:

我正在尝试使用 zabbix 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'

【问题讨论】:

    标签: python zabbix


    【解决方案1】:

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

    您正在按静态主机组 ID 进行过滤,因此我假设您有多个主机,并且您想要每个主机的特定项目的值,例如:

    • 主机组:我的主机组
    • 成员: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']
    

    【讨论】:

    • 你的答案很完美,只是一个问题,我怎样才能正确地捕获来自Timestamp 和tillTimestamp 的变量,而不必将其转换为INT?
    • 必须将它们转换为 INT,history.get 调用仅支持时间戳。我使用这段代码:fromTimestamp = int(time.mktime(datetime.datetime.strptime(args.f, "%d/%m/%Y %H:%M").timetuple())) 将日期转换为对应的时间戳。
    • repl.it/repls/ChillyMiniatureCable 请如何在我的代码中部署它?返回参数错误
    • 用与strptime调用相应的日期字符串替换args.f,参见repl.it/repls/DefiantLovelyFolder
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2018-05-23
    相关资源
    最近更新 更多