|
| 1 | +import urllib3 |
| 2 | +import json |
| 3 | + |
| 4 | + |
| 5 | +slack_url = "https://hooks.slack.com/services/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" |
| 6 | +http = urllib3.PoolManager() |
| 7 | + |
| 8 | + |
| 9 | +def get_alarm_attributes(sns_message): |
| 10 | + alarm = dict() |
| 11 | + |
| 12 | + alarm['name'] = sns_message['AlarmName'] |
| 13 | + alarm['description'] = sns_message['AlarmDescription'] |
| 14 | + alarm['reason'] = sns_message['NewStateReason'] |
| 15 | + alarm['region'] = sns_message['Region'] |
| 16 | + alarm['instance_id'] = sns_message['Trigger']['Dimensions'][0]['value'] |
| 17 | + alarm['state'] = sns_message['NewStateValue'] |
| 18 | + alarm['previous_state'] = sns_message['OldStateValue'] |
| 19 | + |
| 20 | + return alarm |
| 21 | + |
| 22 | + |
| 23 | +def register_alarm(alarm): |
| 24 | + return { |
| 25 | + "type": "home", |
| 26 | + "blocks": [ |
| 27 | + { |
| 28 | + "type": "header", |
| 29 | + "text": { |
| 30 | + "type": "plain_text", |
| 31 | + "text": ":warning: " + alarm['name'] + " alarm was registered" |
| 32 | + } |
| 33 | + }, |
| 34 | + { |
| 35 | + "type": "divider" |
| 36 | + }, |
| 37 | + { |
| 38 | + "type": "section", |
| 39 | + "text": { |
| 40 | + "type": "mrkdwn", |
| 41 | + "text": "_" + alarm['description'] + "_" |
| 42 | + }, |
| 43 | + "block_id": "text1" |
| 44 | + }, |
| 45 | + { |
| 46 | + "type": "divider" |
| 47 | + }, |
| 48 | + { |
| 49 | + "type": "context", |
| 50 | + "elements": [ |
| 51 | + { |
| 52 | + "type": "mrkdwn", |
| 53 | + "text": "Region: *" + alarm['region'] + "*" |
| 54 | + } |
| 55 | + ] |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | +def activate_alarm(alarm): |
| 62 | + return { |
| 63 | + "type": "home", |
| 64 | + "blocks": [ |
| 65 | + { |
| 66 | + "type": "header", |
| 67 | + "text": { |
| 68 | + "type": "plain_text", |
| 69 | + "text": ":red_circle: Alarm: " + alarm['name'], |
| 70 | + } |
| 71 | + }, |
| 72 | + { |
| 73 | + "type": "divider" |
| 74 | + }, |
| 75 | + { |
| 76 | + "type": "section", |
| 77 | + "text": { |
| 78 | + "type": "mrkdwn", |
| 79 | + "text": "_" + alarm['reason'] + "_" |
| 80 | + }, |
| 81 | + "block_id": "text1" |
| 82 | + }, |
| 83 | + { |
| 84 | + "type": "divider" |
| 85 | + }, |
| 86 | + { |
| 87 | + "type": "context", |
| 88 | + "elements": [ |
| 89 | + { |
| 90 | + "type": "mrkdwn", |
| 91 | + "text": "Region: *" + alarm['region'] + "*" |
| 92 | + } |
| 93 | + ] |
| 94 | + } |
| 95 | + ] |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +def resolve_alarm(alarm): |
| 100 | + return { |
| 101 | + "type": "home", |
| 102 | + "blocks": [ |
| 103 | + { |
| 104 | + "type": "header", |
| 105 | + "text": { |
| 106 | + "type": "plain_text", |
| 107 | + "text": ":large_green_circle: Alarm: " + alarm['name'] + " was resolved", |
| 108 | + } |
| 109 | + }, |
| 110 | + { |
| 111 | + "type": "divider" |
| 112 | + }, |
| 113 | + { |
| 114 | + "type": "section", |
| 115 | + "text": { |
| 116 | + "type": "mrkdwn", |
| 117 | + "text": "_" + alarm['reason'] + "_" |
| 118 | + }, |
| 119 | + "block_id": "text1" |
| 120 | + }, |
| 121 | + { |
| 122 | + "type": "divider" |
| 123 | + }, |
| 124 | + { |
| 125 | + "type": "context", |
| 126 | + "elements": [ |
| 127 | + { |
| 128 | + "type": "mrkdwn", |
| 129 | + "text": "Region: *" + alarm['region'] + "*" |
| 130 | + } |
| 131 | + ] |
| 132 | + } |
| 133 | + ] |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | +def lambda_handler(event, context): |
| 138 | + sns_message = json.loads(event["Records"][0]["Sns"]["Message"]) |
| 139 | + alarm = get_alarm_attributes(sns_message) |
| 140 | + |
| 141 | + msg = str() |
| 142 | + |
| 143 | + if alarm['previous_state'] == "INSUFFICIENT_DATA" and alarm['state'] == 'OK': |
| 144 | + msg = register_alarm(alarm) |
| 145 | + elif alarm['previous_state'] == 'OK' and alarm['state'] == 'ALARM': |
| 146 | + msg = activate_alarm(alarm) |
| 147 | + elif alarm['previous_state'] == 'ALARM' and alarm['state'] == 'OK': |
| 148 | + msg = resolve_alarm(alarm) |
| 149 | + |
| 150 | + encoded_msg = json.dumps(msg).encode("utf-8") |
| 151 | + resp = http.request("POST", slack_url, body=encoded_msg) |
| 152 | + print( |
| 153 | + { |
| 154 | + "message": msg, |
| 155 | + "status_code": resp.status, |
| 156 | + "response": resp.data, |
| 157 | + } |
| 158 | + ) |
0 commit comments