How to configure Nagios to send sms to your mobile

Nagios is an open source host, service and network monitoring program. The official homepage is http://nagios.org/, and i use it with cacti to monitor my network.
Nagios sends you an alert via email, sms when a service is down, the sms solution is very usefull when your smtp server is unreachable.
You can drive a GSM mobile by your favorite operating system. With an usb data cable connect it to your monitoring server.
On debian there are a too packages for communicating with mobile phones (gnokii and gsm-utils), at first we’ll use gsm-utils you can get it like:
$ sudo apt-get install gsm-utils
Verify first that your GSM is connected to your server
$ sudo ls -al /dev/ttyUSB*
You can now test it by sending a sms to your mobile number +3299999999.
$ sudo gsmsendsms -d /dev/ttyUSB0 +3299999999 “Nagios – Test SMS OK”
If the test is OK, that mean you will be able to configure nagios to send sms to your phone.
Add a pager to your contact info in /etc/nagios3/conf.d/contacts_nagios2.cfg and set up sms notification mode like:

define contact{
contact_name mezgani
alias Ali MEZGANI
service_notification_period 24×7
host_notification_period 24×7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email,notify-service-by-sms
host_notification_commands notify-host-by-email,notify-host-by-sms
email mezgani@secufox.org
pager +329999999999
}

Now you have to define a command, which talks to our sms-notification-software,
so add these lines to your /etc/nagios3/commands.cfg

define command{
command_name notify-service-by-sms
command_line /usr/bin/gsmsendsms -d /dev/ttyUSB0 $CONTACTPAGER$ “Nagios – $NOTIFICATIONTYPE$ : $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ ($OUTPUT$)”
}

define command{
command_name notify-host-by-sms
command_line /usr/bin/gsmsendsms -d /dev/ttyUSB0 $CONTACTPAGER$ “Nagios – $NOTIFICATIONTYPE$ : Host $HOSTALIAS$ is $HOSTSTATE$ ($OUTPUT$)”
}

At this point, you’ve done.
For gnokii users, you can also get it by aptitude as:
$ sudo aptitude install gnokii

Send you a sms test by:
$ echo “Hello world!” | gnokii –sendsms +329999999999 -r

Later, add these lines to your /etc/nagios3/commands.cfg

define command {
command_name notify-service-by-sms
command_line /usr/bin/printf “%.120s” “Nagios – $NOTIFICATIONTYPE$ : $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ ($OUTPUT$)” | /usr/local/bin/gnokii –sendsms $CONTACTPAGER$ -r
}

define command {
command_name notify-host-by-sms
command_line /usr/bin/printf “%.120s” “Nagios – $NOTIFICATIONTYPE$ : Host $HOSTALIAS$ is $HOSTSTATE$ ($OUTPUT$)” | /usr/local/bin/gnokii –sendsms $CONTACTPAGER$ -r
}

Well, i can not get a dedicated GSM for my monitoring server right now, in my country there are some providers that purpose some sms services like sending free sms via web to mobile for example meditel.
in the suite we will focus on how to configure nagios to send sms to meditel mobile via a python script.
If your operators is meditel, get meditelsms package :
$ git clone git://github.com/mezgani/meditelsms.git.
and now add this following lines to your commands.cfg, change the path of your script in our example i used /home/mezgani/script/sms.py

define command{
command_name notify-host-by-sms
command_line /usr/bin/printf “%.120s” “Nagios – $NOTIFICATIONTYPE$ : Host $HOSTALIAS$ is $HOSTSTATE$ ($OUTPUT$)” |/usr/bin/python /home/mezgani/script/sms.py $CONTACTPAGER$
}

define command{
command_name notify-service-by-sms
command_line /usr/bin/printf “%.120s” “Nagios – $NOTIFICATIONTYPE$ : $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ ($OUTPUT$)” |/usr/bin/python /home/mezgani/script/sms.py $CONTACTPAGER$
}

Advertisements