我尝试将 CloudWatch 警报通知给 Teams/Chatwork!
你好!
我是Beyond大阪事务所的拉面大王Hide。
这是我的第四篇文章。
在这篇文章中,我将介绍一种向Teams/Chatwork通知CloudWatch警报的方法,这是我最近在一个项目中实现的。
在我之前的博文中,我写了一篇关于如何学习AWS认证SAA(Solution Architect Associate)的文章,有兴趣的话请看一下!
配置图
配置图如上所示。
使用 CloudWatch 获取 EC2 指标并在发生警报时通知 SNS。然后,SNS 将从 CloudWatch 发送的各种值发送到 Lambda。
Lambda 格式化该值并将其发送到每个通信工具。
这看起来可能很困难,因为它使用了各种AWS资源,但它很容易实现,所以让我们一起努力吧!
施工步骤
① 创建Lambda
①-①:Lambda > 函数 > 点击创建函数
①-②:指定以下信息,然后点击【创建函数】
● 函数名称:请指定您选择的名称
● 运行时:请指定Python3 系列
①-③:粘贴每个通讯工具的代码
*如果您不想直接在代码中写入webhook URL或身份验证令牌,请设置环境变量。
● 团队守则
#!/usr/bin/python3.8 import boto3 import json from urllib.request import Request, urlopen #替换WebhookURL HOOK_URL = "https://xxxxxxxxxxxxxxx" def lambda_handler(event, context): #生产警报 #message = json . loads(event['Records'][0]['Sns']['Message']) #subject = event['Records'][0]['Sns']['Subject'] #报警测试消息 = { "AlarmName":"test-CPUUtilization"、"AWSAccountId":"xxxxxxxxxxx"、"NewStateValue":"ALARM"、"NewStateReason":"已跨越阈值:最后 1 个数据点中的 1 个 [1.8083480560463125 (xx/xx/xx xx) :xx:xx)] 小于或等于阈值 (80.0)(OK -> ALARM 转换至少 1 个数据点)。","StateChangeTime":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx " ,"Region":"亚太地区(东京)"} subject = "ALARM: test-CPUUtilization in Asia Pacific (Tokyo)" #报警信息 Alarm_name = message["AlarmName"] new_state = message["NewStateValue"] Reason = message [“NewStateReason”]alarm_time = 消息[“StateChangeTime”] account_id = 消息[“AWSAccountId”]区域=消息[“Region”]alert_url =“https://ap-northeast-1.console.aws.amazon.com / cloudwatch/home?region=ap-northeast-1#alarmsV2:alarm/%s" %alarm_namealert_msg = { 'title': "%s" %subject, 'text': '<br>警报名称:%s' %alarm_name + '<br>状态:%s' %new_state + '<br>报警原因:%s' %原因 + '<br><br>报警发生时间(UTC):%s' %alarm_time + '<br>帐户 ID:%s' %account_id + '<br>区域:%s' %region + '<br><br>报警网址:<br> %s' %alert_url } #将字典类型转换为字符串并生成HTTP请求 req = Request(HOOK_URL, json.dumps(alert_msg).encode('utf-8')) #发送 urlopen(req)
● 聊天工作代码
#!/usr/bin/python3.8 import boto3 import json import urllib from urllib.request import Request, urlopen #指定房间ID room_id = 'xxxxxxxxxxxx' #指定tokun = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx' chatwork_URL = "https:// api. chatwork.com/v2/rooms/%s/messages" %room_id def lambda_handler(event, context): #生产警报 #message = json.loads(event['Records'][0]['Sns'][ 'Message ']) #subject = event['Records'][0]['Sns']['Subject'] #警报测试消息 = {"AlarmName":"test-CPUUtilization","AWSAccountId":"xxxxxxxxxxx" ," NewStateValue":"ALARM","NewStateReason":"超过阈值:最后 1 个数据点中的 1 个 [1.8083480560463125 (xx/xx/xx xx:xx:xx)] 小于或等于阈值 (80.0)(最小值1 个数据点用于 OK -> ALARM 转换)。","StateChangeTime":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","Region":"亚太地区(东京)"} subject = "ALARM : 测试-亚太地区(东京)的CPUUtilization" #警报信息 Alarm_name = message["AlarmName"] new_state = message["NewStateValue"] Reason = message["NewStateReason"] Alarm_time = message["StateChangeTime"] account_id = message[ "AWSAccountId "] 区域 = 消息["区域"]alert_url = "https://ap-northeast-1.console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#alarmsV2:alarm/%s" %alarm_name alert_msg = { 'body': '[info][title]%s[/title]'%subject+ '警报名称: %s'%alarm_name +'\n'+ '状态: %s'%new_state +' \n'+ '报警原因:%s'%reason +'\n'+ '报警发生时间(UTC):%s'%alarm_time +'\n'+ '账户 ID:%s'%account_id + '\ n'+ '区域:%s'%region +'\n\n'+ '报警 URL:%s'%alert_url + '[/info]' } headers = { 'X-ChatWorkToken': '%s ' % tokun } #生成查询字符串 data = urllib.parse.urlencode(alert_msg) #转换字符编码 data = data.encode('utf-8') #生成HTTP请求 req = Request(chatwork_URL, data, headers) #发送 urlopen(要求)
*补充:各通讯工具的消息表示法
● 聊天工作
https://developer.chatwork.com/docs/message-notation
● AWS
https://aws.amazon.com/jp/premiumsupport/knowledge-center/sns-lambda-webhooks-chime-slack-teams
①-④:点击【部署】
①-⑤:点击【测试】
①-⑥:填写以下信息,然后点击【保存】
● 事件名称:请指定您选择的名称
● 模板选项:sns-notification
①-⑦:点击【测试】
①-⑧:检查测试报警是否已通知
● 团队
● 聊天工作
①-⑨:编辑变量如下,用于实际的报警通知
#生产报警消息 = json.loads(event['Records'][0]['Sns']['Message']) subject = event['Records'][0]['Sns']['Subject'] #警报测试 #message = {"AlarmName":"test-CPUUtilization","AlarmDescription":"null","AWSAccountId":"xxxxxxxxxxx","AlarmConfigurationUpdatedTimestamp":"xxxx-xx-xxTxx:xx:xx.xxx+ xxxx ","NewStateValue":"ALARM","NewStateReason":"超过阈值:最后 1 个数据点中的 1 个 [1.8083480560463125 (xx/xx/xx xx:xx:xx)] 小于或等于阈值 ( 80.0 )(OK -> ALARM 转换至少 1 个数据点)。","StateChangeTime":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","Region":"亚太地区(东京)","AlarmArn " :"arn:aws:cloudwatch:ap-northeast-1:xxxx:alarm:test-CPUUtilization","OldStateValue":"确定","OKActions":[],"AlarmActions":["arn:aws:sns : ap-northeast-1:xxxxxxxx:test"],"InsufficientDataActions":[],"Trigger":{"MetricName":"CPUUtilization","Namespace":"AWS/EC2","StatisticType":"Statistic" , "Statistic":"AVERAGE","Unit":"null","Dimensions":[{"value":"i-07876365e6af5774f","name":"InstanceId"}],"Period":300,"EvaluationPeriods ":1,"DatapointsToAlarm":1,"ComparisonOperator":"LessThanOrEqualToThreshold","Threshold":80.0,"TreatMissingData":"missing","EvaluateLowSampleCountPercentile":""}} #subject = "警报: 测试 CPUUtilization亚太地区(东京)”
② 创建SNS
②-①:SNS > 主题 > 单击“创建主题”
②-②:指定以下信息,然后点击【创建主题】
● 类型:标准
● 名称: 请输入您喜欢的名字
②-③:点击【创建订阅】
②-④:指定以下信息,然后点击【创建订阅】
*Lambda ARN 信息位置
该函数的 ARN 位于创建的函数 > 函数概述的最右侧,因此请从那里复制它。
②-⑤:查看状态
③ 创建CloudWatch
*我们将假设 EC2 已创建。
③-①:云观察>报警>点击【创建报警】
③-②:选择您最喜欢的指标
③-③:选择报警条件
③-④:通知设置
③-⑤:指定报警名称
③-⑥:点击【创建报警】
④ 报警通知测试
④-①:检查报警是否开启。
④-②:查看报警是否已通知
● 团队
● 聊天工作
*注:
如果您想查看CloudWatch中的所有报警信息,可以通过编辑变量alert_msg的内容来参考,如下所示。
■ 团队
Alert_msg ={ "text":"%s" %message }
■ 聊天工作
Alert_msg = { 'body': '%s' %message }
[输出结果]
{"AlarmName":"test-CPUUtilization","AlarmDescription":"null","AWSAccountId":"xxxxxxxxxxx","AlarmConfigurationUpdatedTimestamp":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","NewStateValue" :"ALARM","NewStateReason":"超过阈值:最后 1 个数据点中的 1 个 [1.8083480560463125 (xx/xx/xx xx:xx:xx)] 小于或等于阈值 (80.0)(最少 1 个数据点)用于 OK -> ALARM 转换)。","StateChangeTime":"xxxx-xx-xxTxx:xx:xx.xxx+xxxx","Region":"亚太地区(东京)","AlarmArn":"arn:aws :cloudwatch:ap-northeast-1:xxxx:alarm:test-CPUUtilization","OldStateValue":"确定","OKActions":[],"AlarmActions":["arn:aws:sns:ap-northeast-1 :xxxxxxxx:test"],"InsufficientDataActions":[],"触发器":{"MetricName":"CPUUtilization","命名空间":"AWS/EC2","StatisticType":"统计","统计":"平均","单位":"null","维度":[{"值":"i-xxxxxxxxxxxxxx","名称":"InstanceId"}],"期间":300,"评估期间":1," DatapointsToAlarm":1,"ComparisonOperator":"LessThanOrEqualToThreshold","阈值":80.0,"TreatMissingData":"缺失","EvaluateLowSampleCountPercentile":""}}
概括
感谢您的辛勤工作!
这次我们介绍的是如何向各个通讯工具通知报警内容!
Slack可以通过AWS Chatbot轻松通知警报,但CloudWatch/SNS不具备向Teams/Chatwork发送警报的能力,因此如果您想通知警报,则很难做到这一点。
您还可以编辑代码以相应地发送消息。请在通知警报时使用它!