Answer the question
In order to leave comments, you need to log in
Which CloudWatch metric to choose?
Hello.
Trying to set up notifications when my server is down.
To do this, I do: -
create an SNS topic
- subscribe to it (which I then confirm)
import boto.ec2.cloudwatch
import sys
import os
import boto.sns
os.environ['AWS_KEY'] = "xxxxx"
os.environ['AWS_SECRET'] = "xxxxx"
os.environ['AWS_REGION'] = "us-west-2"
def create_status_alarm(instance_id):
ec2_conn = boto.ec2.connect_to_region(
os.environ['AWS_REGION'],
aws_access_key_id=os.environ['AWS_KEY'],
aws_secret_access_key=os.environ['AWS_SECRET'])
cloudwatch_conn = boto.ec2.cloudwatch.connect_to_region(
os.environ['AWS_REGION'],
aws_access_key_id=os.environ['AWS_KEY'],
aws_secret_access_key=os.environ['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)
sns_conn = boto.sns.SNSConnection(aws_access_key_id = os.environ['AWS_KEY'], aws_secret_access_key = os.environ['AWS_SECRET'], region=boto.regioninfo.RegionInfo(name=os.environ['AWS_REGION'], endpoint='sns.%s.amazonaws.com' % os.environ['AWS_REGION']))
sns_topic = sns_conn.create_topic('qweqwe2')
topic_arn = sns_topic["CreateTopicResponse"]\
["CreateTopicResult"]\
["TopicArn"]
subscription = sns_conn.subscribe(topic_arn, "email", "[email protected]")
print topic_arn
alarm = boto.ec2.cloudwatch.alarm.MetricAlarm(
connection = cloudwatch_conn,
name = instance_name + "checkfail2",
metric = 'StatusCheckFailed',
namespace = 'AWS/EC2',
statistic = 'Minimum',
comparison = '>',
description = 'status check for %s %s' % (instance_id, instance_name),
threshold = 0,
period = 60,
evaluation_periods = 2,
dimensions = {'InstanceId':instance_id},
alarm_actions = topic_arn,
ok_actions = topic_arn,
insufficient_data_actions = topic_arn)
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])
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question