-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update check-elb-sum-requests.rb to AWS SDK v2 #297
Changes from 2 commits
b41ccc2
d8cfd56
43bf2a3
54a5b0d
fdb438a
d11423d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
# Linux | ||
# | ||
# DEPENDENCIES: | ||
# gem: aws-sdk-v1 | ||
# gem: aws-sdk | ||
# gem: sensu-plugin | ||
# | ||
# USAGE: | ||
|
@@ -31,20 +31,11 @@ | |
# | ||
|
||
require 'sensu-plugin/check/cli' | ||
require 'aws-sdk-v1' | ||
require 'sensu-plugins-aws' | ||
require 'aws-sdk' | ||
|
||
class CheckELBSumRequests < Sensu::Plugin::Check::CLI | ||
option :aws_access_key, | ||
short: '-a AWS_ACCESS_KEY', | ||
long: '--aws-access-key AWS_ACCESS_KEY', | ||
description: "AWS Access Key. Either set ENV['AWS_ACCESS_KEY'] or provide it as an option", | ||
default: ENV['AWS_ACCESS_KEY'] | ||
|
||
option :aws_secret_access_key, | ||
short: '-k AWS_SECRET_KEY', | ||
long: '--aws-secret-access-key AWS_SECRET_KEY', | ||
description: "AWS Secret Access Key. Either set ENV['AWS_SECRET_KEY'] or provide it as an option", | ||
default: ENV['AWS_SECRET_KEY'] | ||
include Common | ||
|
||
option :aws_region, | ||
short: '-r AWS_REGION', | ||
|
@@ -79,42 +70,40 @@ class CheckELBSumRequests < Sensu::Plugin::Check::CLI | |
description: "Trigger a #{severity} if sum requests is over specified count" | ||
end | ||
|
||
def aws_config | ||
{ access_key_id: config[:aws_access_key], | ||
secret_access_key: config[:aws_secret_access_key], | ||
region: config[:aws_region] } | ||
end | ||
|
||
def elb | ||
@elb ||= AWS::ELB.new aws_config | ||
@elb ||= Aws::ElasticLoadBalancing::Client.new(aws_config) | ||
end | ||
|
||
def cloud_watch | ||
@cloud_watch ||= AWS::CloudWatch.new aws_config | ||
@cloud_watch ||= Aws::CloudWatch::Client.new | ||
end | ||
|
||
def elbs | ||
return @elbs if @elbs | ||
@elbs = elb.load_balancers.to_a | ||
@elbs.select! { |elb| config[:elb_names].include? elb.name } if config[:elb_names] | ||
@elbs = elb.describe_load_balancers.load_balancer_descriptions.to_a | ||
@elbs.select! { |elb| config[:elb_names].include? elb.load_balancer_name } if config[:elb_names] | ||
@elbs | ||
end | ||
|
||
def latency_metric(elb_name) | ||
cloud_watch.metrics.with_namespace('AWS/ELB').with_metric_name('RequestCount').with_dimensions(name: 'LoadBalancerName', value: elb_name).first | ||
end | ||
|
||
def statistics_options | ||
{ | ||
def request_count_metric(elb_name) | ||
cloud_watch.get_metric_statistics( | ||
namespace: 'AWS/ELB', | ||
metric_name: 'RequestCount', | ||
dimensions: [ | ||
{ | ||
name: 'LoadBalancerName', | ||
value: elb_name | ||
} | ||
], | ||
start_time: config[:end_time] - config[:period], | ||
end_time: config[:end_time], | ||
end_time: config[:end_time], | ||
statistics: ['Sum'], | ||
period: config[:period] | ||
} | ||
period: config[:period] | ||
) | ||
end | ||
|
||
def latest_value(metric) | ||
metric.statistics(statistics_options.merge(unit: 'Count')).datapoints.sort_by { |datapoint| datapoint[:timestamp] }.last[:sum] | ||
metric.datapoints.sort_by { |datapoint| datapoint[:timestamp] }.last[:sum] | ||
end | ||
|
||
def flag_alert(severity, message) | ||
|
@@ -123,7 +112,7 @@ def flag_alert(severity, message) | |
end | ||
|
||
def check_sum_requests(elb) | ||
metric = latency_metric elb.name | ||
metric = request_count_metric elb.load_balancer_name | ||
metric_value = begin | ||
latest_value metric | ||
rescue StandardError | ||
|
@@ -132,7 +121,6 @@ def check_sum_requests(elb) | |
|
||
@severities.each_key do |severity| | ||
threshold = config[:"#{severity}_over"] | ||
puts metric_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this being removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed this because it looks like a debug statement that was left and forgot. See example output:
|
||
next unless threshold | ||
next if metric_value < threshold | ||
flag_alert severity, | ||
|
@@ -142,11 +130,7 @@ def check_sum_requests(elb) | |
end | ||
|
||
def run | ||
@message = if elbs.size == 1 | ||
elbs.first.inspect | ||
else | ||
"#{elbs.size} load balancers total" | ||
end | ||
@message = "#{elbs.size} load balancers total" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are removing the check to be grammatically correct we should add a parenthesis around the plural form
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noted, will change it, thanks. |
||
|
||
@severities = { | ||
critical: false, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd split these up into two entries but I can do that for you. Basically the removal of the CLI args are the breaking change aside from that upgrading to v2 should be backwards compatible and that should go under
### Changed
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done