So I find myself needing to tweak my Nagios installation a little bit, in this case I found the need for “out of hours” SMS alerts.

Nagios doesn’t cater for this natively, rather it does however allow you to create your own custom commands, this allows you to specify a script to be executed.

Now I am going to assume you are already quite familiar with Nagios , so here is the command definition from my installation.


# ‘alert-by-sms’ command definition
define command{
command_name alert-by-sms
command_line /etc/nagios/alert-by-sms.php “** $NOTIFICATIONTYPE$ alert – $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **”
}

As you can see all this command definition realy does is execute a php script, bear in mind that

“/path/to/php /path/to/script ”

as the command_line does not seem to work, so just add “#!/path/to/php -q” to the top of the php script (before the opening <?PHP tag). and CHMOD +X the file.

The php script used here takes $argv[1] and passes it into a function specific to the SMS api I use, the phone number and API definitions are hard coded ito the script.
You don’t really need me to upload my script, and if you do then you shouldn’t be attempting this …

Basically Nagios will execute the script, as defined at command_line, the script can do anything you choose.

Now to implement the command so it is actually used, I am pretty sure this entry in “timeperiods.cfg” is the default but just incase here it is.

# ‘nonworkhours’ timeperiod definition
define timeperiod{
timeperiod_name nonworkhours
alias Non-Work Hours
sunday 00:00-24:00
monday 00:00-09:00,17:00-24:00
tuesday 00:00-09:00,17:00-24:00
wednesday 00:00-09:00,17:00-24:00
thursday 00:00-09:00,17:00-24:00
friday 00:00-09:00,17:00-24:00
saturday 00:00-24:00
}

This is what I use for the “out of hours” definition, now to implement the SMS alerting, for this I have simply created a new contact definition in “contacts.cfg”, granted this means there are now two contact definitions for myself.

define contact{
contact_name out_of_hours
alias Out Of Hours Mobile

service_notification_period nonworkhours
host_notification_period nonworkhours
service_notification_options c,u,r,f
host_notification_options d,u,r
service_notification_commands alert-by-sms
host_notification_commands alert-by-sms
email HIDDEN EMAIL

}

This can be further customized depending on your setup, in this case the contact is me and I want to receive alerts for all servers & services, so I just add the contact “out_of_hours” into the admins contact group.

define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagios-admin,out_of_hours
}

So there you have it, you now have the ground work to potentially make Nagios fire you alerts anyway you like, you could go as far as having it call you via attached modem, if you _realy_ want, but when you want your servers talking to you via phone call is the day you need to switch to decaff, and head out to the pub once in a while.

Now just “nagios -v /path/to/nagios.cfg” to do a quick sanity check and make sure there are no errors (if you have any go back and fix them and run nagios -v again!), if all is ok /etc/init.d/nagios restart (or equivalent for your distribution).

As always if you run into problems drop me a comment :-)

Tags: , , , , , , , , , ,
9 Responses to “Nagios customization: Alerting via SMS, or anything you like!”
  1. David White says:

    Hey, that looks pretty cool. Can’t say I’d use it personally, but in the company I’ve done work for, I bet they could find something useful out of that…

    Keep up the good work!

  2. Hey I wrote a script for sending SMS’s recently and it could easily be added to nagios.

    My script sends me an SMS when machines on my network stops responding to my pings.

    Check it out http://www.mullineux.org/blog/?p=8

    I send the text messages using a very simple facebook App as it is the only way I could find to do it for free.

    Regards

    Louis

  3. Buzz says:

    Hi Louis,

    Took a look at that, not sure if facebook would ban the accounts though for that little ree sms loophole, depends how many SMS’s you are sending I suppose :-)

    I’ve moved onto adding twitter support since this post, you can use either http://www.saiweb.co.uk/linux/update-twitter-in-a-single-line or http://www.saiweb.co.uk/python/making-the-bird-tweet-using-python

    Cheers

    buzz

  4. [...] Nagios customization: Alerting via SMS, or anything you like! [...]

  5. duke says:

    hello bro…

    can you plz tell me that where is this file—-> “alert-by-sms.php” . as you have included it in your command…

    plz help me out …

  6. Joey says:

    Hi ,

    Where I can find this alert-by-sms.php ?

    And how you are sending sms – to sms gateway or directly to mobile number?

    Any thoughts?

  7. Joey says:

    Hi ,

    Can you pls tell whether is that alert-by-sms.php script ?

    And also are using sms-gateway for sms alert sending ?

    Thanks in advance!!!

  8. Buzz says:

    Hi Joey,

    Yes it is an executable script, that connects to a sms gateway to send the message.

    Cheers

    Buzz

  9. Buzz says:

    I’ve since moved to python, an example can be found here that uses the old clickatell http api: https://github.com/Oneiroi/sysadmin/blob/master/tools/clickatell.py

  10.  
Leave a Reply