解析Zabbix输出格式

时间:2016-06-22 14:19:51

标签: python

我正在使用pyzabbix与Zabbix交互并检索我想要的内容:

#!/usr/bin/env python

# import
import sys, subprocess, time, datetime, csv, platform
from pyzabbix import ZabbixAPI

# api/vars
url='http://172.18.64.100/zabbix'
zh = ZabbixAPI(url)
zh.login(user="Admin",password="xxxxxxxx")
groupid = sys.argv[1]

# get group from argv
group = zh.hostgroup.get(output="shorten", filter={"groupid": [groupid]}, monitored_hosts="1")

# get hosts from looping group above
hosts = zh.host.get(output="count", monitored_hosts="1", groupids=[x['groupid'] for x in group])

# print
for hostid in hosts:
    print hostid
    triggers = zh.trigger.get(filter={"hostid": hostid}, active="1", output=['description'])
    print triggers

所以,输出工作正常,但我需要过滤它,我正在努力:

{u'hostid': u'10166'}
[{u'triggerid': u'14138', u'description': u'Servidor Reiniciado'}, {u'triggerid': u'14139', u'description': u' Pin
 u'18082', u'description': u'Interface 7A'}, {u'triggerid': u'18083', u'description': u'Interface 7B'}, {u'trigger
scription': u'Interface 8A'}, {u'triggerid': u'18085', u'description': u'Interface 8B'}, {u'triggerid': u'18086',
nterface 9A'}, {u'triggerid': u'18087', u'description': u'Interface 9B'}, {u'triggerid': u'14140', u'description':
 {u'triggerid': u'14141', u'description': u'CPU 90% utilizada'}, {u'triggerid': u'14143', u'description': u' Sem c
SNMP por 30 minutos'}, {u'triggerid': u'23138', u'description': u'Servidor Reiniciado'}]
{u'hostid': u'10412'}
[{u'triggerid': u'22084', u'description': u'Agente Zabbix inoperante'}, {u'triggerid': u'22086', u'description': u
r Database (servi\xe7o_windows - McAfeeNetworkSecurityManagerDatabase)'}, {u'triggerid': u'22088', u'description':
ork Security Manager (servi\xe7o_windows - NetworkSecurityManager)'}, {u'triggerid': u'22090', u'description': u'C
, {u'triggerid': u'22091', u'description': u'Servidor Reiniciado'}, {u'triggerid': u'22092', u'description': u'Ser
 {u'triggerid': u'22122', u'description': u'Espa\xe7o em disco menor que 5% em C:'}, {u'triggerid': u'22124', u'de
xe7o em disco menor que 10% em C:'}, {u'triggerid': u'22123', u'description': u'Espa\xe7o em disco menor que 5% em
': u'22125', u'description': u'Espa\xe7o em disco menor que 10% em D:'}, {u'triggerid': u'22094', u'description':
\xedvel menor que 10%'}]
{u'hostid': u'10413'}
[{u'triggerid': u'22102', u'description': u'Agente Zabbix inoperante'}, {u'triggerid': u'22104', u'description': u
r Database (servi\xe7o_windows - McAfeeNetworkSecurityManagerDatabase)'}, {u'triggerid': u'22106', u'description':
ork Security Manager (servi\xe7o_windows - NetworkSecurityManager)'}, {u'triggerid': u'22108', u'description': u'C
, {u'triggerid': u'22109', u'description': u'Servidor Reiniciado'}, {u'triggerid': u'22110', u'description': u'Ser
 {u'triggerid': u'22132', u'description': u'Espa\xe7o em disco menor que 5% em C:'}, {u'triggerid': u'22134', u'de
xe7o em disco menor que 10% em C:'}, {u'triggerid': u'22133', u'description': u'Espa\xe7o em disco menor que 5% em
': u'22135', u'description': u'Espa\xe7o em disco menor que 10% em D:'}, {u'triggerid': u'22112', u'description':
\xedvel menor que 10%'}]

我只想要" hostid"以及"描述"输出的一部分,我的意思是:

hostid - 10166
Servidor Reiniciado
Ping
Interface 7A
Interface 7B
...

hostid - 10412
Agente Zabbix inoperante
...
...

1 个答案:

答案 0 :(得分:0)

谢谢,我再次循环 我知道它的丑陋,但好吧,现在它很好:)

我在#print part:

中添加了另一个循环
for hostid in hosts:
   # triggers = zh.trigger.get(hostids=[x['hostid'] for x in hosts], active="1", selectHosts="1")
    print hostid
    triggers = zh.trigger.get(filter={"hostid": hostid}, active="1", output=['description'])
    for description in triggers:
        print description

我得到了我想要的东西:

{u'hostid': u'10166'}
{u'triggerid': u'14138', u'description': u'Servidor Reiniciado'}
{u'triggerid': u'14139', u'description': u' Ping'}
{u'triggerid': u'18082', u'description': u'Interface 7A'}
{u'triggerid': u'18083', u'description': u'Interface 7B'}
{u'triggerid': u'18084', u'description': u'Interface 8A'}
{u'triggerid': u'18085', u'description': u'Interface 8B'}
{u'triggerid': u'18086', u'description': u'Interface 9A'}
{u'triggerid': u'18087', u'description': u'Interface 9B'}
{u'triggerid': u'14140', u'description': u'System Health'}
{u'triggerid': u'14141', u'description': u'CPU 90% utilizada'}
{u'triggerid': u'14143', u'description': u' Sem coleta de dados via SNMP por 30 minutos'}
{u'triggerid': u'23138', u'description': u'Servidor Reiniciado'}
{u'hostid': u'10412'}
{u'triggerid': u'22084', u'description': u'Agente Zabbix inoperante'}
{u'triggerid': u'22086', u'description': u' McAfee IPS Manager Database (servi\xe7o_windows - McAfeeNetworkSecurit
{u'triggerid': u'22088', u'description': u' McAfee IPS Network Security Manager (servi\xe7o_windows - NetworkSecur
{u'triggerid': u'22090', u'description': u'CPU 100% utilizada'}
{u'triggerid': u'22091', u'description': u'Servidor Reiniciado'}
{u'triggerid': u'22092', u'description': u'Servidor Reiniciado'}
{u'triggerid': u'22122', u'description': u'Espa\xe7o em disco menor que 5% em C:'}
{u'triggerid': u'22124', u'description': u'Espa\xe7o em disco menor que 10% em C:'}
{u'triggerid': u'22123', u'description': u'Espa\xe7o em disco menor que 5% em D:'}
{u'triggerid': u'22125', u'description': u'Espa\xe7o em disco menor que 10% em D:'}
{u'triggerid': u'22094', u'description': u'Mem\xf3ria dispon\xedvel menor que 10%'}

谢谢大家。