【无标题】prometheus 钉钉webhook,加签方式

import json
import requests
import time
import hmac
import hashlib
import base64
import urllib.parse
from urllib.parse import urlparse

from flask import Flask
from flask import request

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def send():
    if request.method == 'POST':
        post_data = request.get_data()
        app.logger.debug(post_data)
        send_alert(json.loads(post_data))
        return 'success'
    else:
        return 'weclome to use prometheus alertmanager dingtalk webhook server!'


def send_alert(data):
    token = 'xx'
    secret = 'xx'
    timestamp = int(round(time.time() * 1000))
    url = 'https://oapi.dingtalk.com/robot/send?access_token=%s&timestamp=%d&sign=%s' % (token, timestamp, make_sign(timestamp, secret))

    alerts = data['alerts']
    alert_name = alerts[0]['labels']['alertname']
    data_count = []
    for k in alerts:
        a= k['labels']['job']
        b = k['labels']['instance']
        c = k['annotations']['description']
        data_count.append('\n' + '--------------------------' + '\n' + "名称: "  + a + '\n' +'主机IP:' +  b + '\n' + '内容:' + c)
    data_count  = "\n".join(data_count)

    title = '%s 有 %d 条新的报警' % (alert_name, len(alerts))

    send_data = {
        "msgtype": "markdown",
        "markdown": {
            "title": title,
            "text": title + "\n" + "![](https://bxdc-static.oss-cn-beijing.aliyuncs.com/images/prometheus-recording-rules.png)\n" + data_count
        }
    }

    req = requests.post(url, json=send_data)

def make_sign(timestamp, secret):
    secret_enc = bytes(secret, 'utf-8')
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = bytes(string_to_sign, 'utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    return sign


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
prometheus-webhook-dingtalk 是一个用于将 Prometheus 监控告警消息发送到Webhook 工具。它可以帮助你将 Prometheus 监控告警通过机器人发送到指定的群组或用户。 你可以通过以下步骤来配置和使用 prometheus-webhook-dingtalk: 1. 安装 prometheus-webhook-dingtalk:你可以使用 Go 工具链来安装 prometheus-webhook-dingtalk,运行以下命令: ``` go get github.com/timonwong/prometheus-webhook-dingtalk/cmd/dingtalk ``` 2. 创建机器人:在中创建一个自定义机器人,并获取到它的 Webhook 地址,用于将告警消息发送到指定的群组或用户。 3. 创建配置文件:在 prometheus-webhook-dingtalk 的配置文件中,你需要指定机器人的 Webhook 地址以及其他相关参数。你可以创建一个名为 config.yml 的配置文件,并将以下示例内容填入: ```yaml listen: 0.0.0.0:8060 dingtalk: webhook: https://oapi.dingtalk.com/robot/send?access_token=your_webhook_token ``` 4. 启动 prometheus-webhook-dingtalk:运行以下命令来启动 prometheus-webhook-dingtalk: ``` dingtalk -config.file=config.yml ``` 5. 配置 Prometheus:在 Prometheus 的配置文件中,添以下内容来指定告警消息的接收端: ```yaml receivers: - name: 'dingtalk' webhook_configs: - url: 'http://prometheus-webhook-dingtalk:8060/dingtalk/webhook' ``` 6. 重新启动 Prometheus:确保 Prometheus 已经重新载了配置文件,并重启 Prometheus 服务。 现在,当 Prometheus 监控触发告警时,prometheus-webhook-dingtalk 将会将告警消息发送到机器人的 Webhook 地址,从而通知到指定的群组或用户。 请注意,以上步骤仅为一般示例,实际操作可能会因环境和需求而有所不
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值