Activities

April 2012
M T W T F S S
« Mar   May »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Nagios : Enabling FREE SMS notification [INDIA]

Here I found some good trick to enable Nagios sms notification free for Indian users using one of the sms free service offered by way2sms website. have a look at way2sms.com.

Most of the cases I used to take tiny pieces of information from different blog sites and customize and modify it for my purpose. This website (brijin.net) has the python script to send sms using way2sms. I’m thanking to that blog owner :-).

Action involved are,

1. Register new account with way2sms.com.
2. Configure python script
3. Add the new nagios command for enabling sms notification for service and host failure.
4. Update the contact information.

1. Create an account with way2sms.com

Register your moble and this service company send you the login details over sms. You need this login information in python script to send sms. This script just does the normal web login to send sms.

2. Install python script

Create a script under “/usr/local/nagios/libexec/send_sms.py“. You may need to update the following fields in the script
br[“username”] = “” #YOUR MOBILE NUMBER HERE
br[“password”] = “” #YOUR PASSWORD HERE

Here is the script content.

#!/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 occurred 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()

Now you need to install python-mechanize in order to work this script. so you need to install python package installer program “eazy_install” which shipped with setuptools.

[root@nagios /]# wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e
[root@nagios /]# tar -zxvf setuptools-0.6c11.tar.gz
[root@nagios /]# cd setuptools-0.6c11
[root@nagios setuptools-0.6c11]# python ./setup.py install
[root@nagios setuptools-0.6c11]# easy_install mechanize

Now you need to verify the packages are correctly installed.

[root@nagios ~]# python  /usr/local/nagios/libexec/send_sms.py -h
usage: Usage: send_sms.py -m [number] -t [text]

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
[root@nagios ~]#

Sending test sms

[root@nagios ~]# python  /usr/local/nagios/libexec/send_sms.py -m 94977720 -t " Testing Nagios notification"
>>> initializing..
>>> connecting to way2sms...
>>> Free SMS, Send Free SMS, Send Free SMS to india, Free email alerts, email2SMS, SMS Alerts,Bill Reminders, EMI Reminders, Loan Reminders, TV Shows Reminders, Future SMS, Mobile Bill Reminders
>>> logged in..
>>> sending message...
>>> submitting...
>>> logging out...
[root@nagios ~]#

So your installation looks good.

3. Updating “/usr/local/nagios/etc/objects/commands.cfg” file
Open the file and append the following lines at the end of file

define command{
    command_name    notify-host-by-sms
    command_line    /usr/local/nagios/libexec/send_sms.py  -m $CONTACTPAGER$ -t "Type: $NOTIFICATIONTYPE$\
                    Host: $HOSTNAME$ \
                    State: $HOSTSTATE$ \
                    Address: $HOSTADDRESS$ \
                    Info: $HOSTOUTPUT$ Time: $LONGDATETIME$"
}

define command{
    command_name    notify-service-by-sms
    command_line    /usr/local/nagios/libexec/send_sms.py  -m $CONTACTPAGER$  -t "Type: $NOTIFICATIONTYPE$\
                    Host: $HOSTNAME$ \
                    Service: $SERVICEDESC$ \
                    State: $SERVICESTATE$ \
                    Date/Time: $LONGDATETIME$ Additional Info:$SERVICEOUTPUT$"
}

4. Update your contact information (/usr/local/nagios/etc/objects/contacts.cfg)

You may need to add 3 lines in it.

service_notification_commands notify-service-by-email,notify-service-by-sms
host_notification_commands notify-host-by-email,notify-service-by-sms
pager 9744209638

My sample contact will look like this.

define contact{
contact_name    liju
use             generic-contact
alias           Liju Mathew
email           liju@serveridol.com
service_notification_commands notify-service-by-email,notify-service-by-sms
host_notification_commands notify-host-by-email,notify-service-by-sms
pager 94000000
 }

[root@nagios ~]# service nagios restart
Running configuration check…done.
Stopping nagios: done.
Starting nagios: done.
[root@nagios ~]#

Then simply restart Nagios service. For the testing purpose you may set one incorrect service name which was not defined in nrpe commands list. So that you get a notification sms and have the test easily.

