Home > Guides, Linux, Nagios > SMS alerts with Nagios

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: $error\n";
} 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.

Categories: Guides, Linux, Nagios Tags: , ,
  1. Andrew
    August 17th, 2009 at 10:00 | #1

    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 17th, 2009 at 10:43 | #2

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

  3. August 17th, 2010 at 14:15 | #3

    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
    ;)

  4. paddy
    January 17th, 2012 at 12:08 | #4

    @ 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.

  5. Jonathan
    January 17th, 2012 at 12:12 | #5

    @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

  6. paddy
    January 18th, 2012 at 11:25 | #6

    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??

  7. Jonathan
    January 18th, 2012 at 11:28 | #7

    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.

  1. October 15th, 2009 at 20:46 | #1

Notify me of followup comments via e-mail