This is a 3 part posting that covers how you can setup 2-way
Nagios alerting via SMS.
The series is broken down as such:
- Part 1: Background and Requirements
- Part 2: Sending SMS messages from Nagios
- Part 3: Acknowledging SMS messages (this article)
Review
As we saw in
Part 2 we covered setting up Nagios to send out alerts.
Whilst this is great, it's not of great value if we can't do anything with these SMS messages. In addition, if no-one is sitting in front of the actual Nagios web console to acknowledge the issue, then Nagios will continue sending the SMS messages and will quickly fill up your phone's message memory. Not ideal!
The message is quite straight forward as well to read. A service problem message contains:
- The type of incident: PROBLEM
- The Host affected
- The Service affected
- The Severity of the incident
- Additional Information about the incident.
- The Time of the incident
- A field to Respond/acknowledge the incident
A host problem message is quite similar but does not have a Service or Severity line.
Here is a Host problem message for comparison:
PROBLEM
Host 'MyServer' is DOWN
I: CRITICAL - Host Unreachable (10.0.0.74)
T: 2008-05-22 17:52:33
R:
Replying to the Nagios Problem SMS
When a problem from Nagios is sent to you via SMS it's quite easy to respond.
All you need to do is reply to the message (and include the text - which most mobile phones on the market provide a feature to do).
Whilst not required, you can append a simple message to your reply. This will be used in the acknowledgment response and add a comment to the service.
Comments don't need to be long, but they do help your other team members or interested parties know what is happening with the issue.
Be careful what you write!

