I was recently playing around with pygooglevoice and after growing tired of texting all my friends /dev/urandom, I decided to try and do something useful with it: make Nagios send alerts via SMS on the cheap.
First step is to hack up a python script to take standard input from a pipe and send it the way of googlevoice:
#!/usr/bin/env python2 from googlevoice import Voice from googlevoice.util import input import sys voice = Voice() voice.login('abc@mailinator.com','somepassword'') message = sys.stdin.read()[:160] voice.send_sms(11234567890,message) # print(str(message)) voice.logout
Extra emphasis on hacky, right? Maybe one day I’ll get around to removing the hardcoded nastiness or hashing the clear-text passwords but for now this is just a fun proof of concept. I saved this to /usr/bin/et_phone_home.py and made it executable. Now is a perfect time to test this out:
echo "test woot" | /usr/bin/et_phone_home.py
EDIT: It appears the API for logging in to google voice has changed and pygooglevoice 0.5 raises “LoginError”. This should be fixed upstream or can be manually fixed in settings.py as per http://code.google.com/p/pygooglevoice/issues/detail?id=58#c31
Now, in your Nagios installation check out the commands.cfg
file. Mine happens to live in /etc/nagios/objects/
but the location of yours may differ. Based on the syntax of the run-of-the-mill email notifications, we can muster up something similar for our little python script above; one for a host notification and one for a service notification:
define command{ command_name notify-host-by-gv command_line /usr/bin/printf "%b" "$NOTIFICATIONTYPE$ on $HOSTNAME$\nState: $HOSTSTATE$ Info: $HOSTOUTPUT$" | /usr/bin/et_phone_home.py } define command{ command_name notify-service-by-gv command_line /usr/bin/printf "%b" "$NOTIFICATIONTYPE$ $SERVICEDESC$ on $HOSTALIAS$\nState:$SERVICESTATE$ Info:$SERVICEOUTPUT$" | /usr/bin/et_phone_home.py }
I made myself a contact inside a contact group by adding the following to contacts.cfg
define contact{ contact_name gv use generic-contact alias Google Voice } define contactgroup{ contactgroup_name gv alias Google Voice members gv
If the shell script wasn’t so hacky, we could probably add multiple users with phone numbers defined via the pager
variable Nagios has and pass that along to the shell script to text multiple people.
Next step is to edit my templates.cfg
and add the gv group to my already existing critical-services template:
define service{ name critical-service ; The 'name' of this service template ... contact_groups admins,gv
Now when services or hosts that use the critical-service generate notifications, I see a cute (annoying) SMS alert on my tele:
From: Google Voice PROBLEM HTTP on test_host State:CRITICAL Info: Connection refused
With a bit of polish, this could stack up fairly well against the high cost of a dedicated SMS gateway device or the hassle of tethering your phone to the machine and using something like gnokii to accomplish this. Unfortunately, since the above method doesn’t cellular technology, you won’t be getting any SMS notifications when your ISP or default gateway bites the dust 🙂