bin/check-instance-events.rb in sensu-plugins-aws-3.2.1 vs bin/check-instance-events.rb in sensu-plugins-aws-4.0.0

- old
+ new

@@ -26,11 +26,11 @@ # Released under the same terms as Sensu (the MIT license); see LICENSE # for details. # require 'sensu-plugin/check/cli' -require 'aws-sdk-v1' +require 'aws-sdk' class CheckInstanceEvents < Sensu::Plugin::Check::CLI option :aws_access_key, short: '-a AWS_ACCESS_KEY', long: '--aws-access-key AWS_ACCESS_KEY', @@ -72,14 +72,11 @@ secret_access_key: config[:aws_secret_access_key], region: config[:aws_region] } end def ec2_regions - # This is for SDK v2 - # Aws.partition('aws').regions.map(&:name) - - AWS::EC2.regions.map(&:name) + Aws.partition('aws').regions.map(&:name) end def run event_instances = [] aws_config = {} @@ -98,19 +95,19 @@ aws_config[:access_key_id] = config[:aws_access_key] aws_config[:secret_access_key] = config[:aws_secret_access_key] end aws_regions.each do |r| # Iterate each possible region - ec2 = AWS::EC2::Client.new(aws_config.merge!(region: r)) + ec2 = Aws::EC2::Client.new(aws_config.merge!(region: r)) begin describe_instance_options = {} if config[:instance_id].any? describe_instance_options = describe_instance_options.merge(instance_ids: config[:instance_id]) end - ec2.describe_instance_status(describe_instance_options)[:instance_status_set].each do |i| - next if i[:events_set].empty? + ec2.describe_instance_status(describe_instance_options).instance_statuses.each do |i| + next if i[:events].empty? # Exclude completed reboots since the events API appearently returns these even after they have been completed: # Example: # "events_set": [ # { @@ -119,23 +116,23 @@ # "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|instance-stop|system-maintenance/) && (x[:description] =~ /\[Completed\]|\[Canceled\]/) } + i[:events].reject { |x| (x[:code] =~ /system-reboot|instance-reboot|instance-stop|system-maintenance/) && (x[:description] =~ /\[Completed\]|\[Canceled\]/) } unless useful_events.empty? if config[:include_name] name = '' begin instance_desc = ec2.describe_instances(instance_ids: [i[:instance_id]]) name = instance_desc[:reservation_index][i[:instance_id]][:instances_set][0][:tag_set].select { |tag| tag[:key] == 'Name' }[0][:value] rescue => e puts "Issue getting instance details for #{i[:instance_id]} (#{r}). Exception = #{e}" end - event_instances << "#{name} (#{i[:instance_id]} #{r}) (#{i[:events_set][0][:code]}) #{i[:events_set][0][:description]}" + event_instances << "#{name} (#{i[:instance_id]} #{r}) (#{i[:events][0][:code]}) #{i[:events][0][:description]}" else - event_instances << "#{i[:instance_id]} (#{r}) (#{i[:events_set][0][:code]}) #{i[:events_set][0][:description]}" + event_instances << "#{i[:instance_id]} (#{r}) (#{i[:events][0][:code]}) #{i[:events][0][:description]}" end end end rescue => e unknown "An error occurred processing AWS EC2 API (#{r}): #{e.message}"