bin/metrics-ec2-count.rb in sensu-plugins-aws-0.0.4 vs bin/metrics-ec2-count.rb in sensu-plugins-aws-1.0.0

- old
+ new

@@ -1,20 +1,20 @@ #! /usr/bin/env ruby # # ec2-count-metrics # # DESCRIPTION: -# This plugin retrives number of EC2 status +# This plugin retrieves number of EC2 instances # # OUTPUT: # plain-text # # PLATFORMS: # Linux # # DEPENDENCIES: -# gem: aws-sdk +# gem: aws-sdk-v1 # gem: sensu-plugin # # USAGE: # #YELLOW # @@ -37,97 +37,99 @@ default: 'sensu.aws.ec2' 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_ID'] or provide it as an option" + 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_ACCESS_KEY', - long: '--aws-secret-access-key AWS_SECRET_ACCESS_KEY', - description: "AWS Secret Access Key. Either set ENV['AWS_SECRET_ACCESS_KEY'] or provide it as an option" + 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 (such as us-east-1).', + description: 'AWS Region (defaults to us-east-1).', default: 'us-east-1' option :type, short: '-t METRIC type', long: '--type METRIC type', description: 'Count by type: status, instance', default: 'instance' def aws_config - hash = {} - hash.update access_key_id: config[:access_key_id], secret_access_key: config[:secret_access_key] if config[:access_key_id] && config[:secret_access_key] - hash.update region: config[:aws_region] - hash + { access_key_id: config[:aws_access_key], + secret_access_key: config[:aws_secret_access_key], + region: config[:aws_region] + } end - def run - begin + def by_instances_status(client) + if config[:scheme] == 'sensu.aws.ec2' + config[:scheme] += '.count' + end - client = AWS::EC2::Client.new aws_config + options = { include_all_instances: true } + data = client.describe_instance_status(options) - def by_instances_status(client) - if config[:scheme] == 'sensu.aws.ec2' - config[:scheme] += '.count' - end + total = data[:instance_status_set].count + status = {} - options = { include_all_instances: true } - data = client.describe_instance_status(options) - - total = data[:instance_status_set].count - status = {} - - unless total.nil? - data[:instance_status_set].each do |value| - stat = value[:instance_state][:name] - if status[stat].nil? - status[stat] = 1 - else - status[stat] = status[stat] + 1 - end - end + unless total.nil? + data[:instance_status_set].each do |value| + stat = value[:instance_state][:name] + if status[stat].nil? + status[stat] = 1 + else + status[stat] = status[stat] + 1 end + end + end - unless data.nil? # rubocop: disable Style/GuardClause - # We only return data when we have some to return - output config[:scheme] + '.total', total - status.each do |name, count| - output config[:scheme] + ".#{name}", count - end - end + unless data.nil? # rubocop: disable Style/GuardClause + # We only return data when we have some to return + output config[:scheme] + '.total', total + status.each do |name, count| + output config[:scheme] + ".#{name}", count end + end + end - def by_instances_type(client) - if config[:scheme] == 'sensu.aws.ec2' - config[:scheme] += '.types' - end + def by_instances_type(client) + if config[:scheme] == 'sensu.aws.ec2' + config[:scheme] += '.types' + end - data = {} + data = {} - instances = client.describe_instances - instances[:reservation_set].each do |i| - i[:instances_set].each do |instance| - type = instance[:instance_type] - if data[type].nil? - data[type] = 1 - else - data[type] = data[type] + 1 - end - end + instances = client.describe_instances + instances[:reservation_set].each do |i| + i[:instances_set].each do |instance| + type = instance[:instance_type] + if data[type].nil? + data[type] = 1 + else + data[type] = data[type] + 1 end + end + end - unless data.nil? # rubocop: disable Style/GuardClause - # We only return data when we have some to return - data.each do |name, count| - output config[:scheme] + ".#{name}", count - end - end + unless data.nil? # rubocop: disable Style/GuardClause + # We only return data when we have some to return + data.each do |name, count| + output config[:scheme] + ".#{name}", count end + end + end + + def run + begin + + client = AWS::EC2::Client.new aws_config if config[:type] == 'instance' by_instances_type(client) elsif config[:type] == 'status' by_instances_status(client)