Sha256: b8748fd9bf9e66ed9fc432495b356e28510d69d0f5be8204d08523ed22d855c0

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

#!/usr/bin/env ruby
#
# Send notifications to an SNS topic
#
# Requires the aws-sdk gem.
#
# See README for usage information
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details

require 'sensu-handler'
require 'aws-sdk'
require 'erubis'

class SnsNotifier < Sensu::Handler
  def topic_arn
    settings['sns']['topic_arn']
  end

  def region
    settings['sns']['region'] || 'us-east-1'
  end

  def event_name
    "#{@event['client']['name']}/#{@event['check']['name']}"
  end

  def message
    template = if template_file && File.readable?(template_file)
                 File.read(template_file)
               else
                 <<-BODY.gsub(/^\s+/, '')
        <%= @event['check']['notification'] || @event['check']['output'] %>
      BODY
               end
    eruby = Erubis::Eruby.new(template)
    eruby.result(binding)
  end

  def template_file
    settings['sns']['template_file']
  end

  def handle
    sns = Aws::SNS::Client.new(region: region)

    subject = if @event['action'].eql?('resolve')
                "RESOLVED - [#{event_name}]"
              else
                "ALERT - [#{event_name}]"
              end

    options = {
      subject: subject,
      message: "#{subject} - #{message}",
      topic_arn: topic_arn
    }

    sns.publish(options)
  rescue => e
    puts "Exception occured in SnsNotifier: #{e.message}", e.backtrace
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sensu-plugins-aws-10.1.1 bin/handler-sns.rb
sensu-plugins-aws-10.1.0 bin/handler-sns.rb
sensu-plugins-aws-10.0.3 bin/handler-sns.rb
sensu-plugins-aws-10.0.2 bin/handler-sns.rb
sensu-plugins-aws-10.0.1 bin/handler-sns.rb
sensu-plugins-aws-10.0.0 bin/handler-sns.rb