Alert manager自定义webhook发送告警消息
  # Alert manager自定义webhook发送告警消息
由于公司内部系统无法访问外网,这里采用使用 python 进行解析prometheus json消息,中转再转发到内网的短信接口上。 基于 python 2.7 版本
# -*- coding:utf-8 -*-
from flask import Flask, request
import requests
import json
import sys  
reload(sys)  
sys.setdefaultencoding('utf8')
app1 = Flask(__name__)
def sendMessage (url,dict):
      # 字典转化为json
      jsons = json.dumps(dict, sort_keys=True, indent=4, separators=(',', ':'))
      r1 = requests.post(url,data=jsons, headers={"Content-type": "application/json"})
      return r1
@app1.route('/send', methods=['POST'])
def send():
    try:
      url_send_api = 'http://127.0.0.1:8383/sendMessage' 
      # 获取并把json转化为字典
      data = json.loads(request.get_data(as_text=True))
      #print (request.get_data(as_text=True))
      # 遍历接收的字段的值
      for i in range(0,len(data["alerts"])):
        # 描述
        description = data["alerts"][i]["annotations"]["description"]
        # 标题
        #summary = data["alerts"][i]["annotations"]["summary"]
        # 手机号
        phone_number = data["receiver"]
        sendMessageDict = {
        "phones": phone_number,
        "message": description
        } 
        sendMessage(url_send_api,sendMessageDict)
        #print (sendMessageDict)
        print (description + phone_number)
    except Exception as e:
      print(e)
if __name__ == '__main__':
  app1.run(debug=False,host='127.0.0.1',port=5000)
 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
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
# Alert manager 配置
receivers:
#接收者名称
- name: 'xxx'
  #接收者为webhook类型
  webhook_configs:
  #webhook的接收地址
  - url: 'http://127.0.0.1:5000/send'
 1
2
3
4
5
6
7
2
3
4
5
6
7
上次更新: 2024/05/11, 03:55:33