Skip to content

Commit

Permalink
Update check-elb-latency.rb to AWS SDK v2 (#296)
Browse files Browse the repository at this point in the history
* Update check-elb-latency.rb to AWS SDK v2

* Update changelog

* code style

* Revert removal of double pipes

* Update based on feedbacks

* [ci skip] fix typo
  • Loading branch information
boutetnico authored and majormoses committed Nov 2, 2018
1 parent 8d45e92 commit 96a06b0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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-latency.rb` no longer takes `aws_access_key` and `aws_secret_access_key` options. (@boutetnico)
### Changed
- `check-elb-latency.rb` was updated to aws-sdk v2. (@boutetnico)

## [14.0.0] - 2018-11-01
### Breaking Changes
Expand Down
62 changes: 26 additions & 36 deletions bin/check-elb-latency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
#
# 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
Expand All @@ -13,7 +13,7 @@
# Linux
#
# DEPENDENCIES:
# gem: aws-sdk-v1
# gem: aws-sdk
# gem: sensu-plugin
#
# USAGE:
Expand All @@ -32,20 +32,11 @@
#

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']

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',
Expand Down Expand Up @@ -87,42 +78,41 @@ class CheckELBLatency < Sensu::Plugin::Check::CLI
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)
Expand All @@ -131,7 +121,7 @@ def flag_alert(severity, 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
Expand All @@ -143,14 +133,14 @@ def check_latency(elb)
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
Expand Down

0 comments on commit 96a06b0

Please sign in to comment.