经过更多研究后编辑。
事件的参数。get应用于
event object
仅限:您可以根据value、acknowleged、hostids、groupid等进行筛选,但不能使用它按主机名筛选输出。
您可以使用hostids参数(请参阅
API
),但必须先调用API才能将目标主机名转换为主机ID。
或者你可以使用
selectHosts = 'extend'
仅限,它将返回一个事件和主机列表,其中包含一个时间段的完整详细信息,然后迭代结果并根据您的条件进行筛选。
第一个需要更多的API调用,但我认为它更优雅。第二个将返回特定时间范围内所有主机的所有事件,然后必须过滤掉所有不需要的事件。
带有HostID筛选的Python示例:
hostId = zapi.get_id('host', item="TEST host name")
eventObj = zapi.event.get(time_from=1515771918, hostids=hostId, value="1", selectHosts='extend')
for event in eventObj:
for host in event['hosts']:
# filter by host['description'] or any other host value
不带HostID筛选的Python示例:
eventObj = zapi.event.get(time_from=1515771918, value="1", selectHosts='extend')
for event in eventObj:
for host in event['hosts']:
# filter by host['name'] or host['description'] or any other host value
在这两种情况下,extend输出将为每个事件提供完整的主机信息:
[
{
"acknowledged": "0",
"c_eventid": "0",
"clock": "1515773211",
"correlationid": "0",
"eventid": "2738610",
"hosts": [
{
"available": "0",
"description": "Host description",
"disable_until": "0",
"error": "",
"errors_from": "0",
"flags": "0",
"host": "192.168.1.1",
"hostid": "10283",
"ipmi_authtype": "-1",
"ipmi_available": "0",
"ipmi_disable_until": "0",
"ipmi_error": "",
"ipmi_errors_from": "0",
"ipmi_password": "",
"ipmi_privilege": "2",
"ipmi_username": "",
"jmx_available": "0",
"jmx_disable_until": "0",
"jmx_error": "",
"jmx_errors_from": "0",
"lastaccess": "0",
"maintenance_from": "0",
"maintenance_status": "0",
"maintenance_type": "0",
"maintenanceid": "0",
"name": "Your device name or hostname",
"proxy_hostid": "0",
"snmp_available": "1",
"snmp_disable_until": "0",
"snmp_error": "",
"snmp_errors_from": "0",
"status": "0",
"templateid": "0",
"tls_accept": "1",
"tls_connect": "1",
"tls_issuer": "",
"tls_psk": "",
"tls_psk_identity": "",
"tls_subject": ""
}
],
"ns": "259800604",
"object": "0",
"objectid": "15177",
"r_eventid": "2738613",
"source": "0",
"userid": "0",
"value": "1"
},
-- other events --
]
您可以使用selectHosts来限制通过使用属性数组代替“extend”检索的值:
eventObj = zapi.event.get(time_from=1515771918, hostids=hostId, value="1", selectHosts=['description', 'status', 'host'])
此请求将返回以下主机格式的事件:
{
"acknowledged": "0",
"c_eventid": "0",
"clock": "1516502139",
"correlationid": "0",
"eventid": "2768212",
"hosts": [
{
"description": "Test server for API experiments",
"host": "Test Server",
"hostid": "10270",
"status": "0"
}
],
"ns": "536030065",
"object": "0",
"objectid": "14920",
"r_eventid": "0",
"source": "0",
"userid": "0",
"value": "1"
},