Ho scritto un po' di codice Ruby in grado di mandare SMS gratis con Skebby utilizzando due account per privati (funziona anche con uno soltanto sia come mittente che destinatario).
L'account Skebby deve essere attivo attivo e dovete aver abilitato username e password (dal pannello di gestione utente).

Lo script è questo:

#!/usr/bin/env ruby
# Copyright 2012 by Tommaso Visconti
#
# This code is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# The full text of the GNU General Public License can be found here: http://www.gnu.org/licenses/gpl.txt
require 'net/http'  
require 'uri'  
require 'cgi'  
$SENDER = '393XXXXXXXXX'
$RECIPIENT = '393XXXXXXXXY'
$USERNAME = 'my_username'
$PASSWORD = 'my_password'
# The class sends free SMS using the Skebby API
# @author Tommaso Visconti

class SkebbyGatewaySendSMS

  # Initializes the object
  # @param username [String] The Skebby username
  # @param text [String] The text of the SMS
  # @param recipients [Array] The recipients of the message
  # @param sender [String] The SMS sender
  def initialize(username = '', text = '', recipient = '', sender = '')

    @url = 'http://gateway.skebby.it/api/send/smsskebby/advanced/http.php'

    sms_method = 'send_sms'

    #@recipients = getRecipients(recipients)

    @parameters = {
      'method' => sms_method,
      'username' => username,
      'password' => self.getPassword,
      'text' => text,
      'recipients[]' => recipient,
      'sender_number' => sender
    }
  end

  # Sends the HTTP request
  # @return [Boolean] The result of the HTTP request
  # @note It returns the result of the HTTP request, not the result of the sending. Use getResponse() or printResponse() to check that!
  def sendSMS
    @response = Net::HTTP.post_form(URI(@url), @parameters)

    @response.message == "OK" ? true : false
  end

  # Return the response from the server
  # @return [Hash] The hash containing the result of the sending
  def getResponse
    result = {}
    @response.body.split('&').each do |res|
    if res != ''
      temp = res.split('=')
      if temp.size > 1
        result[temp[0]] = temp[1]
      end
    end
  end

  return result
  end

  # Prints the response in human-readable format
  # @return [Boolean] The result of the SMS sending
  def printResponse
    result = self.getResponse
    if result.has_key?('status') and result['status'] == 'success'
      puts "SMS sent successfully"
      true
    else
      puts "Error sending the SMS, printing the full trace:"
      result.each do |key,value|
        puts "\t#{key} => #{CGI::unescape(value)}"
      end
      false
    end
  end

  # Encodes the recipients in the correct format
  # @param recipients [Array] The Array containing the recipients as String in the format 3912345678 (without + or 00 at the beginning)
  # @return [String] The String in the format accepted by the server
  # @note This method uses CGI::escape to escape the recipients
  def getRecipients(recipients)
    recipients.inject('') do |result, number|
    result << '&' unless number == recipients.first
    result << "recipients[]=#{CGI::escape(number)}" end
  end

  # Obtains the password from the command line hiding the input
  # @return (String) The given password
  def getPassword
    unless $PASSWORD.nil?
      password = $PASSWORD
    else
      puts "Password:"
      system "stty -echo"
      password = $stdin.gets.chomp
      system "stty echo"
    end
    password
  end
end

# Example method to send SMS fast
def send_my_sms(text = 'Empty')  
  gw = SkebbyGatewaySendSMS.new($USERNAME, text, $RECIPIENT, $SENDER)
  if gw.sendSMS
    gw.printResponse
  else
    puts "Error in the HTTP request"
  end
end

# Send SMS with Icinga/Nagios
if ARGV.size > 0  
  text = ARGV[0]
  send_my_sms(text)
end  

Il programma può essere usato da linea di comando passandogli il messaggio come primo argomento oppure potete includerlo in un altro script ruby e usare la classe SkebbyGatewaySendSMS.
Se invece salvate questo script, ad esempio, in /usr/local/icinga/libexec/smsgateway_ e lo rendete eseguibile, potete usarlo per far mandare SMS a Icinga. Bisogna intanto definire due comandi in etc/objects/commands.cfg:

# 'notify-service-by-sms' command definition
define command{
      command_name    notify-service-by-sms
      command_line      /usr/local/icinga/libexec/sms_gateway "--Nagios Service Notification-- Host: $HOSTNAME$, State: $HOSTSTATE$ Service $SERVICEDESC$ Description: $SERVICESTATE$ Time: $LONGDATETIME$"
}
# 'notify-host-by-sms' command definition
define command{
      command_name    notify-host-by-sms
      command_line      /usr/local/icinga/libexec/sms_gateway "--Nagios Host Notification-- Host: $HOSTNAME$, State: $HOSTSTATE$, Time: $LONGDATETIME$"
}

Quindi in etc/objects/contacts.cfg aggiungere all'utente icingaadmin (o chi per lui) le notifiche via SMS:

define contact{
    contact_name                    icingaadmin
    use                             generic-contact
    alias                           Icinga Admin
    email                           my_email@domain.com
    host_notification_commands      notify-host-by-sms,notify-host-by-email
    service_notification_commands      notify-service-by-sms,notify-service-by-email
}

Aggiornamento

Vedo con piacere che Skebby ha usato il codice che gli ho inviato per creare l'esempio di codice Ruby, anche se nel farlo ha completamente eliminato la licenza GPL che aveva il codice originale...

blog comments powered by Disqus