Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created March 28, 2015 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mryoshio/2561baa55b9341a15ecf to your computer and use it in GitHub Desktop.
Save mryoshio/2561baa55b9341a15ecf to your computer and use it in GitHub Desktop.
Add two CloudWatch alarm
require 'aws-sdk'
REGION = 'ap-northeast-1'
WAIT_INTERVAL = 1
def put_alarms(instance_id)
cloudwatch = Aws::CloudWatch::Client.new(region: REGION)
cloudwatch.put_metric_alarm(
alarm_name: "cpu-mon-#{instance_id}",
alarm_description: "Alarm when CPU exceeds 70 percent",
actions_enabled: true,
alarm_actions: ['<ARN send notification to>'],
metric_name: 'CPUUtilization',
namespace: 'AWS/EC2',
statistic: 'Average',
dimensions: [
{
name: 'InstanceId',
value: instance_id
}
],
period: 300,
evaluation_periods: 1,
threshold: 70,
comparison_operator: 'GreaterThanOrEqualToThreshold'
)
cloudwatch.put_metric_alarm(
alarm_name: "status-checked-failed-#{instance_id}",
alarm_description: "Alarm when any status check failed",
actions_enabled: true,
alarm_actions: ['<ARN send notification to>'],
metric_name: 'StatusCheckFailed',
namespace: 'AWS/EC2',
statistic: 'Maximum',
dimensions: [
{
name: 'InstanceId',
value: instance_id
}
],
period: 60,
evaluation_periods: 2,
threshold: 1.0,
comparison_operator: 'GreaterThanOrEqualToThreshold'
)
sleep(WAIT_INTERVAL)
end
Aws::EC2::Resource.new(region: REGION) \
.instances.each { |instance| put_alarms(instance.id) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment