04:获取zabbix监控信息

 目录:zabbix其他篇

01: 安装zabbix server

02:zabbix-agent安装配置 及 web界面管理

03: zabbix API接口 对 主机、主机组、模板、应用集、监控项、触发器等增删改查

04:获取zabbix监控信息

05:zabbix 监控配置

目录:

1.1 检索警报     返回顶部

  参考官网:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/alert/get

  1、通过触发器trigger获取所有报警信息

#! /usr/bin/env python
# -*- coding: utf-8 -*
import urllib2
import json

url = 'http://1.1.1.5/zabbix/api_jsonrpc.php'
username = 'Admin'
password = '1'

################################ 一:登陆脚本 login.py  ###########################
#1、定义通过HTTP方式访问API地址的函数,后面每次请求API的各个方法都会调用这个函数
def requestJson(url,values):
    data = json.dumps(values)
    req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'})
    response = urllib2.urlopen(req, data)
    output = json.loads(response.read())
    try:
        message = output['result']
    except:
        message = output['error']['data']
        print message
        quit()
    return output['result']

#2、API接口认证的函数,登录成功会返回一个Token
def authenticate(url, username, password):
    values = {'jsonrpc': '2.0',
              'method': 'user.login',
              'params': {
                  'user': username,
                  'password': password
              },
              'id': '0'
              }
    idvalue = requestJson(url,values)
    return idvalue  # 结果是一个token值:cc75ed2a314906a835ac0786266468ac
zabbix认证和请求函数
# 获取所有触发器中报警信息,如果不报警则不会获取到
def trigger_get_alarm(auth):
    values = {
                "jsonrpc": "2.0",
                "method": "trigger.get",
                "params": {
                    "output": [
                        "host",
                        "description",
                        "triggerid",
                        "eventid",
                        "templateids"
                    ],
                    "selectGroups": [
                        "name"
                    ],
                    "selectHosts": [
                        "name",
                        "host"
                    ],
                    "selectItems": [
                        "name",
                        "lastvalue",
                        "units"
                    ],
                    "filter": {
                        "value": 1
                    },
                    "monitored": 1,
                    "selectLastEvent": "extend",
                    "expandComment": 1,
                    "expandDescription": 1,
                    "sortfield": "priority",
                    "sortorder": "DESC",
                    "withLastEventUnacknowledged": 1
                },
                "auth": auth,
                "id": 1
            }

    output = requestJson(url, values)
    return output


auth = authenticate(url, username, password)
print json.dumps( trigger_get_alarm(auth) )

# 获取的报警信息如下
'''
[{
    "description": "User_Login",   # 这里是报警信息的具体内容
    "items": [{
        "itemid": "28439",
        "units": "",
        "lastvalue": "59",
        "name": "login_user"
    }],
    "lastEvent": {
        "eventid": "73",
        "objectid": "15601",
        "clock": "1528266869",
        "object": "0",
        "acknowledged": "0",
        "value": "1",
        "source": "0",
        "ns": "387320307"
    },
    "triggerid": "15601",
    "hosts": [{
        "host": "zabbix_agent_1.1.1.3",
        "hostid": "10264",
        "name": "zabbix_agent_1.1.1.3"
    }],
    "groups": [{
        "groupid": "19",
        "name": "New Create Group"
    }, {
        "groupid": "20",
        "name": "New Group 02"
    }]
}]
'''
获取所有触发器中报警信息,如果不报警则不会获取到

1.2 历史数据     返回顶部

  参考官网:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/history/get

#1、只返回给定 监控项 的历史记录
def get_historys_by_item(auth):
    values = {
                "jsonrpc": "2.0",
                "method": "history.get",
                "params": {
                    "output": "extend",
                    "history": 3,         # 要返回的历史对象类型
                    # 0 - numeric float;数字浮点数;1 - character;字符 ;2 - log; 日志;3 - numeric unsigned; 数字符号;4 - text.文本
                    "itemids": ["28439"],
                    "sortfield": "clock",
                    "sortorder": "DESC",
                    "limit": 2    # 显示两条数据
                },
                "auth": auth,
                "id": 1
            }

    output = requestJson(url, values)
    return output

auth = authenticate(url, username, password)
print json.dumps( get_historys_by_item(auth) )
'''
[{
    "itemid": "28439",
    "ns": "244866385",
    "value": "4",
    "clock": "1528274369"
}, {
    "itemid": "28439",
    "ns": "197647992",
    "value": "4",
    "clock": "1528274339"
}]
'''
1、只返回给定 监控项 的历史记录
#2、只返回给定 主机 的历史记录
def get_historys_by_host(auth):
    values = {
                "jsonrpc": "2.0",
                "method": "history.get",
                "params": {
                    "output": "extend",
                    "history": 3,         # 要返回的历史对象类型
                    # 0 - numeric float;数字浮点数;1 - character;字符 ;2 - log; 日志;3 - numeric unsigned; 数字符号;4 - text.文本
                    "hostids": "10264",
                    "sortfield": "clock",
                    "sortorder": "DESC",
                    "limit": 2    # 显示两条数据
                },
                "auth": auth,
                "id": 1
            }

    output = requestJson(url, values)
    return output

auth = authenticate(url, username, password)
print json.dumps( get_historys_by_host(auth) )
'''
[{
    "itemid": "28439",
    "ns": "244866385",
    "value": "4",
    "clock": "1528274369"
}, {
    "itemid": "28439",
    "ns": "197647992",
    "value": "4",
    "clock": "1528274339"
}]
2、只返回给定 主机 的历史记录

 

posted @ 2018-06-06 15:35  不做大哥好多年  阅读(1389)  评论(0编辑  收藏  举报