· 9 years ago · Oct 05, 2016, 03:48 PM
1{"timestamp":"2016-10-05T15:36:49.477967+0000","level":"info","message":"handler output","handler":{"topic_arn":"arn:aws:sns:us-west-2:xssxxx","region":"us-west-2","use_ami_role":"true","access_key":"xxxxx","secret_key":"xxxxx","type":"pipe","command":"/opt/sensu/embedded/bin/handler-sns.rb","name":"sns"},"output":["Exception occured in SnsNotifier: undefined method `[]' for nil:NilClass\n/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-aws-3.2.1/bin/handler-sns.rb:34:in `use_ami_role'\n/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-aws-3.2.1/bin/handler-sns.rb:63:in `handle'\n/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugin-1.4.2/lib/sensu-handler.rb:81:in `block in <class:Handler>'\n"]}
2
3
4
5
6
7
8ubuntu@ip-172-31-2-150:/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-aws-3.2.1/bin$ sudo more /etc/sensu/conf.d/handler_sns.json
9{
10 "mailer-sns": {
11 "topic_arn": "arn:aws:sns:usxxxxxxx",
12 "region": "us-west-2",
13 "use_ami_role": true,
14 "access_key": "xxxxxxx",
15 "secret_key": "xxxxx"
16 },
17 "handlers": {
18 "sns": {
19 "topic_arn": "arn:aws:sns:usxxxxxxx",
20 "region": "us-west-2",
21 "use_ami_role": true,
22 "access_key": "xxxx",
23 "secret_key": "xxxxx",
24 "type": "pipe",
25 "command": "/opt/sensu/embedded/bin/handler-sns.rb"
26 }
27 }
28}
29
30
31
32
33
34ubuntu@ip-172-31-2-150:/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-aws-3.2.1/bin$ more /opt/sensu/embedded/bin/handler-sns.rb
35#!/opt/sensu/embedded/bin/ruby
36#
37# This file was generated by RubyGems.
38#
39# The application 'sensu-plugins-aws' is installed as part of a gem, and
40# this file is here to facilitate running it.
41#
42
43require 'rubygems'
44
45version = ">= 0.a"
46
47if ARGV.first
48 str = ARGV.first
49 str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
50 if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
51 version = $1
52 ARGV.shift
53 end
54end
55
56load Gem.activate_bin_path('sensu-plugins-aws', 'handler-sns.rb', version)
57
58
59
60
61
62
63
64
65ubuntu@ip-172-31-2-150:/opt/sensu/embedded/lib/ruby/gems/2.3.0/gems/sensu-plugins-aws-3.2.1/bin$ more handler-sns.rb
66#!/usr/bin/env ruby
67#
68# This handler assumes it runs on an ec2 instance with an iam role
69# that has permission to send to the sns topic specified in the config.
70# This removes the requirement to specify an access key and secret access key.
71# See http://docs.aws.amazon.com/IAM/latest/UserGuide/WorkingWithRoles.html
72#
73# Requires the aws-sdk gem.
74#
75# Setting required in sns.json
76# topic_are : The arn for the destination sns topic
77#
78# Released under the same terms as Sensu (the MIT license); see LICENSE
79# for details
80
81require 'sensu-handler'
82require 'aws-sdk-v1'
83require 'erubis'
84
85class SnsNotifier < Sensu::Handler
86 def topic_arn
87 settings['sns']['topic_arn']
88 end
89
90 def region
91 settings['sns']['region'] || 'us-east-1'
92 end
93
94 def event_name
95 "#{@event['client']['name']}/#{@event['check']['name']}"
96 end
97
98 def use_ami_role
99 use_ami_role = settings['sns']['use_ami_role']
100 use_ami_role.nil? ? true : use_ami_role
101 end
102
103 def aws_access_key
104 settings['sns']['access_key'] || ''
105 end
106
107 def aws_access_secret
108 settings['sns']['secret_key'] || ''
109 end
110
111 def message
112 template = if template_file && File.readable?(template_file)
113 File.read(template_file)
114 else
115 <<-BODY.gsub(/^\s+/, '')
116 <%= @event['check']['notification'] || @event['check']['output'] %>
117 BODY
118 end
119 eruby = Erubis::Eruby.new(template)
120 eruby.result(binding)
121 end
122
123 def template_file
124 settings['sns']['template_file']
125 end
126
127 def handle
128 if use_ami_role
129 AWS.config(region: region)
130 else
131 AWS.config(access_key_id: aws_access_key,
132 secret_access_key: aws_access_secret,
133 region: region)
134 end
135
136 sns = AWS::SNS.new
137
138 t = sns.topics[topic_arn]
139
140 subject = if @event['action'].eql?('resolve')
141 "RESOLVED - [#{event_name}]"
142 else
143 "ALERT - [#{event_name}]"
144 end
145 options = { subject: subject }
146 t.publish("#{subject} - #{message}", options)
147 rescue => e
148 puts "Exception occured in SnsNotifier: #{e.message}", e.backtrace
149 end
150end