SMS alerts with Nagios

In a previous post I mentioned how easy it is to increase functionality in Nagios.

Today I was asked to set up SMS alerts in Nagios, as well as the existing email alerts. I am by no means the first person to write about this, but this post is intended to be a start-to-finish guide, covering all aspects.

Choosing a provider

The first step is choosing a provider which has a decent API for sending SMS messages. I chose AQL, as I have used them in the past. They allow you to send SMS messages via a web interface, HTTP GET, HTTP POST, or email.

In that way, perhaps the easiest way to get SMS alerts is to get Nagios to email its alerts to the AQL SMS gateway. But I wanted to do it “properly”.

So I signed up for an account with AQL and bought a small number of SMS credits for the account. It’s also possible to have a contract for heavy usage, but I can always upgrade to that if I need to.

Defining a new method of alerting in Nagios

In the file /usr/local/nagios/etc/objects/commands.cfg there is a section where notification commands are defined. I added a couple more definitions for SMS alerts for hosts and services. My SMS script would have a calling syntax like /path/to/script phone_number message.

So I added a couple of definitions like this, obviously using a real mobile phone number:

define command{
command_name alert-service-by-sms
command_line /usr/local/nagios/libexec/alert-by-sms 01234567890 "Nagios Service $NOTIFICATIONTYPE$ Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}

define command{
command_name alert-host-by-sms
command_line /usr/local/nagios/libexec/alert-by-sms 01234567890 "Nagios Host $NOTIFICATIONTYPE$ Alert: $HOSTALIAS$ is $HOSTSTATE$"
}

The script

Now all remains is to write the script that will do the legwork. If you decide to go with AQL as your provider, you need to install their Perl module from CPAN. Use a command like this:

cpan SMS::AQL

And then make a Perl script like this. You can save it anywhere you like; I chose to put it with the other Nagios executables in /usr/local/nagios/libexec just to keep it with the rest. Adjust the username and password to match your AQL credentials, and set the sender parameter to be either a UK mobile number (so the recipient can reply to the message) or simply a text string which appears as a name to the recipient, and does not allow them to reply.

#!/usr/bin/perl -wT

use strict;
use SMS::AQL;

my $to = $ARGV[0];
my $msg = $ARGV[1];
$to =~ s/[^0-9]//gi;

my $sms = new SMS::AQL({
username => 'fred',
password => 'bloggs',
options => {
sender => 'Nagios',
},
});

my ($ok, $error) = $sms->send_sms($to, $msg);
if (!$ok) {
print "Failed to send the message, error: $errorn";
} else {
print "Success!n";
}

It is, of course, wise to test that your script works by calling it from the command line. Once you’re happy it works, it’s time to tell Nagios to start sending alerts.

Enabling SMS alerts

This time, we need to edit /usr/local/nagios/etc/objects/contacts.cfg. Modify your contact entry to add the lines in bold. This assumes you have only one user with a mobile phone – remember their mobile number is hard-coded into the command definition!

If you have more than one user and you set alert-service-by-sms or alert-host-by-sms for both, you will get two text messages sent to the same phone number for each Nagios alert. As I only have one user it’s not a problem for me, but in the future I will probably post a more elegant solution where each user can get an individual text message.

define contact{
contact_name                    jonathan
use                             generic-contact
alias                           Jonathan
email                           alerts@example.com
service_notification_commands   alert-service-by-sms
host_notification_commands      alert-host-by-sms

}

And that should be everything! Now you have to test it, either by breaking a host or service, or setting up a bogus one that will definitely fail.

20 Comments

  1. Andrew
    August 17, 2009
    Reply

    You can do single sms to multiple contacts by adding a ‘pager’ variable to the contact and replacing the number with ‘$CONTACTPAGER$’, like this:

    define contact{

    pager 447xxxxxxxxx ;
    }

    command_line /usr/local/bin/aql $CONTACTPAGER$ “Service: $SERVICEDESC$ | Host: $HOSTNAME$ | State: $SERVICESTATE$ | Date: $LONGDATETIME$”

  2. Jonathan
    August 17, 2009
    Reply

    @Andrew
    Thanks Andrew, I thought there might be a quick way of doing this.

  3. […] check_k8temp for monitoring the temperature of an AMD K8 (e.g. Athlon or Sempron) CPU * See my blog post if you are interested in setting up SMS alerts with Nagios […]

  4. August 17, 2010
    Reply

    An another solution exist : TeamTILT for Nagios.

    A great advantage is the heartbeat functionnality ! If your server or your mail service is down, the TeamTILT platform notify you by SMS…

    An another advantage is the number of media types : SMS, Voice call, iPhone push, email, fax, …

    Just have a look here : http://www.alarmtilt.com/en/manage-your-nagios-alerts-with-teamtilt.html

    😉

  5. paddy
    January 17, 2012
    Reply

    @ Jonathan ,

    I didn’t understand your commands defination in commands.cfg .
    It has [ command_line /usr/local/nagios/libexec/alert-by-sms ] .Here what is alert-by-sms.
    Is it script or command? If script , how should I get it?

    Thanks in advance.

  6. Jonathan
    January 17, 2012
    Reply

    @paddy

    The contents of the alert-by-sms script is given in the Perl script about halfway down this page – the bit that begins #!/usr/bin/perl -wT

  7. paddy
    January 18, 2012
    Reply

    If you have your own sms gateway , say , http://eai-k1.ge.org:54556 , in this case , alert-by-sms script won’t work.

    [I don’t want to go for AQL sms gateway , I have my own but not able to use it]

    Then how should one go for sms-alerts.

    Can you pls give your thoughts??

  8. Jonathan
    January 18, 2012
    Reply

    If you have your own SMS gateway, you’ll need to write your own alert-by-sms script in whatever language you like. It just needs to be able to push the right information to the gateway.

  9. Down
    August 1, 2012
    Reply

    Hey any idea why i’m getting this error?
    Use of uninitialized value $to in substitution (s///) at ./alert-by-sms line 8.
    Use of uninitialized value $to in substitution (s///) at /usr/local/share/perl/5.12.4/SMS/AQL.pm line 183.
    Failed to send the message, error: Unrecognised response from server: to_num

  10. August 1, 2012
    Reply

    It looks like you haven’t specified the “to” number as an argument. You need to use alert-by-sms like this:

    ./alert-by-sms 01234567890 "Message goes here"

  11. Down
    August 2, 2012
    Reply

    Jonathan, perfect it works.
    But now i have one other strange Issue.
    I cannot sent an SMS to US.

    Nagios 113779790 Igor is playing 🙂 02-08-12 02:26:32 Invalid Number
    iandev 16123479690 test 02-08-12 02:25:50 Delivered

    when i’m sending SMS from the WEB page they are delivered. but when i’m seding from the script somehow the phone number is changed and i get Invalid Number

    Any Idea?

  12. bicky b
    January 24, 2013
    Reply

    Hi Jonathan
    I am trying to get this working in my Centos box with Nagios, I have AQL account with enough credit and I will be really grateful if you can assist me on this please, So far:
    I have installed AQL Perl module from CPAN (http://search.cpan.org/~bigpresh/SMS-AQL-1.00/lib/SMS/AQL.pm)
    Created a file called /usr/local/nagios/libexec/alert-by-sms.pl with following content

    —————————-
    #!/usr/bin/perl -wT

    use strict;
    use SMS::AQL;

    my $to = $ARGV[0];
    my $msg = $ARGV[1];
    $to =~ s/[^0-9]//gi;

    my $sms = new SMS::AQL({
    username => ‘myaqlusername’,
    password => ‘myaqlpassword’,
    options => {
    sender => ‘Nagios’,
    },
    });

    my ($ok, $error) = $sms->send_sms($to, $msg);
    if (!$ok) {
    print “Failed to send the message, error: $errorn”;
    } else {
    print “Success!n”;
    }
    ————————————

    Now just to test it I ran follwing command from root and following is what I am getting:

    [root@core-nix-01 libexec]# ./alert-by-sms.pl 07987654321 “hello test”
    -bash: ./alert-by-sms.pl: Permission denied
    [root@core-nix-01 libexec]#

    Thank you.

    • January 24, 2013
      Reply

      Sounds like you’ve haven’t set the execute permission on your script. You need to run chmod a+x alert-by-sms.pl to set it executable by all users.

  13. bicky b
    January 25, 2013
    Reply

    Thank you Jonathan, it works now with:
    ./alert-by-sms.pl 079XXXXXXXX “hello test”

  14. bicky b
    January 25, 2013
    Reply

    by the way how do i send sms to multiple mobile number; following is what I got:

    define contact{
    contact_name bicky
    use generic-contact
    alias bicky
    email alerts@bicky.com
    service_notification_commands alert-service-by-sms
    host_notification_commands alert-host-by-sms
    pager 079XXXXXXXX
    }

    thank you.

  15. February 22, 2013
    Reply

    If you want something ready to use (with Nagios plugin included) take a look at this hardware device: SMSEagle (http://www.smseagle.eu).

    This is a hardware sms gateway, that you put into your server room, place inside your SIM card and it does sms sending together with Nagios.

    Nagios plugin for it is here: http://exchange.nagios.org/directory/Addons/Notifications/SMS/SMS-Notifications-via-SMSEagle/details

    With that you are independent from 3rd party sms service providers – it works when your Internet connection is down.

  16. Jane
    December 3, 2013
    Reply

    hi! if I use my sms gateway what script must I use???

Leave a Reply