zoukankan      html  css  js  c++  java
  • 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、只返回给定 主机 的历史记录
  • 相关阅读:
    算法总结之 两个链表生成相加链表
    算法总结之 复制含有随机指针节点的链表
    算法总结之 将单向链表按某值划分成左边小、中间相等、右边大的形式
    在PHP5.3以上版本运行ecshop和ecmall出现的问题及解决方案
    windows下配置nginx+php环境
    ecmall程序结构图与数据库表分析
    ecmall数据字典
    Ecmall二次开发-增删改查操作
    PHP7:10件事情你需要知道的
    PHP命名空间规则解析及高级功能3
  • 原文地址:https://www.cnblogs.com/xiaonq/p/9145398.html
Copyright © 2011-2022 走看看