Sha256: cc87a6d04bc1089eac25a36a2f52b28270305a53dae8dcf4db9a5293878c4f74

Contents?: true

Size: 1.39 KB

Versions: 41

Compression:

Stored size: 1.39 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 StandardError => e
    puts "Exception occured in SnsNotifier: #{e.message}", e.backtrace
  end
end

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
sensu-plugins-aws-18.6.0 bin/handler-sns.rb
sensu-plugins-aws-boutetnico-1.4.0 bin/handler-sns.rb
sensu-plugins-aws-boutetnico-1.3.4 bin/handler-sns.rb
sensu-plugins-aws-boutetnico-1.2.0 bin/handler-sns.rb
sensu-plugins-aws-boutetnico-1.1.1 bin/handler-sns.rb
sensu-plugins-aws-boutetnico-1.0.6 bin/handler-sns.rb
sensu-plugins-aws-18.5.0 bin/handler-sns.rb
sensu-plugins-aws-18.4.2 bin/handler-sns.rb
sensu-plugins-aws-18.4.1 bin/handler-sns.rb
sensu-plugins-aws-18.4.0 bin/handler-sns.rb
sensu-plugins-aws-18.3.0 bin/handler-sns.rb
sensu-plugins-aws-18.2.0 bin/handler-sns.rb
sensu-plugins-aws-18.1.0 bin/handler-sns.rb
sensu-plugins-aws-18.0.0 bin/handler-sns.rb
sensu-plugins-aws-17.2.0 bin/handler-sns.rb
sensu-plugins-aws-17.1.0 bin/handler-sns.rb
sensu-plugins-aws-17.0.0 bin/handler-sns.rb
sensu-plugins-aws-16.2.0 bin/handler-sns.rb
sensu-plugins-aws-16.1.0 bin/handler-sns.rb
sensu-plugins-aws-16.0.0 bin/handler-sns.rb