Way2sms offers SMS communication totally cost free to mobile users anywhere in India, at the same time, enables advertisers to reach out to millions of mobile users through the revolutionary concept of ‘Mobitisement’ (advertisement on mobile phone)..blah..blah..

So my question is why can’t  we utilize this free SMS service to send alerts from monitoring softwares like Nagios?  Isn’t it possible?  Of course it is. Modern languages like perl and python offers powerful modules like mechanize  for pragmatic browsing. So with the help of mechanize module in python (me bit python addicted ;-) ) I have decided to create a small knife to inject my Nagios alerts through way2sms.com, so that I can receive my notifications on the fly.

No need to explain this stuff I think, just go on, it’s like a story. Just blind automation.  Don’t forget to put your way2sms USER NAME & PASSWORD on the script.

EDIT: way2sms have modified their website with random IDs in form fields and all. Don’t know why,may be to prevent automation. :-D
Please use the new script ==>
http://brijin.net/sendsms3.py

below one will not work!

 

#!/usr/bin/env python 

import sys
import time
try:
    import mechanize
except ImportError:
    print "Please install mechanize module for python"
    print "Install python-mechanize, if you are on a Ubuntu/Debian machine"
    sys.exit(1)
try:
    from optparse import OptionParser
except ImportError:
    print "Error importing optparse module"
    sys.exit(1)

def SendSMS(mobile,text):
    print ">>> initializing.."
    br = mechanize.Browser()
    print ">>> connecting to way2sms..."
    try:
       br.open("http://site3.way2sms.com/entry.jsp")
       br.select_form(name="loginform")
       br["username"] = ""  #YOUR MOBILE NUMBER HERE
       br["password"] = ""  #YOUR PASSWORD HERE
       br.form.method="POST"
       br.form.action="http://site1.way2sms.com/Login1.action"
       print ">>> " + br.title()
       response = br.submit()
       response.get_data()
       print ">>> logged in.."
    except:
        print ">>> FATAL: Error occured while login process!"
        sys.exit(1)
    try:
        print ">>> sending message..."
        br.open("http://site1.way2sms.com/jsp/InstantSMS.jsp")
        br.select_form(name="InstantSMS")
        br["MobNo"]      = mobile
        br["textArea"]   = text
        br.form.method="POST"
        br.form.action="http://site1.way2sms.com/quicksms.action"
        response = br.submit()
        print ">>> submitting..."
        print ">>> logging out..."
        br.open("http://site1.way2sms.com/jsp/logout.jsp")
        br.close()
    except:
        print ">>> html seems to be changed!"
        print ">>> please modify the program to work with newly modified website!"
        sys.exit(1)

def main():
    parser = OptionParser()
    usage = "Usage: %prog -m [number] -t [text]"
    parser = OptionParser(usage=usage, version="%prog 1.0")
    parser.add_option("-m", "--number",  action="store", type="string",dest="number",  help="Mobile number to send sms")
    parser.add_option("-t", "--text", action="store", type="string", dest="text", help="Text to send")
    (options, args) = parser.parse_args()
    if options.number and options.text:
       SendSMS(options.number,options.text)
    else:
       print "Fatal: Required arguments are missing!"
       print "Use: -h / --help to get help."

if __name__ == "__main__":
   main()

 

Example Usage:

bris@firefly:~$ ./sendsms.py -h
Usage: sendsms.py -m  -t 

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -m NUMBER, --number=NUMBER
                        Mobile number to send sms
  -t TEXT, --text=TEXT  Text to send

 

You can define a new alert command and configure nagios to use this script.  Here is an example,

define command{
    command_name    notify-host-by-sms
    command_line    /path/to/sendsms.py -m number -t "Type: $NOTIFICATIONTYPE$\
                    Host: $HOSTNAME$ \
                    State: $HOSTSTATE$ \
                    Address: $HOSTADDRESS$ \
                    Info: $HOSTOUTPUT$ Time: $LONGDATETIME$"
}

You can use this notify-host-by-sms command to define a new (or existing, its all your choice) contact, and that’s it!

The setup I am having a is a different one, I cannot share you that :-D . Instead of this small script I wrote my own daemon which will listen on a tcp port(loopback) which will keep the login to way2sms.com upto some time,  and my client program will send text messages from Nagios to this daemon through tcp (designed small protocol for that). The advantage is suppose if we want to sent one alert to 10 cellphone numbers then the daemon will do whole thing in a single login shot! No need to login again and again.

 

Thanks for reading! Drop me a line if you have any queries. :-)

 

 

 

«