bin/check-elb-latency.rb in sensu-plugins-aws-14.0.0 vs bin/check-elb-latency.rb in sensu-plugins-aws-15.0.0

- old
+ new

@@ -2,20 +2,20 @@ # # check-elb-latency # # # DESCRIPTION: -# This plugin checks the health of an Amazon Elastic Load Balancer. +# This plugin checks the latency of an Amazon Elastic Load Balancer. # # OUTPUT: # plain-text # # PLATFORMS: # Linux # # DEPENDENCIES: -# gem: aws-sdk-v1 +# gem: aws-sdk # gem: sensu-plugin # # USAGE: # Warning if any load balancer's latency is over 1 second, critical if over 3 seconds. # check-elb-latency --warning-over 1 --critical-over 3 @@ -30,25 +30,16 @@ # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. # require 'sensu-plugin/check/cli' -require 'aws-sdk-v1' +require 'sensu-plugins-aws' +require 'aws-sdk' class CheckELBLatency < 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'] + include Common - 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'] - option :aws_region, short: '-r AWS_REGION', long: '--aws-region REGION', description: 'AWS Region (defaults to us-east-1).', default: 'us-east-1' @@ -85,55 +76,54 @@ long: "--#{severity}-over SECONDS", proc: proc(&:to_f), description: "Trigger a #{severity} if latancy is over specified seconds" 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('Latency').with_dimensions(name: 'LoadBalancerName', value: elb_name).first - end - - def statistics_options - { + cloud_watch.get_metric_statistics( + namespace: 'AWS/ELB', + metric_name: 'Latency', + dimensions: [ + { + name: 'LoadBalancerName', + value: elb_name + } + ], start_time: config[:end_time] - config[:period], - end_time: config[:end_time], + end_time: config[:end_time], statistics: [config[:statistics].to_s.capitalize], - period: config[:period] - } + period: config[:period], + unit: 'Seconds' + ) end def latest_value(metric) - metric.statistics(statistics_options.merge(unit: 'Seconds')).datapoints.sort_by { |datapoint| datapoint[:timestamp] }.last[config[:statistics]] + metric.datapoints.sort_by { |datapoint| datapoint[:timestamp] }.last[config[:statistics]] end def flag_alert(severity, message) @severities[severity] = true @message += message end def check_latency(elb) - metric = latency_metric elb.name + metric = latency_metric elb.load_balancer_name metric_value = begin latest_value metric rescue StandardError 0 end @@ -141,17 +131,17 @@ @severities.each_key do |severity| threshold = config[:"#{severity}_over"] next unless threshold next if metric_value < threshold flag_alert severity, - "; #{elbs.size == 1 ? nil : "#{elb.inspect}'s"} Latency is #{sprintf '%.3f', metric_value} seconds. (expected lower than #{sprintf '%.3f', threshold})" + "; #{elbs.size == 1 ? nil : "#{elb.load_balancer_name}'s"} Latency is #{sprintf '%.3f', metric_value} seconds. (expected lower than #{sprintf '%.3f', threshold})" break end end def run @message = if elbs.size == 1 - elbs.first.inspect + elbs.first.load_balancer_name else "#{elbs.size} load balancers total" end @severities = {