Internet, UNIX, Video, Leisure…
Sending SMS from Nagios

Sometimes you need to be able to send SMS from a computer. This is obvious when you’re talking of monitoring your servers. Email alerts are good, but what if you’re sleeping ?
I used to set up a SMS modem, with it’s own SIM card and subscription. This is really easy, using smsd daemon, to send SMS. Just put a formated file in a directory and bam!, you SMS is fired.
But what when you can’t set a SMS Modem in a datacenter ?
Then you have to use an online SMS provider.
you just have to call an API to send a SMS
it’s not free (you pay per SMS when the modem can have an unlimited SMS sending subscription
it is network dependant ; if you have a network failure, you will never be informed
Once you know that, you can start using it.
I will set up two monitoring plateforms : one inside the datacenter, monitoring every services and local network and one outside, on a hosted server somewhere else, to check the network connecivity of the datacenter.
As our main monitoring is using Nagios, I had to write scripts for it. I now have 2 python scripts : one for sending SMS, one to check how many SMS I have on my account.
I first had a quick look on the french SMS providers. I chosed www.smsenvoi.com as they are one of the cheapest and the http and mail API are free (yes, some providers make you pay for that !!). They are also based in Marseilles. I’m happy thinking people there can do better than people from Paris
So, let’s dig the http API. You can find it at http://www.smsenvoi.com/pgs_api.php
It’s quite simple, even if ugly : you send data in POST request, like : userLogin=your.account@domain.com&userPassword=your_password_clear&…
And call a page, depending on your need :
- smsEnvoi.php : send a message
- smsNumber.php : see how many SMS you have availlable in your account
- smsStatus.php and smsMNC.php : check if a SMS have been sent (this is used when you do mass sending)
You can do pretty neat things. Just check on their website what the URL’s are.
To be honest, I only re-write, in a simple way, what the PHP code do but in Python.
You can find the two scripts attached.
sendSMS.py take a message from the line input and send it. You have to start it for each recipient. Even if it’s easy to add many recipient at the same time, Nagios can’t do that. I’m just keeping it simple.
check_sms_free is a script (nagios check formated) to check how many SMS you still have in your account. The Nagios check is defines to be at WARNING state when only 50 SMS are availlable and in CRITICAL state when only 10 are.
Here are the Nagios config (I do have it in Nconf, but here is what is generated in the statis Nagios file) :
command_name host-notify-by-sms
command_line /usr/bin/printf "%b" "Type: $NOTIFICATIONTYPE$ – Host: $HOSTALIAS$ – Address: $HOSTADDRESS$ – State: $HOSTSTATE$ – Date: $LONGDATETIME$ – Info: $HOSTOUTPUT$" | /opt/monitor/nagios/scripts/sendSMS.py –login=$USER8$ –passwd=$USER9$ –smsserver=www.smsenvoi.com –receivers=+$CONTACTPAGER$ –smsfrom=monitor-prod
}
define command {
command_name notify-by-sms
command_line /usr/bin/printf "%b" "Type: $NOTIFICATIONTYPE$ – Service: $SERVICEDESC$ – Host: $HOSTALIAS$ – State: $SERVICESTATE$ – Date: $LONGDATETIME$ – Info: $SERVICEOUTPUT$" | /opt/monitor/nagios/scripts/sendSMS.py –login=$USER8$ –passwd=$USER9$ –smsserver=www.smsenvoi.com –receivers=$CONTACTPAGER$ –smsfrom=monitor-prod
}
define command {
command_name check_sms_free
command_line $USER1$/check_sms_free –login=$USER8$ –passwd=$USER9$ –smsserver=$ARG1$ -w $ARG2$ -c $ARG3$
}
Define the username and password for the smsenvoi account in resource.cfg as $USER8$ and $USER9$
Then, you can add the check to a host :
service_description check_sms_free
check_command check_sms_free!www.smsenvoi.com!50!10
host_name localhost
check_period 24×7
notification_period 24×7
contact_groups admins,sysadmins
event_handler_enabled 0
active_checks_enabled 1
passive_checks_enabled 1
notifications_enabled 1
check_freshness 0
freshness_threshold 86400
use Default_timeperiod_interval_5
}
Don’t forget to define the cellphone number of each users in the « PAGER » field :
contact_name @lecentre.net-sms
alias sms lecentre
host_notification_options d,r
service_notification_options c,r
email @lecentre.net
pager +33682887766
host_notification_period 24×7
service_notification_period 24×7
host_notification_commands host-notify-by-sms
service_notification_commands notify-by-sms
}
Note the format of the phone number, with international format, starting with +
And voila…
Find the two scripts here : sendSMS.py and check_sms_free
Todos :
- limit the message size to 160 caracters, which is the max for a single SMS. If you send messages longer, they will count as two SMS.
- catch every exception in the httplib Python framework (this is really basic for the moment)
1 mars 2010 - 02:15
thanks for this article. i ll try it..
5 août 2010 - 16:12
Bonjour,
Je suis très intéressé par smsStatus.php and smsMNC.php
car je suis chez SMSEnvoi et je ne sais comment décripter le json de retour
Merci d’avance
Cordialement
Hello
I am very interested and smsStatus.php smsMNC.php
because I’m at SMSEnvoi and I do not know how to decipher the json return
Thank you in advance
Cordially
6 août 2010 - 17:10
Le JSON de retour est décrit dans la doc de l’API V2.
Il n’y a pas plus d’infos qu’avant, c’est juste la manière de le retourner qui change…
Les fichiers en python sont sur le blog, tu peux les telecharger. Sinon pose des questions plus précises (mais je serais out-of-the-office pour 2 mois a partir de la semaine prochaine…)