Skip to content

Instantly share code, notes, and snippets.

@mikeyk
Created July 25, 2012 18:31
Show Gist options
  • Save mikeyk/3177747 to your computer and use it in GitHub Desktop.
Save mikeyk/3177747 to your computer and use it in GitHub Desktop.
Create a StatusCheckFailed alarm
import boto.ec2.cloudwatch
import sys
import os
AWS_KEY = os.environ.get("AWS_ACCESS_KEY_ID")
AWS_SECRET = os.environ.get("AWS_SECRET_ACCESS_KEY")
AWS_REGION = os.environ.get("AWS_EC2_REGION", "us-east-1")
TOPIC = 'YOUR_TOPIC'
def create_status_alarm(instance_id):
ec2_conn = boto.ec2.connect_to_region(AWS_REGION,
aws_access_key_id=AWS_KEY,
aws_secret_access_key=AWS_SECRET)
cloudwatch_conn = boto.ec2.cloudwatch.connect_to_region(
AWS_REGION,
aws_access_key_id=AWS_KEY,
aws_secret_access_key=AWS_SECRET)
reservations = ec2_conn.get_all_instances(filters = {'instance-id': instance_id})
if reservations and reservations[0].instances:
instance = reservations[0].instances[0]
instance_name = instance.tags['Name']
else:
print "Invalid instance-id!"
sys.exit(1)
alarm = boto.ec2.cloudwatch.alarm.MetricAlarm(
connection = cloudwatch_conn,
name = instance_name + "-status-alarm",
metric = 'StatusCheckFailed',
namespace = 'AWS/EC2',
statistic = 'Maximum',
comparison = '>=',
description = 'status check for %s %s' % (instance_id, instance_name),
threshold = 1.0,
period = 60,
evaluation_periods = 2,
dimensions = {'InstanceId':instance_id},
alarm_actions = [TOPIC],
ok_actions = [TOPIC],
insufficient_data_actions = [TOPIC])
cloudwatch_conn.put_metric_alarm(alarm)
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: create_status_alarm.py <instanceid>"
sys.exit(2)
create_status_alarm(sys.argv[1])
@jonathanwcrane
Copy link

Very nice. Helpful demonstration of something I'm building today. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment