说明
关于cloudwatch对接飞书告警部署细节,详见飞书 Amazon CloudWatch 告警
lambda_function函数
# cat lambda_function.py
# -*- coding: UTF-8 -*-
# author: tengf.w@test.com
# date: 20211108
# version: V3
# Description: Use Feishu card template to send information
import requests
import json
import os
def lambda_handler(event, context):
# Set Feishu parameters
data_app = {
"app_id": "your-id",
"app_secret": "your-secret"
}
chat_name="your-chat-name"
# Get token
try:
res = requests.post("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/", json=data_app)
if res.status_code == 200:
res_json = res.json()
access_token = res_json.get("tenant_access_token")
access_token = access_token
except Exception as e:
return {"error": e}
headers={
"Authorization": "Bearer {}".format(access_token),
"Content-Type": "application/json; charset=utf-8"
}
# 获取群列表
params = {
"page_size": 100,
"page_token": ""
}
try:
res = requests.get("https://open.feishu.cn/open-apis/chat/v4/list", params=params, headers=headers)
if res.status_code == 200:
res_json = res.json()
data = res_json.get("data")
groups = data.get("groups")
for i in groups:
if i.get("name") == chat_name:
group = i
except Exception as e:
return {"error": e}
# send Message
chat_id = group.get("chat_id")
print(chat_id)
message = event['Records'][0]['Sns']
Timestamp = message['Timestamp']
Subject = message['Subject']
sns_message = json.loads(message['Message'])
region = message['TopicArn'].split(':')[-3]
NewStateReason = json.loads(event['Records'][0]['Sns']['Message'])['NewStateReason']
if "ALARM" in Subject:
title = '[AI生产环境] 警报!!'
elif "OK" in Subject:
title = '[AI生产环境] 故障恢复!'
else:
title = '[AI生产环境] 警报状态异常'
content = "**【详情信息】**\n" \
+ "**时间**: " + Timestamp + "\n" \
+ "**内容**: " + Subject + "\n" \
+ "**状态**: {old} => {new}".format(old=sns_message['OldStateValue'], new=sns_message['NewStateValue']) + "\n" \
+ "**AWS区域**: " + sns_message['Region'] + "\n" \
+ "**监控资源对象**: " + sns_message['Trigger']['Namespace'] + "\n" \
+ "**监控指标**: " + sns_message['Trigger']['MetricName'] + "\n" \
+ "**报警名称**: " + sns_message['AlarmName'] + "\n" \
+ "**报警创建方式**: " + sns_message['AlarmDescription'] + "\n" \
+ "**报警细节**: " + NewStateReason
data_alert = {
"chat_id": chat_id,
"msg_type": 'interactive',
"card": {
"config": {
"wide_screen_mode": True
},
"header": {
"template": "red",
"title": {
"tag": "plain_text",
"content": title
}
},
"elements": [
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": content
}
},
]
}
}
data_recover = {
"chat_id": chat_id,
"msg_type": 'interactive',
"card": {
"config": {
"wide_screen_mode": True
},
"header": {
"template": "green",
"title": {
"tag": "plain_text",
"content": title
}
},
"elements": [
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": content
}
},
]
}
}
try:
if "OK" in Subject:
response=requests.post("https://open.feishu.cn/open-apis/message/v4/send/", headers=headers,json=data_recover)
else:
response=requests.post("https://open.feishu.cn/open-apis/message/v4/send/", headers=headers,json=data_alert)
print(response)
print(response.json())
except Exception as e:
return {"error":e}
告警信息
当前使用版本,与之前版本不同之处是,卡片标题的颜色,红色表示告警,绿色表示告警恢复。报警信息格式: