forked from sensu/sensu-community-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new notification handler for sending SMS via clockwork API
- Loading branch information
Showing
2 changed files
with
84 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,17 @@ | ||
{ | ||
"clockworksms": { | ||
"key": "1234abc1esg1234abcdfdfg", | ||
"to": { | ||
"3864153012": [ | ||
"ok", | ||
"warning" | ||
], | ||
"61431324189": [ | ||
"ok", | ||
"critical", | ||
"warning" | ||
] | ||
} | ||
} | ||
} | ||
|
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,67 @@ | ||
#!/usr/bin/env ruby | ||
# | ||
# Sensu Handler: clockworksms | ||
# | ||
# This handler send SMS via clockworksms API based on the severity of the check result. | ||
# | ||
# Copyright 2014 Dejan Golja <[email protected]> | ||
# | ||
# 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 'clockwork' | ||
require 'timeout' | ||
|
||
class ClockWorkSmsNotif < Sensu::Handler | ||
|
||
def event_name | ||
@event['client']['name'] + '/' + @event['check']['name'] | ||
end | ||
|
||
def send_sms(to, content) | ||
content[157..content.length] = '...' if content.length > 160 | ||
message = @api.messages.build | ||
message.to = to | ||
message.content = content | ||
response = message.deliver | ||
|
||
unless response.success | ||
puts "#{response.error_code} - #{response.error_description}" | ||
end | ||
end | ||
|
||
def handle | ||
|
||
key = settings["clockworksms"]["key"] | ||
to = settings["clockworksms"]["to"] | ||
|
||
raise 'Please define a valid SMS key' if key.nil? | ||
raise 'Please define a valid set of SMS recipients to use this handler' if (to.nil? || !to.is_a?(Hash)) | ||
|
||
message = @event['check']['notification'] || @event['check']['output'] | ||
|
||
@api = Clockwork::API.new(key) | ||
|
||
to.each do |phone, severities| | ||
break unless severities.is_a?(Array) | ||
severities.map!(&:downcase) | ||
case @event['check']['status'] | ||
when 0 | ||
if severities.include?('ok') | ||
send_sms(phone, "OK-#{event_name} #{message}") | ||
end | ||
when 1 | ||
if severities.include?('warning') | ||
send_sms(phone, "WARN-#{event_name} #{message}") | ||
end | ||
when 2 | ||
if severities.include?('critical') | ||
send_sms(phone, "CRIT-#{event_name} #{message}") | ||
end | ||
end | ||
end | ||
end | ||
|
||
end |