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

- old
+ new

@@ -1,12 +1,13 @@ #!/usr/bin/env ruby # # CHANGELOG: +# * 0.8.0: +# - Added support to use ec2_region from client definition # * 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 +# - 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: # - Adds configuration to filter by state reason @@ -59,12 +60,33 @@ # If this is not the case, you can either sub-class this handler and override # `ec2_node_should_be_deleted?` in your own organization-specific handler, or modify this # handler to suit your needs. # # -# Optional a Sensu configuration snippet: +# A Sensu Client configuration using the ec2_region attribute: # { +# "client": { +# "name": "i-424242", +# "address": "127.0.0.1", +# "ec2_region": "eu-west-1", +# "subscriptions": ["all"] +# } +# } +# or embeded in the ec2 block +# { +# "client": { +# "name": "i-424242", +# "address": "127.0.0.1", +# "ec2" : { +# "region": "eu-west-1" +# }, +# "subscriptions": ["all"] +# } +# } +# +# Or a Sensu Server configuration snippet: +# { # "aws": { # "access_key": "adsafdafda", # "secret_key": "qwuieohajladsafhj23nm", # "region": "us-east-1c" # } @@ -76,11 +98,11 @@ # - EC2_REGION # # If none of the settings are found it will then attempt to # generate temporary credentials from the IAM instance profile # -# If region is not specified in either of the above 2 mechanisms +# If region is not specified in either of the above 3 mechanisms # we will make a request for the EC2 instances current region. # # To use, you can set it as the keepalive handler for a client: # { # "client": { @@ -151,18 +173,10 @@ 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? @@ -196,10 +210,12 @@ end def region @region ||= begin region_check = ENV['EC2_REGION'] - region_check = settings['aws']['region'] if settings.key? 'aws' + region_check = settings['aws']['region'] if settings.key?('aws') + region_check = @event['client']['ec2_region'] if @event['client'].key?('ec2_region') + region_check = @event['client']['ec2']['region'] if @event['client'].key?('ec2') && @event['client']['ec2'].key?('region') if region_check.nil? || region_check.empty? region_check = Net::HTTP.get(URI('http://169.254.169.254/latest/meta-data/placement/availability-zone')) matches = /(\w+\-\w+\-\d+)/.match(region_check) if !matches.nil? && !matches.captures.empty? region_check = matches.captures[0]