Configure Nagios to send SMS alerts

  13 Dec 2014


I have tried to register to use Twilio service. But the verification messege is too slow (in thailand). It’s like the verification sms is sent to me after it’s already expired!? so no way for me to get verified. I found nexmo service, it’s simple and the demo just works. So I use it :)

How to configure Nagios to send SMS Notifications via Nexmo? I check out this plugin but you have to install those Getopt::Long, LWP, URI::Escape on your system. If you have permission to install, then you will be fine.

First add the following in your command.cfg file (e.g. /usr/local/nagios/etc/objects/commands.cfg)

# 'notify-service-by-sms' command definition
define command{
       command_name    notify-service-by-sms
       command_line    /usr/local/bin/notify_sms.pl -k KEY -s SECRET -t $CONTACTPAGER$ -f "Apsumon Monitoring Service" -m "Service: $SERVICEDESC$\\nHost: $HOSTNAME$\\nAddress: $HOSTADDRESS$\\nState: $SERVICESTATE$\\nInfo: $SERVICEOUTPUT$\\nDate: $LONGDATETIME$"
}

# 'notify-host-by-sms' command definition
define command{
       command_name    notify-host-by-sms
       command_line    /usr/local/bin/notify_sms.pl -k KEY -s SECRET -t $CONTACTPAGER$ -f Nagios -m "Host $HOSTNAME$ is $HOSTSTATE$\\nInfo: $HOSTOUTPUT$\\nTime: $LONGDATETIME$"
}

replace KEY and SECRET from your Nexmo api settting.

Then, in your contacts.cfg file (e.g. /usr/local/nagios/etc/objects/contacts.cfg) add the SMS notification commands like so:

define contact{
        ....
        service_notification_commands   notify-service-by-sms
        host_notification_commands      notify-service-by-sms
        ....
}

And add a pager number (phone number) to your contacts definition ( prefix, e.g. 66 for TH or 44 for UK ), from the previous step, it will look like this:

define contact{
        ....
        service_notification_commands   notify-service-by-sms
        host_notification_commands      notify-service-by-sms
        pager      66814445555
        ....
}

If you already have other notifications, use comma (,) to separate commands e.g. notify-host-by-email,notify-host-by-slack,notify-host-by-sms

Don’t put a leading 00 or +.

Try to shut some services down. I stop ssh. The trick I use is I connect to server with mosh to make sure if there is network issue I will still be connected. Wait after you get HARD state alert, then you should get a notification

[1418476839] SERVICE NOTIFICATION: root;localhost;SSH;OK;notify-service-by-sms;SSH OK - OpenSSH_5.3p1 Debian-3ubuntu3 (protocol 2.0)

If this happens, you should get sms.

Unless you have to check what is wrong with the command_line in command.cfg. I recommended to use http://www.waggy.at/nagios/capture_plugin.txt to see the error in /tmp/captured-plugins.log or you can set debug_level=256 in your nagios.cfg file to see output for what nagios has executed and just grab that to run manually.

comments powered by Disqus