首页
  • 监控

    • grafana
    • prometheus
  • 学习笔记

    • 《核心系统命令实战》
    • 《MySQL 是怎样运行的:从根儿上理解 MySQL》
    • 《Ansible权威指南》
  • 博客搭建
  • git
  • python
  • 友情链接
  • 文档编写规范
  • 我用过的电脑
  • 喷涂相关
  • 每日一溜
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

小刘说

砥砺前行
首页
  • 监控

    • grafana
    • prometheus
  • 学习笔记

    • 《核心系统命令实战》
    • 《MySQL 是怎样运行的:从根儿上理解 MySQL》
    • 《Ansible权威指南》
  • 博客搭建
  • git
  • python
  • 友情链接
  • 文档编写规范
  • 我用过的电脑
  • 喷涂相关
  • 每日一溜
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • python

    • Alert manager自定义webhook发送告警消息
      • Alert manager自定义webhook发送告警消息
      • Alert manager 配置
  • 编程
  • python
小刘
2022-04-18
目录

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

# Alert manager 配置

receivers:
#接收者名称
- name: 'xxx'
  #接收者为webhook类型
  webhook_configs:
  #webhook的接收地址
  - url: 'http://127.0.0.1:5000/send'
1
2
3
4
5
6
7
上次更新: 2024/05/11, 03:55:33
最近更新
01
kubernetes控制器-Service
08-18
02
kubernetes控制器-Deployment
08-08
03
kubernetes调度基础
07-27
更多文章>
Theme by Vdoing | Copyright © 2023-2024 本站支持IPv6访问 本站支持SSL安全访问
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式