-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matt Jones
committed
Feb 16, 2015
1 parent
a99c124
commit 059bfaa
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"sns": { | ||
"topic_arn": "arn:aws:sns:us-east-1:111111111111:topic" | ||
,"use_ami_role": true | ||
,"access_key": "MY_KEY" | ||
,"secret_key": "MY_secret" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env ruby | ||
# | ||
# This handler assumes it runs on an ec2 instance with an iam role | ||
# that has permission to send to the sns topic specified in the config. | ||
# This removes the requirement to specify an access key and secret access key. | ||
# See http://docs.aws.amazon.com/IAM/latest/UserGuide/WorkingWithRoles.html | ||
# | ||
# Requires the aws-sdk gem. | ||
# | ||
# Setting required in sns.json | ||
# topic_are : The arn for the destination sns topic | ||
# | ||
# Released under the same terms as Sensu (the MIT license); see LICENSE | ||
# for details | ||
|
||
require 'rubygems' if RUBY_VERSION < '1.9.0' | ||
require 'sensu-handler' | ||
require 'aws-sdk' | ||
|
||
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 use_ami_role | ||
use_ami_role = settings['sns']['use_ami_role'] | ||
use_ami_role.nil? ? true : use_ami_role | ||
end | ||
|
||
def aws_access_key | ||
settings['sns']['access_key'] || '' | ||
end | ||
|
||
def aws_access_secret | ||
settings['sns']['secret_key'] || '' | ||
end | ||
|
||
def message | ||
@event['check']['notification'] || @event['check']['output'] | ||
end | ||
|
||
def handle | ||
if use_ami_role | ||
AWS.config(region: region) | ||
else | ||
AWS.config(access_key_id: aws_access_key, | ||
secret_access_key: aws_access_secret, | ||
region: region) | ||
end | ||
|
||
sns = AWS::SNS.new | ||
|
||
t = sns.topics[topic_arn] | ||
|
||
if @event['action'].eql?('resolve') | ||
subject = "RESOLVED - [#{event_name}]" | ||
options = { subject: subject } | ||
t.publish("#{subject} - #{message}", options) | ||
else | ||
subject = "ALERT - [#{event_name}]" | ||
options = { subject: subject } | ||
t.publish("#{subject} - #{message}", options) | ||
end | ||
rescue => e | ||
puts "Exception occured in SnsNotifier: #{e.message}", e.backtrace | ||
end | ||
end |