bin/handler-ec2_node.rb in sensu-plugins-aws-3.0.0 vs bin/handler-ec2_node.rb in sensu-plugins-aws-3.1.0

- old
+ new

@@ -1,9 +1,11 @@ #!/usr/bin/env ruby # # CHANGELOG: # * 0.7.0: +# - Added method instance_id to check in client config section +# ['client']['aws']['instance_id'] first. # - Update to new API event naming and simplifying ec2_node_should_be_deleted method and fixing # match that will work with any user state defined. # * 0.6.0: # - Fixed ec2_node_should_be_deleted to account for an empty insances array # * 0.5.0: @@ -138,30 +140,42 @@ # Call ec2_node_should_be_deleted method and check for instance state and if valid delete from the sensu API otherwise # instance is in invalid state if ec2_node_should_be_deleted? delete_sensu_client! else - puts "[EC2 Node] #{@event['client']['name']} is in an invalid state" + puts "[EC2 Node] #{instance_id} is in an invalid state" end end # Method to delete client from sensu API def delete_sensu_client! response = api_request(:DELETE, '/clients/' + @event['client']['name']).code deletion_status(response) end + def instance_id + if @event['client'].key?('aws') && @event['client']['aws'].key?('instance_id') + @event['client']['aws']['instance_id'] + else + @event['client']['name'] + end + end + + def instance_name + @event['client']['name'] + end + # Method to check if there is any insance and if instance is in a valid state that could be deleted def ec2_node_should_be_deleted? # Defining region for aws SDK object ec2 = Aws::EC2::Client.new(region: region) settings['ec2_node'] = {} unless settings['ec2_node'] instance_states = @event['client']['ec2_states'] || settings['ec2_node']['ec2_states'] || ['shutting-down', 'terminated', 'stopping', 'stopped'] instance_reasons = @event['client']['ec2_state_reasons'] || settings['ec2_node']['ec2_state_reasons'] || %w(Client.UserInitiatedShutdown Server.SpotInstanceTermination Client.InstanceInitiatedShutdown) begin # Finding the instance - instances = ec2.describe_instances(instance_ids: [@event['client']['name']]).reservations[0] + instances = ec2.describe_instances(instance_ids: [instance_id]).reservations[0] # If instance is empty/nil instance id is not valid so client can be deleted if instances.nil? || instances.empty? true else # Checking for instance state and reason, and if matches any of the user defined or default reasons then