The response you send will also be used in a follow-up SMS to everyone notifying them that someone is looking at the issue. You don't want to send something that might not be appreciated others that recipients
Acknowledging SMS messages
Acknowledging messages from Nagios is quite simple now we have our SMS Daemon setup (
see Part 2 for details)
As a result, any incoming SMS messages get stored into our MySQL database.
We just need a process now to read them from the database, interpret them and then update Nagios appropriately.
Setting up the SMS Daemon Phone book
In order to turn your mobile numbers into something more meaningful (like the name you use to login into Nagios), you can use the Gammu SMSD phonebook.
The phone book table in the database reads like:
CREATE TABLE `pbk` (
`GroupID` INT(11) NOT NULL DEFAULT '-1',
`Name` TEXT NOT NULL,
`Number` TEXT NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Basically it has 3 columns:
GroupID,
Name and
Number.
In our use, we don't utilize the GroupID field, but you could in theory use this for other applications you use.
By default, I use a value of '1' for the GroupID
You need to enter your phone number in the form that Gammu reads it back in. This is usually in the form of: +CCnnnnnnnnn. That means a + to represent a country code, then the two digit country code, and then the 9 digit mobile number.
For example if your name was Fred and you were an Australian User with the unfortunate mobile number of 0412345678 (you're bound to get lots of prank calls!) your entry would be like:
INSERT INTO `sms`.`pbk` (`GroupID`, `Name`, `Number`) VALUES ('1', 'Fred', '+61412345678');
Ideally anyone that has an entry in your
Nagios contacts.cfg file that lists a pager, should be listed in the database.
Tip: Use the same 'Name' in the database that matches the
contact_name in the Nagios contacts file. This will make it easier to maintain in future. You could write a simple script to read the contacts.cfg file and populate the table with contact_names and pager details. That will be an exercise left to the reader!

.
Checking for incoming SMS messages
All incoming messages are stored in the inbox table of your sms database. (Makes sense really!).
Let's take a look how this table is defined:
CREATE TABLE `inbox` (
`UpdatedInDB` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP on UPDATE CURRENT_TIMESTAMP,
`ReceivingDateTime` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`TEXT` TEXT NOT NULL,
`SenderNumber` VARCHAR(20) NOT NULL DEFAULT '',
`Coding` ENUM('Default_No_Compression','Unicode_No_Compression','8bit','Default_Compression','Unicode_Compressio
n') NOT NULL DEFAULT '8bit',
`UDH` TEXT NOT NULL,
`SMSCNumber` VARCHAR(20) NOT NULL DEFAULT '',
`Class` INT(11) NOT NULL DEFAULT '-1',
`TextDecoded` VARCHAR(160) NOT NULL DEFAULT '',
`ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`RecipientID` TEXT NOT NULL,
`Processed` ENUM('false','true') NOT NULL DEFAULT 'false',
UNIQUE KEY `ID` (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
It's now a case we just need to read from the database at regular intervals to determine if we have any new SMS messages.
The easiest way is making use of the 'Processed' field. Notice it can be set to either 'true' or 'false'? We'll make use of this.
SELECT * FROM inbox WHERE Processed='false';
That should now return all our
unprocessed SMS messages.
From there it's just a matter of grabbing the results and processing them.
Processing incoming SMS messages
In order to process incoming SMS messages we need to undertake the following:
- Retrieve all SMS messages not currently processed.
- Read the SMS message
- Based on contents of the message take some action in Nagios
- Once decoded and acted upon the message, update the record to a processed status.
I undertake the tasks using my
readsms.pl script. Please download it and follow the walk-thru below.
Explanation of the code
Let's walk through the script above to explain what we're doing:
- Line 1: Defining that this is a perl script and to display warnings.
- Line 3: Informs perl to use strict checking, ensuring all variables and the like are declared.
- Line 4: Load the perl module DBI )The Database Interface for perl).
- Line 6-36: Define variables that we will be using in the program. Required due to the 'use strict' pragma on line 3.
- Line 38-44: Check for a lockfile. (If it's there the program is already running). If it doesn't exist create it. Open the nagios named pipe (/var/log/nagios/rw/nagios.cmd) that we will append to. ( thus the >> syntax. ) Display errors and exit if any condition fails.
- Line 45-47: Create a DataBase Handle. This connects us to the MySQL server with our username and password. If it fails, display an error and exit.
- Line 49-50: Prepare the SQL queries we wish to run. The first selects all unprocessed incoming SMS messages, whilst the second will select all the names and numbers of people in our phone book.
- Line 52-56: Execute the query to obtain the SMS messages we wish to process. Apply the names of the database columns to each position in the result. This makes it easier to address instead of $row[n].
- Line 58-61: Execute the query to grab all the phone book entries. Place these in the $caller_array. Also determine the number of entries we have in our phone book.
- Line 63-64: Commence working our way through the SMS messages to be processed. Stuff the results one at a time in @row for processing
- Line 65-66: Check that the SMS message received commences with the string "PROBLEM". If so we know it's likely to be a Nagios response.
- Line 67-69: Set the Author of the message to the phone number recorded in the database. This will be used in the case no matching entry is found in the phone book.
- Line 71-76: Loop through every entry in the phone book and see if we have a matching entry. If we do, then set the Author to their real name. The Author variable is what will appear in the Nagios acknowledgment message and helps identify who responded.
- Line 78-80: Split the actual SMS message based on the lines. Each SMS line is an element in the array. Save details of how many lines the message contains.
- Line 82-86: Determine if this is a service message acknowledgment we have received.
- Line 88-91: Grab the Host, Service and Reply from the SMS message.
- Line 92: Build the command we wish to send to Nagios. It is in the format outlined here.
- Line 93: Send the Acknowledgment command to the Nagios named-pipe for processing. Doing so will ensure Nagios processes it, and will display the reply and acknowledgment on the Nagios website. This will also suppress any further notifications during this problem period.
- Line 95-102: Same as the service message acknowledgment (See Line 82-93). Instead of handling Service Messages this handles Host messages. The format of the acknowledgment host problem command we need to generate to send to Nagios.
- Line 105-107: We've processed this message. So lets now update the database record and flag it as processed. By doing so, it ensures we won't read this message in future executions of the script.
- Line 110-119: Close the database, close the Nagios named pipe and also delete our lock file. When cleaned up let's exit the script.
So what have we just done?
The
readsms.pl script has now processed your reply.
Just what has it done then?
It has updated Nagios and marked the issue as being worked on (denoted by the workman

icon), it has also placed the response received as a comment

so others can see both
who and
what is being done.
The Acknowledgment SMS is sent out to all parties who received the initial Problem SMS. By acknowledging the problem, no further PROBLEM messages will be sent out for this issue.
Once the issue has been resolved, a RECOVERY message will be sent out to all parties to complete the PROBLEM -> ACK -> RECOVERY process. The workman icon will disappear, however the comment will remain. This makes it useful for future problem resolution as you can see the history of given problems. (Do they occur at the same time on a regular basis, etc?)
Concluding remarks
Hopefully you have found this 3-part post/tutorial of use.

It's been fun writing it, and reviewing the setup we used.
You will probably want to add the readsms.pl script to your nagios user's
crontab. This will mean it can run at regular intervals to pickup and process any Nagios alert replies. I set mine to run every minute.
You can use the included scripts (They are all licensed under the
GNU GPL v3) - the software used (Nagios, gammu, MySQL and the perl interpretor have their own licensing which you should confirm meets your requirements).
As a programmer, I make a great system administrator.

As such, whilst my code works perfectly fine in my situation, it may not for you. I don't promise that the code won't eat babies.

Having said that, this simple example should show you how easy it is to get 2-way SMS systems up and running with very little investment.
Feel free to expand on what's covered here. You might wish to look at
other commands you can issue to Nagios via your Nagios SMS service. (such as: disable a service, disable notifications, etc).
Gammu's SMSD can be used for other things besides just Nagios alerts. You could use it to send various commands to your server whilst you're physically not in front of a terminal. The possibilities are endless.
For those that want a simple Web-based SMS gateway -- download at
Syed Network's Gammu SMS Gateway. It's very basic and you can build upon it. (It's written in PHP).
Another item for those with either
MythTV machines, would be to setup a service to start recording a given channel at a given date/time for a selected duration. As such -- never miss another show!
Those with
HVAC (a subset of
Home Automation) systems could utilize gammu to 'SMS' into home and set things like lights, heating or the security system.
Feel free to use the comment section to leave your ideas and share your thoughts with myself and others.
Happy SMSing!
This is a 3 part posting that covers how you can setup 2-way Nagios alerting via SMS. The series is broken down as such: Part 1: Background and Requirements (this article)Part 2: Sending SMS messages from NagiosPart 3: Acknowledging SMS messages For
Tracked: May 24, 03:05
This is a 3 part posting that covers how you can setup 2-way Nagios alerting via SMS. The series is broken down as such: Part 1: Background and RequirementsPart 2: Sending SMS messages from Nagios (this article)Part 3: Acknowledging SMS messages Sen
Tracked: May 24, 03:06