Python 具有zabbix api值历史记录的输出
我试图用zabbixPython 具有zabbix api值历史记录的输出,python,zabbix,Python,Zabbix,我试图用zabbixhostid->itemid->historyAPI捕获以下序列,但它没有向我返回任何内容。我需要这个脚本来返回最后的值由ZABBIX收集,包括项目id+主机名 脚本 from zabbix.api import ZabbixAPI from datetime import datetime import time zapi = ZabbixAPI(url='http://192.168.1.250/zabbix', user='Admin', password='za
hostid->itemid->history
API捕获以下序列,但它没有向我返回任何内容。我需要这个脚本来返回最后的值由ZABBIX收集,包括项目id+主机名
脚本
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'
输出
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'
没有什么回报
所需输出
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'
您的代码有一些问题(静态
itemid
,在历史记录中缺少参数。get
等),我将尝试总结所有问题
您正在按静态主机组id进行筛选,因此我假设您有多个主机,并且您需要每个主机的特定项的值,例如:
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
- 主机组: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']
你的答案很完美,只是一个问题,我如何才能正确地从Timestamp和tillTimestamp捕获变量,而不必将其转换为INT?你必须将它们转换为INT,历史记录。get调用只支持时间戳。我使用这段代码:
fromTimestamp=int(time.mktime(datetime.datetime.strtime(args.f,“%d/%m/%Y%H:%m”).timetuple())
将日期转换为相应的时间戳。如何在我的代码中部署它?返回的参数errorSubstituteargs.f
,带有与strtime
调用相应格式的日期字符串,请参阅