Cool !!! Now you have ABSOLUTELY FREE nagios sms alert facility.

You may also use this script for sending sms if any critical systems/process reached the threshold limit like backup failure, intruder detention found etc. etc.

nJnoy 🙂

16 comments to Nagios : Enabling FREE SMS notification [INDIA]

  • Vivek I S

    Hi Dude… this is really very useful info.. crisp & clear.. thnx..

  • Troy

    Hi Liju,
    Very clear and precise..worked like a charm..thanks.
    Cheers,
    Troy

    • rajesh

      hi troy,
      i followed above guidlines. Every thing is working fine. i can send mail from command line using [ python /usr/local/nagios/libexec/send_sms.py -m 99XXXXXXXX -t “this is test”.]
      Now i want sms should be delivered if MySql goes down or ” ping” stops or ssh down.
      how to do that ? You reply awaiting !!!!!!!

  • Pallav

    hey,
    I think website has changed

    mine saying html has changed

  • rajesh

    i followed above guideline but i got an error when i see the log file. Below are the log msg
    #/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

    Nagios Core 3.4.1
    Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
    Copyright (c) 1999-2009 Ethan Galstad
    Last Modified: 05-11-2012
    License: GPL

    Website: http://www.nagios.org

    #service nagios restart

    Reading configuration data…
    Read main config file okay…
    Processing object config file ‘/usr/local/nagios/etc/objects/commands.cfg’…
    Error: Invalid command object directive ‘Address:’.
    Error: Could not add object property in file ‘/usr/local/nagios/etc/objects/commands.cfg’ on line 257.
    Error processing object config files!

    and if i commented ( Address: $HOSTADDRESS$\) in command.cfg file.
    nagios restart successfully but in browser it shows below msg.

    Whoops!

    Error: Could not read object configuration data!

    Here are some things you should check in order to resolve this error:

    Verify configuration options using the -v command-line option to check for errors.
    Check the Nagios log file for messages relating to startup or status data errors.

    Please help on this.

  • pls allow this service for bangladesh

  • This is very good documentation.

  • Nikhil

    Hi,

    Thanks for the useful script..

    The script works correctly if I run it directly (not through nagios).
    But through nagios, it gives “Fatal error”. The error is because of line
    br.select_form(‘loginform’)

    Can you please help in resolving it. Is it because of some administrative permissions to user ‘nagios’.

    Thanks.

    • Hello Nikhil

      Ensure that you script has copied under “nagios’s libexec ” directory and others has execute permission.

      Keep in mind that you may need to install certain modules required for run this scripts.

      [root@nagios ~]# ls -la /usr/local/nagios/libexec/send_sms.py
      -rwxr-xr-x 1 root root 2320 Apr 11 2012 /usr/local/nagios/libexec/send_sms.py
      [root@nagios ~]#

  • akshay

    I used your script but it gives me error. Stating ”
    >>> html seems to be changed!
    >>> please modify the program to work with newly modified website!

    I intentionally put then the incorrect password, even then it says

    >>> html seems to be changed!
    >>> please modify the program to work with newly modified website!

    but in actual it should say, if the password is incorrect

    >>> FATAL: Error occurred while login process!

    So, can you please resolve this and help me?

  • Deepak I

    I get this error.. .what am I doing wrong???

    >>> initializing..
    >>> Debug: ON
    >>> connecting to way2sms…
    Debug information
    *************************
    Traceback (most recent call last):
    File “/usr/local/nagios/libexec/send_sms.py”, line 83, in do
    response = self.br.open(self.master)
    File “/usr/lib/python2.4/site-packages/mechanize/_mechanize.py”, line 203, in open
    return self._mech_open(url, data, timeout=timeout)
    File “/usr/lib/python2.4/site-packages/mechanize/_mechanize.py”, line 255, in _mech_open
    raise response
    httperror_seek_wrapper: HTTP Error 403: request disallowed by robots.txt

    *************************
    >>> FATAL: Error occured while performing process!

  • Sreejith

    connecting to way2sms…
    >>> FATAL: Error occurred while login process!

  • rahul

    >>> FATAL: Error occurred while login process!

    this error is coming, can you please suggest!!!!

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>