首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有zabbix-api值历史记录的输出

具有zabbix-api值历史记录的输出
EN

Stack Overflow用户
提问于 2019-10-29 01:05:01
回答 1查看 1.4K关注 0票数 0

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

脚本

代码语言:javascript
复制
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'])

输出

什么都没有回报我

期望输出

代码语言:javascript
复制
'host','28689','84'
'host','28689','82'
'host','28689','85'
'host','28689','83'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-29 14:05:35

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

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

  • 主机组: MyHostGroup
  • 成员: host01,host02,host03
  • 项目感兴趣: ICMP Loss

输出应该类似于:

代码语言:javascript
复制
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实现:

代码语言:javascript
复制
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']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58600238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档