grafana用lark发告警python3接口

1.先在lark群聊里面创建机器人,并获取机器人链接。

2.后台运行下面python3脚本。

3.在grafana添加告警通道,设置告警。

python 复制代码
# !/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")
相关推荐
青 春 记 忆35 分钟前
零基础入门python21:Flask账本的数据模型——用户、分类和账目
python·flask·后端开发
烛影摇红透纱窗3 小时前
hiveserver2加入promethus+grafana的监控
grafana
虎王物联3 小时前
Docker Compose一键部署物联网平台:EMQX+InfluxDB+Grafana完整方案
物联网·docker·grafana·emqx
行者-全栈开发6 小时前
JMeter + InfluxDB + Grafana:搭建生产级压测实时监控与告警体系
jmeter·grafana·时序数据库·influxdb·实时监控·backendlistener·告警性能测试
玉&心2 天前
在grafana中引入prometheus的数据监控
grafana·prometheus
liuyicenysabel2 天前
Flask 个人站 iProfile 上线笔记(k3s + Nginx + Let‘s Encrypt)
笔记·nginx·flask
johnny2333 天前
Flask生态:Flask-SocketIO、Flask-MonitoringDashboard
flask
weixin_BYSJ19873 天前
springboot智慧公共交通系统---附源码51123
java·javascript·spring boot·python·django·flask·php
0xBADCODE3 天前
Flask SSTI读SECRET_KEY+伪造Session:税务系统渗透全流程
前端·后端·python·安全·web安全·网络安全·flask
玉&心3 天前
在grafana中加入elasticsearch的日志dashboard
elasticsearch·grafana