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.