当前位置:   article > 正文

统计zabbix 报警事件信息_zabbix alert.get

zabbix alert.get
使用zabbix的python api根据主机名过滤统计3天的报警的所有事件
  • 详细代码如下:
#-*- coding:utf-8 -*-
# python2.7
'''从zabbix 监控获取业务组报警的信息'''

import requests
import json
import time
import datetime
import re


server_url = "http://monitor.test.com:8000/api_jsonrpc.php"
header = {"Content-Type": "application/json"}
username = "admin"
password = "****"

def get_token():
‘’‘ 获取api 返回的token 值 ’‘’
    login_data = json.dumps({
        "jsonrpc": "2.0",
        "method": "user.login",
        "params": {
            'user': username,
            'password': password
        },
        "id": 0
    })
    response_auth = requests.post(server_url, data=login_data, headers=header)
    token = json.loads(response_auth.text)["result"]
    return  token


start_time_str = (datetime.datetime.now()+datetime.timedelta(days=-3)).strftime("%Y-%m-%d %H:%M")
end_time_str =  datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
# 转换为时间戳
start_time =  time.mktime(time.strptime(start_time_str,"%Y-%m-%d %H:%M"))
end_time = time.mktime(time.strptime(end_time_str,"%Y-%m-%d %H:%M"))

def get_alert():
    data = json.dumps(
        {"jsonrpc": "2.0", "method": "alert.get",
         "params": {"output": "extend",
                    "time_from":start_time,"time_till":end_time},
         "auth": get_token(),
         "id": 0}
    )
    responese = requests.post(server_url, data=data, headers=header)
    alter_list = responese.json()["result"]

    for item in alter_list:
        message = item["message"]
        messagestr = message.encode("utf-8")
        #print messagestr
        if messagestr != "Zabbix Server 心跳检测":
             messageDict = json.loads(messagestr)
             hostname = messageDict["hostname"]
             current_status = messageDict["current_status"]
             yw_hostname = re.findall(r"^yw.*|^sh1-yw.*|.*?yewu.*", hostname, re.I)
             message = re.findall(r"Zabbix agent.*|.*restarted.*|.*Hostname was changed.*|.*Host information.*",messageDict["message"],re.I)
             if len(yw_hostname) > 0 and not current_status.startswith("OK") and len(message) == 0:
                 print messageDict


if __name__ == "__main__":
      get_alert()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/297773
推荐阅读
相关标签
  

闽ICP备14008679号