From 65fcea0eac21f95fd4398006551c56bc6760e261 Mon Sep 17 00:00:00 2001 From: James Hannah Date: Mon, 3 Jun 2013 15:35:29 +0100 Subject: Added an SMS provider for clockworksms.com --- etc/mauveserver.conf | 14 ++++++- lib/mauve/notifiers.rb | 1 + lib/mauve/notifiers/sms_clockwork.rb | 72 ++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 lib/mauve/notifiers/sms_clockwork.rb diff --git a/etc/mauveserver.conf b/etc/mauveserver.conf index 4baec0a..75dfb68 100644 --- a/etc/mauveserver.conf +++ b/etc/mauveserver.conf @@ -75,8 +75,8 @@ notification_method("email") { # password "x" # } -# How to notify by SMS - we use aql.com, you'll need to write a module -# to use any other provider. +# How to notify by SMS - we use aql.com, a provider for clockworksms.com +# is also provided. For another provider, you'll need to write a module. # # notification_method("sms") { # provider "AQL" @@ -88,6 +88,16 @@ notification_method("email") { # # Maximum number of SMS messages to concatenate for one notification # max_messages_per_alert 3 # } +# +# notification_method("sms") { +# provider "Clockwork" +# +# apikey "sssseeeeeeccccccccrrrrreeeeeettttsssssss" +# from "01234567890" +# +# # Maximum number of SMS messages to concatenate for one notification +# max_messages_per_alert 3 +# } # Simple default notification preference for root at this machine, at all # alert levels. You probably want more people, see below for a more complete diff --git a/lib/mauve/notifiers.rb b/lib/mauve/notifiers.rb index 7276091..5a7acc0 100644 --- a/lib/mauve/notifiers.rb +++ b/lib/mauve/notifiers.rb @@ -1,6 +1,7 @@ require 'mauve/notifiers/email' require 'mauve/notifiers/sms_default' require 'mauve/notifiers/sms_aql' +require 'mauve/notifiers/sms_clockwork' require 'mauve/notifiers/xmpp' module Mauve diff --git a/lib/mauve/notifiers/sms_clockwork.rb b/lib/mauve/notifiers/sms_clockwork.rb new file mode 100644 index 0000000..b4bd860 --- /dev/null +++ b/lib/mauve/notifiers/sms_clockwork.rb @@ -0,0 +1,72 @@ +require 'mauve/notifiers/debug' +require 'cgi' + +module Mauve + module Notifiers + module Sms + + require 'net/https' + + class Clockwork + GATEWAY = "https://api.clockworksms.com/http/send.aspx" + + attr_writer :apikey, :from + attr_reader :name + + def initialize(name) + @name = name + end + + def send_alert(destination, alert, all_alerts, conditions = {}) + uri = URI.parse(GATEWAY) + + opts_string = { + :key => @apikey, + :to => normalize_number(destination), + :content => prepare_message(destination, alert, all_alerts, conditions), + :from => @from, + }.map { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join("&") + + http = Net::HTTP.new(uri.host, uri.port) + if uri.port == 443 + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + response, data = http.post(uri.path, opts_string, { + 'Content-Type' => 'application/x-www-form-urlencoded', + 'Content-Length' => opts_string.length.to_s + }) + + if response.kind_of?(Net::HTTPSuccess) + # + # Woo -- return true! + # + true + else + false + end + end + + protected + def prepare_message(destination, alert, all_alerts, conditions={}) + was_suppressed = conditions[:was_suppressed] || false + will_suppress = conditions[:will_suppress] || false + + template_file = File.join(File.dirname(__FILE__),"templates","sms.txt.erb") + + txt = if File.exists?(template_file) + ERB.new(File.read(template_file)).result(binding).chomp + else + logger.error("Could not find sms.txt.erb template") + alert.to_s + end + end + + def normalize_number(n) + n.split("").select { |s| (?0..?9).include?(s[0]) }.join.gsub(/^0/, "44") + end + end + end + end +end + -- cgit v1.2.1