1.先在lark群聊里面创建机器人,并获取机器人链接。
2.后台运行下面python3脚本。
3.在grafana添加告警通道,设置告警。文章来源地址https://www.toymoban.com/news/detail-683204.html
# !/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")
文章来源:https://www.toymoban.com/news/detail-683204.html
到了这里,关于grafana用lark发告警python3接口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!