-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcw-show-alarms-sns-actions.py
81 lines (60 loc) · 2.91 KB
/
cw-show-alarms-sns-actions.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
#!/usr/bin/python
# needs a re-write to use boto3 not CLI scraping
import os
import json
import argparse
def show_actions(actionName, alarmName):
nSNScount = 0
for snsTopicArn in alarmJson[actionName]:
if snsTopicArn.find("arn:aws:sns:") != -1:
snsTopicName = snsTopicArn[snsTopicArn.rfind(":")+1:]
for k in snsSubscriptionsJson["Subscriptions"]:
if (k["TopicArn"] == snsTopicArn):
print (f'{actionName},{alarmName},{snsTopicName},{k["Endpoint"]}')
nSNScount += 1
return nSNScount
def printverbose(sStr):
if bVerbose:
print (f'Verbose Message: {sStr}')
return
def main(args):
global alarmJson
global snsSubscriptionsJson
global bVerbose
nAlarmActions = 0
nInsufficientDataActions = 0
nOKActions = 0
bVerbose = args.verbose
if args.region:
sRegionParam = f'--region {args.region}'
else:
sRegionParam = ""
if args.profile:
sProfileParam = f'--profile {args.profile}'
else:
sProfileParam = ""
sAlarmsQuery = f'aws {sRegionParam} {sProfileParam} cloudwatch describe-alarms --max-items 1000'
printverbose(sAlarmsQuery)
result = os.popen(sAlarmsQuery).read()
cwAlarmsJson = json.loads(result)
sSNSQuery = f'aws {sRegionParam} {sProfileParam} sns list-subscriptions --max-items 1000'
printverbose(sSNSQuery)
result = os.popen(sSNSQuery).read()
snsSubscriptionsJson = json.loads(result)
if not args.noheader:
print ("Actions,Alarm Name,Topic,Subscription Endpoint")
for alarmJson in cwAlarmsJson["MetricAlarms"]:
alarmName = alarmJson["AlarmName"]
nAlarmActions += show_actions("AlarmActions", alarmName)
nInsufficientDataActions += show_actions("InsufficientDataActions", alarmName)
nOKActions += show_actions("OKActions", alarmName)
printverbose ("Alarms Returned: " + str(len(alarmJson)))
printverbose (f"Distinct SNS Actions Returned: Alarm Actions {nAlarmActions}, Insufficient Data Actions {nInsufficientDataActions}, OK Actions {nOKActions} = Total {nAlarmActions+nInsufficientDataActions+nOKActions}")
if __name__== "__main__":
parser = argparse.ArgumentParser(description="List Cloudwatch alarms and show their corresponding SNS actions in a linear, CSV-style format. Note this tool only shows SNS actions and ignores scaling actions.")
parser.add_argument('--region', help='The AWS region name, e.g. eu-west-1')
parser.add_argument('--profile', help='The AWS CLI profile to use')
parser.add_argument('--noheader', action='store_true', help='don\'t add the CSV header row')
parser.add_argument('--verbose', action='store_true', help='Verbose mode - display aws commands to execute, etc')
args = parser.parse_args()
main(args)