04:獲取zabbix監控信息

 目錄:zabbix其餘篇

01: 安裝zabbix serverphp

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

03: zabbix API接口 對 主機、主機組、模板、應用集、監控項、觸發器等增刪改查python

04:獲取zabbix監控信息web

05:zabbix 監控配置json

目錄:

1.1 檢索警報     返回頂部

  參考官網:https://www.zabbix.com/documentation/3.4/zh/manual/api/reference/alert/getapi

  一、經過觸發器trigger獲取全部報警信息app

#! /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  ###########################
#一、定義經過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']

#二、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/getide

#一、只返回給定 監控項 的歷史記錄
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"
}]
'''
一、只返回給定 監控項 的歷史記錄
#二、只返回給定 主機 的歷史記錄
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"
}]
二、只返回給定 主機 的歷史記錄
相關文章
相關標籤/搜索