SMS Nagios Notifications with PHP & Twilio

I’ve had a few requests to share my Nagios SMS notifications using Twilio. I’m almost embarrassed to share them, since they are so dead simple. There was another plugin out there to do the same, but it was a lot more advanced and more work to setup, so I wrote my own in PHP.

In the past I would just use my iPhone’s email-to-txt email address. However, when I received the txt message, it wasn’t formated very pretty, and it would have a different “From Number.” So if we had a crazy day, I would have 20-30 message threads in my iPhone all about Nagios. I’ve been lazy in the past, and didn’t clear them out until I had about 200 of them, and it was a lot of “swipe, delete, swipe, delete, swipe, delete” to get rid of them.

What I like out this setup is with Twilio, I can buy a phone number for $1 a month. So all my notifications come through the same number. I’m also planning a way to call/text the number and it will read/send the current status and list the hosts that are down.

First thing is to get your Twilio account setup. Its pretty easy, and when I signed up (a long time ago) I got $30 in credit, not sure if they are doing that still for new customers. Either way you can add a credit card and say how much credit you want to add when your account dips below a certain amount.

Now, you can buy a number and set it up. Even if you’re only sending text messages, Twilio wants you to have a “SMS Request URL” before you can send any text messages. So I just put in a URL to my site that does nothing (yet). You’ll need to grab your Account SID and Token.

How it Works

I threw the code up on GitHub. There are a few things you’ll need to do:

First in sendTextMsg.php change your configurations to match your phone number, account SID and token. Here is the entire sendTextMsg.php file:

[php]
<?php

require(‘twilio-php/Services/Twilio.php’);

/* Start Configs */

$sid = "A123…..";
$token = "29d6b9f…….";
$twilio_number = ‘4045550101’;

/* End Configs */

# Get the Argvs
$phone = $argv[1];
$msg = $argv[2];
$msg = str_replace(‘n’, "n", $msg);

$client = new Services_Twilio($sid, $token);
try
{
$message = $client->account->sms_messages->create(
$twilio_number, // From a valid Twilio number
$phone, // Text this number
$msg
);
} catch (Exception $ex)
{
var_dump($ex);
}
[/php]

As you can see, it is super simple. All you need to do is put this script somewhere with the Twilio PHP libraries (or just use my github code with all of it in there). Then, in your Nagios configurations add the commands:

define command{
    command_name    notify-host-by-txt
    command_line    /usr/bin/php /path/to/sendTxtMsg/sendTxtMsg.php "$CONTACTPAGER$" "Nagios AlertnType: $NOTIFICATIONTYPE$nHost: $HOSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nWhen: $LONGDATETIME$"
}

define command{
    command_name    notify-service-by-txt
    command_line    /usr/bin/php /path/to/sendTxtMsg/sendTxtMsg.php "$CONTACTPAGER$" "Nagios AlertnType: $NOTIFICATIONTYPE$nService: $SERVICEDESC$nHost: $HOSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nWhen: $LONGDATETIME$"
}

Then you need to add it to your contacts:

define contact {
        contact_name                    justin
        alias                           Justin Carmony
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r
        host_notification_options       d,r
        
        # Add the new commands to your list
        service_notification_commands   notify-service-by-email,notify-service-by-txt
        host_notification_commands      notify-host-by-email,notify-host-by-txt
        email                           justin@example.com
        
        # Add a pager number, cause we rock it old school 
        # like that: http://static.howstuffworks.com/gif/restaurant-pager-motorola.jpg
        pager                           4045551234
}

Simple, easy, and works very well. You can test the script by simply executing the command:

php /path/to/sendTxtMsg.php 4045551234 "This is my test.nI am awesome.n"

Enjoy!

4 thoughts on “SMS Nagios Notifications with PHP & Twilio

  1. The easiest way is to try this: http://www.weekwill.com/blog/nagios-sms-alerts-notification-mobile-phone

    It sends out SMS alerts when you receive emails from Nagios. It works great. Try it.

    Like

  2. How do you handle having multiple contact profiles with different pager numbers?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close