Required software / hardware
The Hardware
In order to send SMS messages you'll need a device that can send SMS messages.
Whilst that might seen pretty logical, it's an important step!
Besides being able to send messages, it also needs to be able to communicate with a computer. This is key.
There are two main methods you can accomplish that.
- A GSM modem.
A GSM modem is like a traditional modem but instead of using a telephone line, it uses the wireless mobile network to make calls. It is traditionally connected to the computer via a serial cable. Newer models also support USB connections. Internal models are also available (using PCI slots and the like). The modem accepts a SIM card like that of a normal mobile phone. - A mobile phone (cell phone).
This is your run of the mill mobile phone that we all seem to carry around. A good choice is to use one of the ones you have discarded (haven't we all) when upgrading to a newer model. Consider this your chance to recycle the perfectly good (but not fashionable) phone. Get alerts and save the environment at the same time instead of filling in more landfill!
There are some pre-conditions to using a generic mobile phone:- It must be able to communicate via a cable directly to your computer.
- It must have a built in modem that supports the AT command set (most mobile phones over the past few years do).
- A working SIM card with a mobile/cell phone operator.
In our instance we decided on a
Sony Ericsson W200i phone. The main reasons were:
- Simple USB connection and seen directly in Linux.
- Has two lines (Line 1 / Line 2) - that are both seen as an AT modems.
- The USB connection can also charge the phone directly off the computer, thus no external power pack was required - set and forget!
- We were already familar with the Sony Ericsson phones (we currently use a K750i mobile) - so parts could be interchanged.

- It was a cheap phone we could buy outright for ~$120AUD.
Other phones will also work, take a look on
TuxMobile to see if yours is supported.
Well that's the hardware catered for, now onto the software!
The Software
I'll prattle off the software required, and then go into details on how to configure it.
- Nagios - obviously; as we want to send alerts from it!

- MySQL relational database system - This is used so we can store/retrieve the SMS messages.
- gammu - Make sure your phone is supported.
NB: If you phone isn't listed but is supported under Linux and has support for the AT modem command sets, it's likely to work in gammu.
. - Perl with DBI and DBD:MySQL bindings.
- Webserver (such as Apache) with PHP support. (optional). You only require this if you want a Web2SMS gateway to send SMS messages from your browser. (Gammu comes with a very simple web-interface, or you can spin your own using the Gammu PHP classes.
Many of these components will be available from your Linux distribution in their native packaging format. There are also unofficial repositories available online where you may find these are bundled for you. (Such as gammu). Failing that, you can grab the source and compile it yourself (don't ya love open source!

).
Installation will be an exercise left to the reader. (I can't do all your heavy lifting!

).
Once installed, you can continue on to configure it as required.
Step 1: Plugging in your GSM source.
Plug in your GSM source (GSM modem or mobile phone) and make sure you can see it natively under Linux. You may need to load additional kernel modules (such as cdc_acm for a USB based phone) or it may pick it up natively and load.
Check /var/log/messages and you should see something like this:
usb 2-2: new full speed USB device using address 2
drivers/usb/class/cdc-acm.c: Ignoring extra header
cdc_acm 2-2:1.1: ttyACM0: USB ACM device
drivers/usb/class/cdc-acm.c: Ignoring extra header
cdc_acm 2-2:1.3: ttyACM1: USB ACM device
usbcore: registered new driver cdc_acm drivers/usb/class/cdc-acm.c: v0.23:USB Abstract Control Model driver for USB modems and ISDN adapters
Okay, so we now have two GSM based modems located at
/dev/ttyACM0 and
/dev/ttyACM1.
If you're having problems seeing your GSM based modem refer to
TuxMobil, and
search Google. You'll find someone's likely already done it.
Step 2: Setting up your database.
Gammu will look for a MySQL database called smsd by default. You can change this if so desired..
Examples of the database schema which you can apply will be found in the examples directory of gammu. (look for a file called
mysql.sql).
Record a username and password for the database that has access to SELECT, UPDATE and DELETE as a bare minimum.
Before proceeding, ensure you can connect via the CLI-based client to the database.
Step 3: Configure gammu.
gammu is the program that will allow you to send/recieve SMS messages.
Getting this configuration correct ensures you will have a trouble free SMS gateway.
Open up the file /etc/gammrc (this is the system wide configuration file). If you don't have one to start with, look in the examples directory, and use the
gammurc file included there as a base.
For my W200i phone this is what my config looks like:
[gammu]
port = /dev/ttyACM0
connection = at19200
startinfo = no
name=Sony Ericsson W200i
synchronizetime = no
use_locking = no
;gammuloc = locfile
;gammucoding = utf8
;logfile = gammulog
;logformat = textall
;model=w200i
The two lines that really matter are the
port and
connection lines. The port should match what Linux reports back to you. (In my case it was /dev/ttACM0 - you're may vary). The connection is the connection type and language spoken. Using the at series, makes it nice and standard, though there is
a range of options for other phone connections.
Now you saved your file, make sure you have your phone connected to your system. Lets test it to see we can talk to the mobile successfully.
# gammu --identify
Manufacturer : Sony Ericsson
Model : unknown (AAB-1022044-BV)
Firmware : R4GB001 R4GB001 prgCXC1250719_GENERIC_ME
IMEI : 123456789012345
SIM IMSI : 987654321098765
NB: Your
IMEI and
IMSI values will be different. I've obviously removed my values and replaced them with generic numeric strings.
Let's see if we can actually read some information back from the phone.
# gammu --monitor 1
Press Ctrl+C to break...
Entering monitor mode...
Enabling info about incoming SMS : No error.
Enabling info about incoming CB : Some functions not available for your system (disabled in config or not written).
Enabling info about calls : No error.
Enabling info about USSD : No error.
SIM phonebook : 0 used, 250 free
Dialled numbers : 0 used, 30 free
Received numbers : 0 used, 30 free
Missed numbers : 4 used, 26 free
Phone phonebook : 3 used, 2497 free
Battery level : 87 percent
Charge state : battery connected, but not powered from battery
Signal strength : -85 dBm
Network level : 100 percent
SIM SMS status : 0 used, 0 unread, 50 locations
Phone SMS status : 0 used, 0 unread, 200 locations
Network state : home network
Network : 505 02 (OPTUS, Australia)
Leaving monitor mode...
Okay all looking good. Each phone has differing levels of functionality, what's important here is that you can see your signal strength and network level. It's also a neat way to check your phone battery level (if your phone supports it (in this case my phone is powered by the USB cable, so the battery is not in use).
Now lets see if we can manually send a SMS message.
# echo "Test message" | gammu --sendsms TEXT <<destination mobile number>>
If you want break, press Ctrl+C...
Sending SMS 1/1....waiting for network answer..OK, message reference=0
If all goes well you'll have an SMS on your phone that says "Test message" from your SMS gateway.
If that's working, you've got the base configuration completed. You can effectively send SMS messages from your computer. Now lets automate the process.
Step 4: Configure the SMS Daemon.
We really don't want to be logging in manually every time we send a message.
The SMS Daemon allows us to easily send messages and store them in a database (both sent and recieve).
This is ideal, as it gives some level of auditing and is then easy to determine if something has gone wrong as well.
So lets set that up.
You'll need to configure the /etc/smsdrc file. You can use the
example smsdrc file as a template.
Here is what my file looks like:
[smsd]
commtimeout = 2
sendtimeout = 30
receivefrequency = 30
deliveryreport = log
user = <<DatabaseUser>>
password = <<DatabasePassword>>
pc = localhost
database = smsd
The smsdrc will use the gammurc file to read the configuration of your mobile phone location.
More details of the various options available can be found in the
Gammu:SMSD wiki page.
Now we need to start the daemon. This daemon controls storing the messages in the backend database, as well as sending the message.
Ideally we want the SMS Daemon to restart whenever it stops. We can accomplish this by using an entry in the /etc/inittab. Here is what my entry reads:
# Setup the SMS Daemon
sm:345:respawn:/usr/local/bin/smsd.sh
Basically this means the shell script
/usr/local/bin/smsd.sh will restart (respawn) whenever it's not running in run level 3, 4 or 5.
Now the contents of the
/usr/local/bin/smsd.sh script are as follows:
#!/bin/sh
export LANG=en_US
/usr/bin/gammu smsd MYSQL /etc/smsdrc
The reason the gammu line doesn't reside directly in the /etc/inittab is due to the fact that in order to run gammu you need to correctly set the LANG (language variable). You could set to en_AU (Australia) if so desired, but I find en_US works just fine.
If you don't have an /etc/inittab file you may be running upstart. If that's the case, you can create a file (say smsd) and place it in the /etc/event.d/ directory.
It would look like:
# smsd
#
# This service maintains the SMS Daemon from the point the system is
# started until it is shut down again.
start on runlevel 3
start on runlevel 4
start on runlevel 5
stop on runlevel 0
stop on runlevel 1
stop on runlevel 2
stop on runlevel 6
respawn
exec /usr/local/bin/smsd.sh
You have finished configuring the SMS Daemon.
Check whether the process is running, (shsd.sh). If it's not issue the command as root:
/sbin/telinit q. This forces the init command to re-read the /etc/inittab file and in the process it will start the smsd process if it's not running.
Those using upstart can use
sudo /sbin/initctl start smsd to control the SMS deamon. Check it's running using
sudo /sbin/initctl status smsd.
Now we've got all the pieces in place, it's time to setup Nagios to send those alerts.
Step 5: Configure Nagios.
Firstly we'll need to put in place a script that uses our newly configured SMS Daemon.
I call this
script /usr/local/bin/sendsms.sh. You'll see it referenced in some of the following configuration as well.
We now have to let Nagios know about the script to call when sending an SMS. Search your nagios configuration (found in /etc/nagios on most systems). You will find a command in one of them called
notify-by-email. We need to add two definitions to that file. On my system it's in the misccommands.cfg file, though you might find yours in commands.cfg or something similar.
Add the following to the same file that contains the notify-by-email definition.
define command {
command_name host-notify-by-sms
command_line /usr/
local/nagios/sendsms.sh host
}
define command {
command_name notify-by-sms
command_line /usr/
local/nagios/sendsms.sh service
}
Now edit your contacts. You'll notice we add the host-notify-by-sms and the notify-by-sms definitions as well as our mobile number to the contact (the pager field).
define contact{
contact_name matt
alias Matt Bottrell
service_notification_period 24x7
host_notification_period 24x7
service_notification_options u,c,r
host_notification_options d,u,r
service_notification_commands notify-by-sms,notify-by-email
host_notification_commands host-notify-by-sms,host-notify-by-email
email your@email.com
pager 04XXXXXXXX
}
That's my contact information.... and as such I get both Emails and a SMS message for any service or host issues. You'll need to modify all the contacts you want to receive SMS alerts. You'll also find it's easier to great a contactgroup for these individuals.
define contactgroup{
contactgroup_name admins-sms
alias Administrators via SMS
members matt,pauline,fred,george,lewis
}
Now all you need to do is update the services/hosts you wish to be contacted by SMS.
define service{
use generic-service
host_name MyServer
service_description / partition
is_volatile 0
check_period 24x7
max_check_attempts 5
normal_check_interval 15
retry_check_interval 3
notification_options w,u,c,r
notification_interval 30
notification_period 24x7
contact_groups admins-sms
check_command check_local_disk!20%!10%!/
}
Once you've added the contact groups to all the services you wish you can now check the nagios configuration.
Issue
nagios -v /etc/nagios/nagios.cfg and check there are no warnings and errors. It should appear something like this:
.....
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
If you're all good, you can then reload your nagios configuration (
/etc/init.d/nagios reload).
You should now be able to receive SMS alerts via Nagios.
Coming Up In the next part of the series...
Part 3 of the series covers acknowledging (replying to) the SMS messages you receive from the Nagios system. We'll also cover some of the other items you can use the SMS gateway for (such as a Web2SMS interface).
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 23, 01:29
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 NagiosPart 3: Acknowledging SMS messages (this article) Rev
Tracked: May 24, 02:57