通过ZabbixApi获取告警主机信息

通过ZabbixApi获取告警主机信息

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author  : LTSC
# @File    : trigger_data_info.py
# @Software: PyCharm
from login import *
##查询所有不正常告警触发器
def trigger_list(auth):
    values = {
        "jsonrpc": "2.0",
        "method": "trigger.get",
        "params": {
           #  输出结果包含参数
            "output": [
                "triggerid",
                "description",
                "status",
                "value",
                "priority",
                "lastchange",
                "recovery_mode",
                "hosts",
                "state"
            ],
            "selectHosts": "hosts",#需包含主机ID信息,以便于根据主机ID查询主机信息
            "filter": {
                # 筛选条件
                "value": 1,
                "status": 0
                },
        },
        'auth': auth,
        'id': '1'
    }
    output = requestJson(zabbix_url, values)
    return output

#根据主机ID查询主机信息
def hosts(auth, hostid):
    values = {
        "jsonrpc": "2.0",
        "method": "host.get",
        "params": {
            "output": [
                "hostid",
                "host",
                "available",
                "name",
                "status"
            ],
            "hostids": "%s" % (hostid)
             },
        'auth': auth,
        'id': '1'
    }
    output = requestJson(zabbix_url, values)
    return output
#告警列表
Trigger_list = trigger_list(auth)
    for Trigger_info in Trigger_list:
        Trigger_hostid = (Trigger_info['hosts'])[0]['hostid']#获取主机ID
        Host_info = hosts(auth, Trigger_hostid)#查询主机信息

你可能感兴趣的:(ZabbixApi)