Nagios Alerts via SMS with Kapow

I have a client who required a Nagios installation with alerting via SMS (*). They use Kapow as their SMS gateway.

There were two aspects required:

  1. The sending of alerts via the SMS gateway;
  2. The monitoring of available credits on the SMS gateway;

 

1. Send Alerts via SMS Gateway

The sendsms script is:

#! /bin/bash

USERNAME=username
PASSWORD=password
SENDSMSADDRESS="https://www.kapow.co.uk/scripts/sendsms.php"
MAXMSGLENGTH=320

read -n $MAXMSGLENGTH -r MSG

MSG=`php -r "echo urlencode( \"$MSG\" );"`

wget -q -O - "$SENDSMSADDRESS?username=$USERNAME&password=$PASSWORD&mobile=$1&sms=$MSG"

I use a quick hack with PHP to URL encode the string. I didn’t know a shell command off hand but I’m open to suggestions. This can be tested with:

echo This is a test message | sendsms 353861234567

Edit /etc/nagios/misccommands.cfg to include the following:

# 'host-notify-by-sms' command definition
define command{
        command_name    host-notify-by-sms
        command_line    /usr/bin/printf "%b" "Host '$HOSTALIAS$' is $HOSTSTATE$: $OUTPUT$" | /usr/local/bin/sendsms $CONTACTPAGER$
        }

# 'notify-by-sms' command definition
define command{
        command_name    notify-by-sms
        command_line    /usr/bin/printf "%b" "$NOTIFICATIONTYPE$: $SERVICEDESC$@$HOSTNAME$: $SERVICESTATE$ ($OUTPUT$)" | /usr/local/bin/sendsms $CONTACTPAGER$
        }

Ensure your /etc/nagios/contacts.cfg is updated to include notification by SMS with your mobile number:

define contact{
        contact_name                    barryo
        alias                           Barry O'Donovan
        service_notification_period     barryoworkhours
        host_notification_period        barryoworkhours
        service_notification_options    w,u,c,r
        host_notification_options       d,u,r
        service_notification_commands   notify-by-email,notify-by-sms
        host_notification_commands      host-notify-by-email,host-notify-by-sms
        email                           joe@bloggs.com
        pager                           353868765432
}

Sin é.

 

2. Monitor SMS Gateway Credits

The plugin code is:

#! /bin/bash

USERNAME=username
PASSWORD=password
CHECKCREDITSADDRES="https://www.kapow.co.uk/scripts/chk_credit.php"

CRIT=$1
WARN=$2

CREDITS=`wget -q -O - "$CHECKCREDITSADDRES?username=$USERNAME&password=$PASSWORD"`

if [[ -z $CREDITS || ! $CREDITS -ge 0 ]]; then
        echo -e "$CREDITS\\n";
        exit 3;
elif [[ $CREDITS -le $CRIT ]]; then
        echo -e "$CREDITS SMS credits remaining\\n";
        exit 2;
elif [[ $CREDITS -le $WARN ]]; then
        echo -e "$CREDITS SMS credits remaining\\n";
        exit 1;
else
        echo -e "$CREDITS SMS credits remaining\\n";
        exit 0;
fi

Create a plugin configuration file for Nagios, say /etc/nagios-plugins/config/sms_credits.cfg:

# 'check_sms_credits' command definition
define command{
        command_name    check_sms_credits
        command_line    /usr/local/bin/check_sms_credit $ARG2$ $ARG1$
        }

Where $ARG1$ is the warning threshold and $ARG2$ is the critical threshold.

I add the service to the Nagios monitoring box via /etc/nagios/config/sms_credit.cfg:

#
# check sms credits on Kapow - barryo 20070519
#

define service{
        use                             core-service
        host_name                       noc
        service_description             SMS Credits
        check_command                   check_sms_credits!50!100
}

And I believe that’s it.

*) The monitoring box is in a different country to the servers it monitors so a network failure will not prevent the alert getting out.

8 thoughts on “Nagios Alerts via SMS with Kapow”

  1. Hi!

    Thank you (as well) for the scripts, and taking the time to document it – I was looking for a replacement for using twitter/jaiku, which is too unreliable for monitoring… and finding a decent sms gateway seemed to be a pain. Clearly you made my life very easy 🙂

    My only observation – I had to add ‘–no-check-certificate’ to the wget lines (they failed silently otherwise due to your use of -q).

    Thanks
    David.

  2. Thanks for the note David (and Paul).

    I suspect the ‘–no-check-certificate’ was a temporary problem with Kapow as the SSL cert looks good.

    Still, it’s a not a bad idea as one wants to make sure they get their SMS messages.

  3. Thanks for a good walkthrough

    Just added it to our nagios 3 installation and is working fine, subject to minor tweaking on the service names

    Cheers

  4. I know this is not a support forum, but I’m having a problem which has me baffled and you sound like you know what you’re doing so I’m hoping you can help.

    I’ve got the following service command set up:

    define command{
    command_name notify-by-epager
    command_line curl “http://127.0.0.1:13004/cgi-bin/sendsms?username=simple&password=simple&to=$CONTACTPAGER$&text=$NOTIFICATIONTYPE$:+$HOSTALIAS$/$SERVICEDESC$+is+$SERVICESTATE$”
    }

    The curl command calls a SMS gateway we set up on the Nagios server and is tested and working.

    This works fine when the host comes up and reports:

    RECOVERY: MyServer/PING is OK

    When the service goes down, however, some of the variables are missing resulting in a message like the following:

    PROBLEM: MyServer/ is

    The $SERVICEDESC$ and $SERVICESTATE$ variables just don’t show up for the party.

    Any ideas?

Comments are closed.