Oct 5, 2010

Posted by in Administration, Linux | 21 Comments

Nagios and free SMS alerts

Nagios and free SMS alerts

Every professional administrator need to be informed about each failure on his servers. Imagine that you are in the cinema, in the shop or for example sleeping. Suddenly one of your machines takes very hight load. Problem requires your intervention… but you do not know about it.

Since now it is going to change…


Components:

- 1 fresh Nagios (must be configured),
- 1 “free Google sms gateway” script from this site,
- 10 minutes of free time
- 1 beer (or cup of good coffee ;) ).

Preparation:

1. Connect Google Calendar with your mobile phone.

For this log in into Google Calendar: https://www.google.com/calendar/ and go to the

Settings -> Calendar Settings

option. Make sure that you had configured your telephone number on the Mobile Setup tab. Next go to Calendars tab and make sure that in the Notifications settings you set “By default, remind me via SMS 1 minute before each event”.

2. Download, install and configure script to send SMS using Google Calendar.

cd /usr/bin/
wget http://artursmolarek.com/download/sendsms
chmod +x sendsms

Now you must edit downloaded script and change setting to your GMail account:

nano sendsms
login = 'username@gmail.com'
password = 'your_password'

Install python libraries which have been used in the script:

easy_install elementtree
easy_install gdata

Send test SMS to your phone:

sendsms @company test

3. Edit your Nagios configuration files:

- contacts.cfg should looks like:

service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email,notify-service-by-sms
host_notification_commands notify-host-by-email,notify-host-by-sms

- in the commands.cfg add:

# 'notify-host-by-sms' command definition
    define command{
    command_name notify-host-by-sms
    command_line sendsms "$NOTIFICATIONTYPE$: $HOSTNAME$ - $HOSTSTATE$"
}

# 'notify-service-by-sms' command definition
    define command{
    command_name notify-service-by-sms
    command_line sendsms "$NOTIFICATIONTYPE$: $HOSTALIAS$/$SERVICEDESC$ - $SERVICESTATE$"
}

4. Restart Nagios:

/etc/init.d/nagios restart

Now you can sleep peacefully. Every time Nagios will have problems, you receive SMS direct to your Phone. For free :)

  1. Please specify how to download & install python. I am able to understand your blog except below point.

    *********************
    Install python libraries which have been used in the script:
    view source
    print?
    1 easy_install elementtree
    2 easy_install gdata
    **********************

    Kindly guide me.

  2. Satveer Gaur says:

    Hi Artur,

    I found this article very helpful and thanks a lot for publishing this.But I want to correct here that

    service_notification_options w,u,c,r host_notification_options d,u,r service_notification_commands notify-service-by-email,notify-service-by-sms host_notification_commands notify-host-by-email,notify-host-by-sms

    This we need to configure in templates.cfg file not in contacts.cfg .

    I again want to say Thank You for this article.

    Satveer Gaur

  3. Salim Hoolash says:

    Hi Artur,
    I find your article very interesting. However, I am stuck at the ‘nano sendsms’ part.
    First of all I do not have a gmail account. Do I really need one?
    If I put a fictitious mail, it returns:

    File “/usr/bin/sendsms”, line 9
    login = salim.hoolash@gmail.com
    SyntaxError: invalid syntax ^

    Can you please help? What should be the user name and password?
    Thanks for replying.
    Salim.

  4. Salim Hoolash says:

    Hi Artur,

    I have been able to configure . Forgot to put the ”. Now when I try:
    sendsms
    It returns:
    Unable to login to Google calendar

    Any idea?

    Thanks.

  5. Thanks for the interesting information. Subscribe to rss

  6. Great stuff – a real big thanks for this!

    1 question – I have it texting me all good.

    Is it possible to get it to text 2 people? Myself and another admin?

    Add 2 gmail account details to script?
    Run 2 scripts?

    Is this possible please?

    Regards

    • One of possible solutions: download sendsms script as sendsms2 (set in it configuration for your second admin).

      Next add another one contact in contacts.cfg:

      service_notification_options w,u,c,r
      host_notification_options d,u,r
      service_notification_commands notify-service-by-email,notify-service-by-sms,notify-service-by-sms2
      host_notification_commands notify-host-by-email,notify-host-by-sms,notify-host-by-sms2

      And in the end add in the commands.cfg file:

      # ‘notify-host-by-sms’ command definition
          define command{
          command_name notify-host-by-sms2
          command_line sendsms2 “$NOTIFICATIONTYPE$: $HOSTNAME$ – $HOSTSTATE$”
      }
       
      # ‘notify-service-by-sms2′ command definition
          define command{
          command_name notify-service-by-sms2
          command_line sendsms “$NOTIFICATIONTYPE$: $HOSTALIAS$/$SERVICEDESC$ – $SERVICESTATE$”
      }

  7. Hi
    When you do the test you have to put like this ?

    /usr/bin$ sendsms @vodafone test

    I Have done this but inspite of not getting error in the terminal i don´t receive the sms in my mobile phone

    I have also tried in this way but anithing also
    usr/bin$ sendsms unaim7@gmail.com

    • Look at your google calendar. You should see new event every time when you use sendsms script.

      If you can see events and still not receive sms, check the settings of your google calendar.

      • Hi arthur ,
        Yes , I can see events but i don´t receive the sms.
        I have revised the configuration of the calendar and it´s already ok , also the mobile number is accepted , what´s more when I see the calendar events apeears that they are noticed by sms .
        Maybe can be a problem of google?

        • It seems that the problem is beetween Caldar and you mobile phone. My script works ok.

        • Hi arthur ,

          Finally the problem was that In my pc the date is ok but in the virtual machine I´ve installed nagios the date was wrong (2 ours before normal date )xD and because of that I didn´t get the sms , because it has already passed.
          Now it´s ok and already working .
          THANKS!!!

  8. hey !
    nice job man.

    I am trying to set it up for myself but when i tried:
    sendsms @company test
    i get -bash: /usr/bin/sendsms: Permission denied

    any idea why?
    ive got an google account and enable sms notification etc.

    Best regards

  9. Hi Artur,

    Is there a way in your script where we can put the reminder of the event?

    Regards,

    Arnold

Leave a Reply