From e599505f4a472fbb6d4c73d64a089b2b703e77b3 Mon Sep 17 00:00:00 2001 From: Dejan Golja Date: Thu, 3 Apr 2014 21:22:23 +1100 Subject: [PATCH] Added new notification handler for sending SMS via clockwork API --- handlers/notification/clockworksms.json | 17 +++++++ handlers/notification/clockworksms.rb | 67 +++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 handlers/notification/clockworksms.json create mode 100644 handlers/notification/clockworksms.rb diff --git a/handlers/notification/clockworksms.json b/handlers/notification/clockworksms.json new file mode 100644 index 000000000..25ead51a3 --- /dev/null +++ b/handlers/notification/clockworksms.json @@ -0,0 +1,17 @@ +{ + "clockworksms": { + "key": "1234abc1esg1234abcdfdfg", + "to": { + "3864153012": [ + "ok", + "warning" + ], + "61431324189": [ + "ok", + "critical", + "warning" + ] + } + } +} + diff --git a/handlers/notification/clockworksms.rb b/handlers/notification/clockworksms.rb new file mode 100644 index 000000000..5143297ab --- /dev/null +++ b/handlers/notification/clockworksms.rb @@ -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 +# +# 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