ZabbixAPI,从每个主机中的特定字段检索信息
ZabbixAPI, retrieving information from a particular field within each host
我想从特定主机组中的所有主机检索留给特定 diskX: 的磁盘百分比 space。
我尝试使用 item.get() 函数,但返回了一个空列表。
zapi =ZabbixApi(server)
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 的经验来告诉我我做错了什么?
谢谢:)
您正在尝试使用 history.get()
获取完整的历史记录(没有时间限制)- 这可能是很多数据点,需要由 API 进行预处理。这确实不是一个好主意,因为您可以达到一些 PHP/API 限制 - 时间或内存 - 这就是您当前的情况。
使用time_from/time_till
参数限制history.get()
的时间范围。
请参阅文档:https://www.zabbix.com/documentation/3.4/manual/api/reference/history/get
根据有关请求的更多详细信息进行编辑,请参阅评论。
为避免 php 超时,您应该按照 Jan 的建议拆分请求并使用 time_from/time_till。
当使用发现的项目时,通过 APIs 获得的项目名称将不会扩展宏,有一个关于它的功能请求。
例如,如果您使用 Windows 文件系统发现并且您的服务器有 C: 和 D: 驱动器,在 Zabbix 中您将有两个同名的项目 ("Free disk space on (percentage)"
),而发现的驱动器将在每个项目的 key_
字段中,例如:
vfs.fs.size[C:,pfree]
vfs.fs.size[D:,pfree]
因此,您必须调用项目 get API 过滤通用名称(</code>),然后仅当 <code>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 (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 (percentage)(vfs.fs.size[C:,pfree]) - 1538128853 28/09/2018 12:00:53 Value: 63.3960
SRV01:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128914 28/09/2018 12:01:54 Value: 63.3960
SRV01:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128974 28/09/2018 12:02:54 Value: 63.3960
SRV01:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129034 28/09/2018 12:03:54 Value: 63.3960
SRV01:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129094 28/09/2018 12:04:54 Value: 63.3960
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128824 28/09/2018 12:00:24 Value: 52.2341
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128885 28/09/2018 12:01:25 Value: 52.2341
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128944 28/09/2018 12:02:24 Value: 52.2341
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129004 28/09/2018 12:03:24 Value: 52.2341
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129065 28/09/2018 12:04:25 Value: 52.2341
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128828 28/09/2018 12:00:28 Value: 33.2409
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128888 28/09/2018 12:01:28 Value: 33.2409
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128947 28/09/2018 12:02:27 Value: 33.2409
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129008 28/09/2018 12:03:28 Value: 33.2409
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129069 28/09/2018 12:04:29 Value: 33.2409
我想从特定主机组中的所有主机检索留给特定 diskX: 的磁盘百分比 space。 我尝试使用 item.get() 函数,但返回了一个空列表。
zapi =ZabbixApi(server)
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 的经验来告诉我我做错了什么?
谢谢:)
您正在尝试使用 history.get()
获取完整的历史记录(没有时间限制)- 这可能是很多数据点,需要由 API 进行预处理。这确实不是一个好主意,因为您可以达到一些 PHP/API 限制 - 时间或内存 - 这就是您当前的情况。
使用time_from/time_till
参数限制history.get()
的时间范围。
请参阅文档:https://www.zabbix.com/documentation/3.4/manual/api/reference/history/get
根据有关请求的更多详细信息进行编辑,请参阅评论。
为避免 php 超时,您应该按照 Jan 的建议拆分请求并使用 time_from/time_till。
当使用发现的项目时,通过 APIs 获得的项目名称将不会扩展宏,有一个关于它的功能请求。
例如,如果您使用 Windows 文件系统发现并且您的服务器有 C: 和 D: 驱动器,在 Zabbix 中您将有两个同名的项目 ("Free disk space on (percentage)"
),而发现的驱动器将在每个项目的 key_
字段中,例如:
vfs.fs.size[C:,pfree]
vfs.fs.size[D:,pfree]
因此,您必须调用项目 get API 过滤通用名称(</code>),然后仅当 <code>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 (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 (percentage)(vfs.fs.size[C:,pfree]) - 1538128853 28/09/2018 12:00:53 Value: 63.3960
SRV01:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128914 28/09/2018 12:01:54 Value: 63.3960
SRV01:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128974 28/09/2018 12:02:54 Value: 63.3960
SRV01:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129034 28/09/2018 12:03:54 Value: 63.3960
SRV01:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129094 28/09/2018 12:04:54 Value: 63.3960
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128824 28/09/2018 12:00:24 Value: 52.2341
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128885 28/09/2018 12:01:25 Value: 52.2341
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128944 28/09/2018 12:02:24 Value: 52.2341
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129004 28/09/2018 12:03:24 Value: 52.2341
SRV02:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129065 28/09/2018 12:04:25 Value: 52.2341
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128828 28/09/2018 12:00:28 Value: 33.2409
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128888 28/09/2018 12:01:28 Value: 33.2409
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538128947 28/09/2018 12:02:27 Value: 33.2409
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129008 28/09/2018 12:03:28 Value: 33.2409
SRV03:Free disk space on (percentage)(vfs.fs.size[C:,pfree]) - 1538129069 28/09/2018 12:04:29 Value: 33.2409