bin/check-instance-events.rb in sensu-plugins-aws-0.0.4 vs bin/check-instance-events.rb in sensu-plugins-aws-1.0.0

- old
+ new

@@ -11,11 +11,11 @@ # # PLATFORMS: # Linux # # DEPENDENCIES: -# gem: aws-sdk +# gem: aws-sdk-v1 # gem: sensu-plugin # # USAGE: # #YELLOW # @@ -32,13 +32,25 @@ class CheckInstanceEvents < 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_ID'] or provide it as an option", - default: ENV['AWS_ACCESS_KEY_ID'] + 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'] + + option :aws_region, + short: '-r AWS_REGION', + long: '--aws-region REGION', + description: 'AWS Region (defaults to us-east-1).', + default: 'us-east-1' + option :use_iam_role, short: '-u', long: '--use-iam', description: 'Use IAM role authenticiation. Instance must have IAM role assigned for this to work' @@ -46,28 +58,15 @@ short: '-n', long: '--include-name', description: "Includes any offending instance's 'Name' tag in the check output", default: false - option :aws_secret_access_key, - short: '-s 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", - default: ENV['AWS_SECRET_ACCESS_KEY'] - - option :aws_region, - short: '-r AWS_REGION', - long: '--aws-region REGION', - description: 'AWS Region (such as eu-west-1).', - default: 'us-east-1' - def aws_config - hash = {} - hash.update access_key_id: config[:aws_access_key], secret_access_key: config[:aws_secret_access_key]\ - if config[:aws_access_key] && config[:aws_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 event_instances = [] aws_config = {} @@ -79,25 +78,25 @@ ) end ec2 = AWS::EC2::Client.new(aws_config.merge!(region: config[:aws_region])) begin - ec2.describe_instance_status[:instance_status_set].each do |i| # rubocop:disable Next - unless i[:events_set].empty? - # Exclude completed reboots since the events API appearently returns these even after they have been completed: - # Example: - # "events_set": [ - # { - # "code": "system-reboot", - # "description": "[Completed] Scheduled reboot", - # "not_before": "2015-01-05 12:00:00 UTC", - # "not_after": "2015-01-05 18:00:00 UTC" - # } - # ] - useful_events = i[:events_set].reject { |x| x[:code] == 'system-reboot' && x[:description] =~ /\[Completed\]/ } - unless useful_events.empty? - event_instances << i[:instance_id] - end + ec2.describe_instance_status[:instance_status_set].each do |i| + next if i[:events_set].empty? + + # Exclude completed reboots since the events API appearently returns these even after they have been completed: + # Example: + # "events_set": [ + # { + # "code": "system-reboot", + # "description": "[Completed] Scheduled reboot", + # "not_before": "2015-01-05 12:00:00 UTC", + # "not_after": "2015-01-05 18:00:00 UTC" + # } + # ] + useful_events = i[:events_set].reject { |x| x[:code] == 'system-reboot' && x[:description] =~ /\[Completed\]/ } + unless useful_events.empty? + event_instances << i[:instance_id] end end rescue => e unknown "An error occurred processing AWS EC2 API: #{e.message}" end