Skip to content

Commit

Permalink
Added new notification handler for sending SMS via clockwork API
Browse files Browse the repository at this point in the history
  • Loading branch information
dgolja committed Apr 24, 2014
1 parent 847b1ed commit e599505
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
17 changes: 17 additions & 0 deletions handlers/notification/clockworksms.json
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"
]
}
}
}

67 changes: 67 additions & 0 deletions handlers/notification/clockworksms.rb
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

0 comments on commit e599505

Please sign in to comment.