From b41ccc29d4848c36e79aaee3c55cd549c94a092b Mon Sep 17 00:00:00 2001 From: Nicolas Boutet Date: Wed, 24 Oct 2018 17:36:08 +0800 Subject: [PATCH 1/6] Update check-elb-sum-requests.rb to AWS SDK v2 --- CHANGELOG.md | 4 ++- bin/check-elb-sum-requests.rb | 64 +++++++++++++---------------------- 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f86eb4e..d0b06b49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md) ## [Unreleased] +### Breaking Changes +- `check-elb-sum-requests.rb` was updated to aws-sdk v2 and no longer take `aws_access_key` and `aws_secret_access_key` options. (@boutetnico) ## [12.4.0] - 2018-10-03 ### Changed @@ -19,7 +21,7 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins ## [12.1.0] - 2018-08-28 ### Added -- new `check-efs.rb: checks cloudwatch metrics with the efs namespace for an arbitrary metric (@ivanfetch) +- new `check-efs.rb`: checks cloudwatch metrics with the efs namespace for an arbitrary metric (@ivanfetch) ## [12.0.0] - 2018-06-21 ### Breaking Changes diff --git a/bin/check-elb-sum-requests.rb b/bin/check-elb-sum-requests.rb index 20014877..3f91d4c3 100755 --- a/bin/check-elb-sum-requests.rb +++ b/bin/check-elb-sum-requests.rb @@ -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 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" @severities = { critical: false, From d8cfd566a2ac4f73c4a6ded49dc76ec2c0af0150 Mon Sep 17 00:00:00 2001 From: Nicolas Boutet Date: Wed, 24 Oct 2018 18:47:06 +0800 Subject: [PATCH 2/6] Revert removal of double pipes --- bin/check-elb-sum-requests.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/check-elb-sum-requests.rb b/bin/check-elb-sum-requests.rb index 3f91d4c3..cfde383c 100755 --- a/bin/check-elb-sum-requests.rb +++ b/bin/check-elb-sum-requests.rb @@ -71,11 +71,11 @@ class CheckELBSumRequests < Sensu::Plugin::Check::CLI end def elb - @elb = Aws::ElasticLoadBalancing::Client.new(aws_config) + @elb ||= Aws::ElasticLoadBalancing::Client.new(aws_config) end def cloud_watch - @cloud_watch = Aws::CloudWatch::Client.new + @cloud_watch ||= Aws::CloudWatch::Client.new end def elbs From 43bf2a38fac763d7d80c50380aaee673d48826f5 Mon Sep 17 00:00:00 2001 From: Nicolas Boutet Date: Sun, 28 Oct 2018 16:18:59 +0800 Subject: [PATCH 3/6] Update based on feedbacks --- CHANGELOG.md | 5 ++++- bin/check-elb-sum-requests.rb | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0b06b49..1f84f559 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,10 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins ## [Unreleased] ### Breaking Changes -- `check-elb-sum-requests.rb` was updated to aws-sdk v2 and no longer take `aws_access_key` and `aws_secret_access_key` options. (@boutetnico) +- `check-elb-sum-requests.rb` no longer take `aws_access_key` and `aws_secret_access_key` options. (@boutetnico) + +### Changed +- `check-elb-sum-requests.rb` was updated to aws-sdk v2. (@boutetnico) ## [12.4.0] - 2018-10-03 ### Changed diff --git a/bin/check-elb-sum-requests.rb b/bin/check-elb-sum-requests.rb index cfde383c..50525797 100755 --- a/bin/check-elb-sum-requests.rb +++ b/bin/check-elb-sum-requests.rb @@ -130,7 +130,11 @@ def check_sum_requests(elb) end def run - @message = "#{elbs.size} load balancers total" + @message = if elbs.size == 1 + elbs.first.load_balancer_name + else + "#{elbs.size} load balancers total" + end @severities = { critical: false, From 54a5b0d65205cc439b11e894840df09c151f0a3c Mon Sep 17 00:00:00 2001 From: Nicolas Boutet Date: Sun, 28 Oct 2018 16:49:50 +0800 Subject: [PATCH 4/6] Fix displaying name of faulty LB --- bin/check-elb-sum-requests.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/check-elb-sum-requests.rb b/bin/check-elb-sum-requests.rb index 50525797..696316cc 100755 --- a/bin/check-elb-sum-requests.rb +++ b/bin/check-elb-sum-requests.rb @@ -124,7 +124,7 @@ def check_sum_requests(elb) next unless threshold next if metric_value < threshold flag_alert severity, - "; #{elbs.size == 1 ? nil : "#{elb.inspect}'s"} Sum Requests is #{metric_value}. (expected lower than #{threshold})" + "; #{elbs.size == 1 ? nil : "#{elb.load_balancer_name}'s"} Sum Requests is #{metric_value}. (expected lower than #{threshold})" break end end From fdb438a72bdf344dda4c9a4ed0cee135791decd2 Mon Sep 17 00:00:00 2001 From: Nicolas Boutet Date: Sun, 28 Oct 2018 17:02:51 +0800 Subject: [PATCH 5/6] fix broken output using non-existing variable --- bin/check-elb-sum-requests.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/check-elb-sum-requests.rb b/bin/check-elb-sum-requests.rb index 696316cc..6d80f130 100755 --- a/bin/check-elb-sum-requests.rb +++ b/bin/check-elb-sum-requests.rb @@ -143,7 +143,7 @@ def run elbs.each { |elb| check_sum_requests elb } - @message += "; (#{config[:statistics].to_s.capitalize} within #{config[:period]} seconds " + @message += "; (Sum within #{config[:period]} seconds " @message += "between #{config[:end_time] - config[:period]} to #{config[:end_time]})" if @severities[:critical] From d11423d0a777dd06d838d6d49613bcd0013374a0 Mon Sep 17 00:00:00 2001 From: Nicolas Boutet Date: Sun, 28 Oct 2018 17:21:46 +0800 Subject: [PATCH 6/6] [ci skip] fix typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f84f559..bd8b2711 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins ## [Unreleased] ### Breaking Changes -- `check-elb-sum-requests.rb` no longer take `aws_access_key` and `aws_secret_access_key` options. (@boutetnico) +- `check-elb-sum-requests.rb` no longer takes `aws_access_key` and `aws_secret_access_key` options. (@boutetnico) ### Changed - `check-elb-sum-requests.rb` was updated to aws-sdk v2. (@boutetnico)