-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcloudwatch.py
112 lines (89 loc) · 3.08 KB
/
cloudwatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import logging
import os
import re
import boto3
import requests
def get_slack_bot_token_from_parameter_store():
parameter_name = os.environ['SLACK_TOKEN_PARAMNAME']
ssm = boto3.client('ssm')
response = ssm.get_parameters(
Names=[
parameter_name,
],
WithDecryption=True
)
credentials = response['Parameters'][0]['Value']
return credentials
def send_to_slack_with_channel(data, channel):
data['channel'] = channel
data['token'] = get_slack_bot_token_from_parameter_store()
url = "https://slack.com/api/chat.postMessage"
r = requests.post(url=url, data=data)
if r.status_code != 200:
raise Exception(r.text)
def custom_message_to_slack(title, icon, attachments, channel):
data = {
"username": "my_bot",
"icon_emoji": icon,
"text": title,
"attachments": json.dumps(attachments)
}
send_to_slack_with_channel(data, channel)
def slack_cloudwatch_alert_message(alarm_name, data, channel):
color = "danger"
state = data['NewStateValue']
if state in ["OK"]:
color = "good"
fields = [{
'title': attribute_name,
'value': attribute_value,
'short': False
} for attribute_name, attribute_value in data.items()]
attachments = [{
"color": color,
"fields": fields
}]
custom_message_to_slack(
channel=channel,
title=":warning: Warning - Cloudwatch alert %s" % alarm_name,
icon=":warning:",
attachments=attachments)
def notify_cloudwatch_alert_event(event):
sns_message = event['Records'][0]['Sns']['Message']
data = json.loads(sns_message)
channel = get_slack_channel_from_alert_desc(data['AlarmDescription'])
alert_name = data['AlarmName']
slack_cloudwatch_alert_message(alarm_name=alert_name,
data=data, channel=channel)
def get_slack_channel_from_alert_desc(description):
regex = r'.*channel:([^\s]*).*'
try:
match = re.match(regex, description)
channel = match.group(1)
if channel == "":
raise Exception("empty channel")
return channel
except Exception:
raise Exception("No slack channel can be extracted from: %s" %
description)
def is_cloudwatchalert_event(event):
"""
returns true if given event corresponds to a codepipeline approval event
"""
try:
contains_alarm_name = 'AlarmName' in event['Records'][0]['Sns'][
'Message']
contains_alarm_desc = 'AlarmDescription' in event['Records'][0]['Sns'][
'Message']
return contains_alarm_name and contains_alarm_desc
except Exception:
return False
def sns_alert_to_slack(event, context):
if is_cloudwatchalert_event(event):
notify_cloudwatch_alert_event(event)
else:
logging.log("received event is not cloudwatch alert event: %s" % json.dumps(event))
raise Exception("not recognized cloudwatch alert event")
print(get_slack_bot_token_from_parameter_store())
print("something else")