ZabbixAPI,从每个主机内的特定字段中检索信息

时间:2018-09-27 19:34:19

标签: zabbix

我想从特定主机组内的所有主机检索特定diskX剩余的磁盘空间百分比。 我尝试使用item.get()函数,但是返回了一个空列表。

  

zapi = ZabbixApi(服务器)

for t in zapi.item.get(groups = 'Type1',filter = {'name': 'Free Disk Space on X'},)

使用item.get方法的此^^方法。 给我一个空名单

我尝试使用history.get方法,但是会导致超时

for t in groups:
  t2 += zapi.history.get(filter = {'name':'free Disk Space on E:(percentage)'},)

任何人都对Zabbix Api有任何经验,可以向我建议我做错了什么?

谢谢:)

2 个答案:

答案 0 :(得分:0)

您正在尝试使用history.get()获取完整的历史记录(无时间限制)-可能是很多数据点,需要通过API进行预处理。那真的不是一个好主意,因为您可以达到一些PHP / API限制(时间或内存),这就是您当前的情况。

使用time_from/time_till参数来限制history.get()的时间范围。 参见文档:https://www.zabbix.com/documentation/3.4/manual/api/reference/history/get

答案 1 :(得分:0)

有关请求的更多详细信息后进行了编辑,请参见注释。

为避免php超时,您应该拆分请求,并按照Jan的建议使用time_from / time_till。

使用发现的物品时,通过API获得的物品名称不会扩展宏,对此有功能要求。

例如,如果您使用Windows文件系统发现,并且服务器具有C:和D:驱动器,则在Zabbix中,您将拥有两个具有相同名称("Free disk space on $1 (percentage)")的项目,而发现的驱动器将位于每个项目的key_字段,例如:

vfs.fs.size[C:,pfree]
vfs.fs.size[D:,pfree]

因此,您将必须调用该项目以获取通用名称($1)的API过滤,然后仅在key_包含目标驱动器名称的情况下获取历史记录值

我用主机组过滤器和更多详细的变量和输出更新了示例脚本:编辑任何不需要的字段以简化所需的输出。

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

zapi = ZabbixAPI(url=zabbixServer, user=zabbixUser, password=zabbixPass)

# Static filters, implement argparse if needed
itemFilter = {  "name" : "Free disk space on $1 (percentage)"  }
hostgroupFilter = { "name": "Some HostGroup" }
keyFilter = "C\:"

# args.f and args.t supplied from cmd line - see argparse
fromTimestamp = time.mktime(datetime.datetime.strptime(args.f, "%d/%m/%Y %H:%M").timetuple())
tillTimestamp = time.mktime(datetime.datetime.strptime(args.t, "%d/%m/%Y %H:%M").timetuple())


# Get only the host of the specified hostgroup
hostGroup = zapi.hostgroup.get(filter=hostgroupFilter,output='extend')
hosts =  zapi.host.get(groupids=hostGroup[0]['groupid'],output='extend')

for host in hosts:
    items = zapi.item.get(filter=itemFilter, host=host['host'], output='extend' )

    for item in items:
        # Check if the item key contains the target object (in your example, if in contains C:)
        if re.search(keyFilter, item['key_']):

            values = zapi.history.get(itemids=item['itemid'], time_from=fromTimestamp, time_till=tillTimestamp, history=item['value_type'])
            for historyValue in values:
                currentDate = datetime.datetime.fromtimestamp(int(historyValue['clock'])).strftime('%d/%m/%Y %H:%M:%S')
                print "{}:{}({}) - {} {} Value: {}".format(
                  host['host'],
                  item['name'],
                  item['key_'],
                  historyValue['clock'],
                  currentDate, historyValue['value'])

5分钟的样本输出,带有3个Windows服务器的主机组

SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128853 28/09/2018 12:00:53 Value: 63.3960
SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128914 28/09/2018 12:01:54 Value: 63.3960
SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128974 28/09/2018 12:02:54 Value: 63.3960
SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129034 28/09/2018 12:03:54 Value: 63.3960
SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129094 28/09/2018 12:04:54 Value: 63.3960
SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128824 28/09/2018 12:00:24 Value: 52.2341
SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128885 28/09/2018 12:01:25 Value: 52.2341
SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128944 28/09/2018 12:02:24 Value: 52.2341
SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129004 28/09/2018 12:03:24 Value: 52.2341
SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129065 28/09/2018 12:04:25 Value: 52.2341
SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128828 28/09/2018 12:00:28 Value: 33.2409
SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128888 28/09/2018 12:01:28 Value: 33.2409
SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128947 28/09/2018 12:02:27 Value: 33.2409
SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129008 28/09/2018 12:03:28 Value: 33.2409
SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129069 28/09/2018 12:04:29 Value: 33.2409
相关问题