Skip to content

Instantly share code, notes, and snippets.

@yureative
Last active October 8, 2015 11:26
Show Gist options
  • Save yureative/d73a80b94ccf045bd574 to your computer and use it in GitHub Desktop.
Save yureative/d73a80b94ccf045bd574 to your computer and use it in GitHub Desktop.
A sensu mutator to add EC2 instance's tags to event data
#!/usr/bin/env ruby
require 'json'
require 'aws-sdk-core'
module Sensu::Extension
class EC2Tags < Mutator
def name
'ec2_tags'
end
def description
'Add EC2 instance\'s tags'
end
def post_init
@aws_region = settings['aws']['region']
end
def run(event)
mutated =
if event[:client][:name] =~ /^i-[0-9a-z]+/i
# If the client is EC2 instance, fetch its tags and add those to the event json
resp = Aws::EC2::Client.new(region: @aws_region).describe_instances({instance_ids: [event[:client][:name]]})
instances = resp.reservations.map { |r| r.instances }.flatten
if instances.length > 0
tags = instances.first.tags.reduce({}) { |a, b| a.merge({ b.key => b.value }) } # => e.g. { 'Name' => 'prd-example-web', 'Role' => ... }
event.merge(client: {ec2: {tags: tags}})
else
event
end
else
event
end
yield(JSON.dump(mutated), 0)
end
end
end
@yureative
Copy link
Author

/sensu/extensions/mutators/ec2_tags.rb

{
  "handlers": {
    "pagerduty": {
      "type": "pipe",
      "command": "/etc/sensu/handlers/pagerduty.rb",
      "mutator": "ec2_tags"
    },
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment