grafana用lark发告警python3接口

1.先在lark群聊里面创建机器人,并获取机器人链接。
2.后台运行下面python3脚本。
3.在grafana添加告警通道,设置告警。

# !/usr/bin/env python
# _*_ coding: utf-8 _*_

from flask import Flask, request,jsonify
#import smtplib
#from email.mime.text import MIMEText
from datetime import datetime
import requests
import json

def send_lark(title,content):
    url = "你的lark机器人webhook地址"
    headers = {
            'Content-Type': 'application/json' 
            }
    payload_message = {
            "msg_type": "post",
            "content": {"post": {
                "zh_cn":{
                    "title":title,
                    "content":[[{
                        "tag":"text",
                        "text": content
                        }]]
                    }
                }}
            }
    response = requests.request("POST", url, headers=headers, data=json.dumps(payload_message))
    print(response)

app = Flask(__name__)

@app.route("/send_lark", methods=["POST"])
def send_mail():
    info = request.json
    print("#########:",info, type(info))
    cusname = "随便定义"
    alter_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    state = info['state']
    ruleName = info['ruleName']
    title=info['title']
    #message=info['message']
    metric=[]
    value=[]
    for i in range(0, len(info['evalMatches'])):
        metric.append(info['evalMatches'][i]['metric'])
        value.append(info['evalMatches'][i]['value'])
    content = """
    商户: %s
    主机: %s
    级别: %s
    规则: %s
    报警值: %s
    时间: %s
    """ % (cusname,metric,state,ruleName,value,alter_time)
    print(content)
    return jsonify(send_lark(title,content))
    #return "成功"

if __name__ == "__main__":
    app.run("0.0.0.0", "5050")

你可能感兴趣的:(python3,flask,python3,lark,grafana)