Skip to content

Instantly share code, notes, and snippets.

@hideokamoto
Created February 2, 2016 08:39
Show Gist options
  • Save hideokamoto/065277a5de7f372a44d1 to your computer and use it in GitHub Desktop.
Save hideokamoto/065277a5de7f372a44d1 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ $# -ne 3 ]; then
echo "Usage: $0 [AlarmName] [Region] [Profile]"
echo "Region: ap-northeast-1 ap-southeast-1 ap-southeast-2 eu-central-1 eu-west-1 sa-east-1 us-east-1 us-west-1 us-west-2"
exit
fi
NAME=$1
METRIC_NAME=StatusCheckFailed_System
NAMESPACE=AWS/EC2
STATISTIC=Maximum
PERIOD=60
EVALUATION_PERIODS=1
THRESHOLD=1
COMPARISON_OPERATOR=GreaterThanOrEqualToThreshold
ALARM_ACTIONS=arn:aws:automate:$2:ec2:recover
AWS="aws --profile $3 --region $2"
INSTANCE_LIST=`${AWS} ec2 describe-instances | jq -r '.Reservations[].Instances[].InstanceId'`
for INSTANCE_ID in $INSTANCE_LIST;
do
echo $INSTANCE_ID
ALARM_NAME="$INSTANCE_ID-$NAME"
$AWS cloudwatch put-metric-alarm \
--alarm-name $ALARM_NAME \
--alarm-actions $ALARM_ACTIONS \
--metric-name $METRIC_NAME \
--namespace $NAMESPACE \
--dimensions Name=InstanceId,Value=$INSTANCE_ID \
--statistic $STATISTIC \
--period $PERIOD \
--evaluation-periods $EVALUATION_PERIODS \
--threshold $THRESHOLD \
--comparison-operator $COMPARISON_OPERATOR
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment