Skip to content

Instantly share code, notes, and snippets.

@davidplappert
Created July 18, 2016 14:48
Show Gist options
  • Select an option

  • Save davidplappert/39b17324670b205c2c2aff397c952e23 to your computer and use it in GitHub Desktop.

Select an option

Save davidplappert/39b17324670b205c2c2aff397c952e23 to your computer and use it in GitHub Desktop.
Jenkins job to update and maintain cloudwatch alerts with cloudwatch meterics
#!/usr/bin/env ruby
require 'aws-sdk'
require 'json'
require 'curb'
## cw-processHeartbeat user
AWS_ACCESS_KEY_ID = '*****'
AWS_SECRET_ACCESS_KEY = '*****'
MINUTES = '5'
## http://docs.aws.amazon.com/sdkforruby/api/Aws/CloudWatch.html
CLIENT = Aws::CloudWatch::Client.new(
access_key_id: AWS_ACCESS_KEY_ID,
secret_access_key: AWS_SECRET_ACCESS_KEY,
region: 'us-east-1'
)
next_token = nil
## Make sure every metric has an alarm and that it is built correctly
100.times do
## http://docs.aws.amazon.com/sdkforruby/api/Aws/CloudWatch/Client.html#list_metrics-instance_method
metrics = CLIENT.list_metrics({
namespace: "Process Heartbeats",
next_token: next_token
})
metrics.metrics.each do |metric|
p metric.metric_name
if metric.metric_name.split('-').first == "prod"
sns = '*****'
else
sns = '*****'
end
## Fix me, only update metric if needs updating
update_metric = CLIENT.put_metric_alarm({
alarm_name: metric.metric_name,
metric_name: metric.metric_name,
namespace: 'Process Heartbeats',
period: 60,
evaluation_periods: MINUTES,
comparison_operator: 'LessThanThreshold',
threshold: 1.0,
actions_enabled: true,
unit: "Count",
ok_actions: [ "#{sns}" ],
alarm_actions: [ "#{sns}" ],
insufficient_data_actions: [ "#{sns}" ],
statistic: 'Minimum',
})
sleep 1
end
next_token = metrics.next_token
break if next_token.nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment