Nagios. Sending SMS via LittleSMS

Notifications by email are convenient, of course, but not timely. If you are traveling or have simply stepped away from your desk, you will never know that your servers or services have suddenly stopped working.
To fix this, I use SMS notifications. Today I will explain how to set up SMS sending in Nagios.
Choosing a gateway for sending messages
There are several ways to send SMS messages:
- Buy a phone and a SIM card from a mobile provider, connect the phone to the server, and send SMS messages through it. This method is inconvenient because it requires physical access to the server, adds the hassle of maintaining the phone itself, and so on. As for advantages, I cannot think of any at all; I think this was simply relevant back in the day.
- Free SMS via email gateways and services that send SMS messages in response to certain events, for example through Google Calendar. If you have no money at all and plenty of time for pointless work, this is the option for you.
- Sending through services—naturally, not for free, but not all that expensive either. For example, there is a fairly cheap (from 30 kopecks per SMS), convenient, and seemingly good-quality service called LittleSMS, which I decided to use to send SMS messages.
Naturally, I first discussed with friends which services they were currently using, and that resulted in this forum thread. Besides LittleSMS, you can use other services and even combine them, but I will leave that up to you; in this article, I will implement support for just one service.
Signing up for LittleSMS and writing a script to send SMS messages
Go to the registration page and enter the information the form requires. After registering, go to the API Settings section and copy the API-key. Then go to the Sender Names section and specify a sender name. You will need to specify the same name in the sendsms.py script (the DEFAULT_SENDER variable), or you can pass it as an argument each time you run it:
sendsms --sender MYSENDER ...
After studying the LittleSMS API, I wrote this simple script, sendsms.py. It is not tailored exclusively to Nagios and can be used throughout the server for various purposes. Let’s install it:
wget --no-check-certificate -O /usr/local/sendsms.py https://gist.github.com/raw/4463243/sendsms.py
ln -s /usr/local/sendsms.py /usr/bin/sendsms
chmod +x /usr/bin/sendsms
Now we need to specify our email and API-key:
emacs /usr/bin/sendsms
# И заполняем значения переменных USER_EMAIL и USER_APIKEY соответственно
# Там же вы можете поменять DEFAULT_SENDER
You can view sent SMS messages on the Messages page, and send them like this:
sendsms --recipients 79119876543 --sender MYSENDER --message TestMessage
sendsms --recipients 79119876543 --message TestMessage
sendsms -R 79119876543 -M TestMessage
# или даже так:
echo "Some message" | sendsms -R 79119876543
To avoid errors when sending a large message (that is, lots of small SMS messages), for example through a pipe, I provided the --read-bytes READ_BYTES flag, which sends the first 256 bytes by default. You can change this value:
cat /path/to/report.txt | sendsms -R 79119876543 --read-bytes 2048
By the way, after writing this article, I discovered a section on the website called Download ready-made solutions, which has some quite good ready-made solutions.
Configuring Nagios
Go to the Nagios configuration directory. On FreeBSD, this is usually /usr/local/etc/nagios/objects, and on Debian, it is /etc/nagios3.
First, let’s add the commands. Open the commands.cfg file for editing and add the following:
define command{
command_name notify-host-by-sms
command_line /usr/bin/sendsms --recipients $_CONTACTPHONE$ --message \
'$NOTIFICATIONTYPE$ on "$HOSTNAME$": $HOSTOUTPUT$'
}
define command{
command_name notify-service-by-sms
command_line /usr/bin/sendsms --recipients $_CONTACTPHONE$ --message \
'$NOTIFICATIONTYPE$ for "$SERVICEDESC$" on "$HOSTNAME$": $SERVICEOUTPUT$'
}
You can find all these standard macros in the documentation.
Next, we need to specify that, for our users, the notify-host-by-email and notify-service-by-email commands should be accompanied by calls to notify-host-by-sms and notify-service-by-sms. To do this, we need to change the contact template. Open the templates.cfg file:
define contact{
name generic-contact
...
service_notification_commands notify-service-by-email, notify-service-by-sms
host_notification_commands notify-host-by-email, notify-host-by-sms
...
}
On Debian, contact templates are not used by default, so the service_notification_commands and host_notification_commands directives must be set directly in the contact (or you should create a template yourself and use it, which I think would be the better approach).
Finally, let’s specify our phone numbers in the contacts. Open the contacts.cfg file for editing (on Debian, this is conf.d/contacts_nagios2.cfg):
define contact{
contact_name adw0rd
use generic-contact
alias Mikhail Andreev
email info@adw0rd.com
# service_notification_commands notify-service-by-email, notify-service-by-sms
# host_notification_commands notify-host-by-email, notify-host-by-sms
_phone 79119876543
}
Here we specified “_phone” instead of “phone”, because custom variables are defined with the “_” prefix. For details, see the Custom Object Variables page.
Restart Nagios and wait for SMS messages:
# во FreeBSD
/usr/local/etc/rc.d/nagios restart
# или в Debian
/etc/init.d/nagios3 restart
If you cannot wait, go into the Nagios web interface, select any service, and click “Send custom service notification”. Fill out the form and submit it. You should receive an SMS within seconds.
In the same Nagios web interface, you can view notifications and the commands that sent them in the “Reports > Notifications” section. Look for “notify-service-by-sms” in the “Notification Command” column.
What else should you read?
Comments are closed. Read the original discussion in Russian →