-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2_alaram.py
55 lines (49 loc) · 1.77 KB
/
ec2_alaram.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
import boto3
import collections
#Create Session with IAM User
session = boto3.session.Session(aws_access_key_id='ACCESS_KEY', aws_secret_access_key='ACCESS_KEY_SECRET')
ec2_sns = '<SNS_TOPIC_ARN>'
name_tag_equal = "something"
#Create AWS clients
ec = session.client('ec2')
cw = session.client('cloudwatch')
#Enumerate EC2 instances
reservations = ec.describe_instances().get('Reservations', [])
instances = sum(
[
[i for i in r['Instances']]
for r in reservations
], [])
for instance in instances:
try:
for tag in instance['Tags']:
if tag['Key'] == 'Name':
name_tag = tag['Value']
print "Found instance %s with name %s" % (instance['InstanceId'], name_tag)
#Create CPU Credit Alarms
if name_tag == name_tag_equal:
#Create Metric "CPU Credit Balance >= 95 for 10 Minutes"
response = cw.put_metric_alarm(
AlarmName="%s %s Credit Balance Warning" % (name_tag, instance['InstanceId']),
AlarmDescription='CPU Credit Balance >= 95 for 10 Minutes',
ActionsEnabled=True,
AlarmActions=[
ec2_sns,
],
MetricName='CPUCreditBalance',
Namespace='AWS/EC2',
Statistic='Average',
Dimensions=[
{
'Name': 'InstanceId',
'Value': instance['InstanceId']
},
],
Period=300,
EvaluationPeriods=2,
Threshold=95.0,
ComparisonOperator='GreaterThanOrEqualToThreshold'
)
except Exception as e:
print ("Error Encountered.")
print (e)