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.

O2 XDA Exec as a Modem

I recently purchased an O2 XDA Exec to fulfill by on-call/support obligations. With a trip away this weekend, I decided to play it safe and just make sure it worked as a modem for an install of Windows hidden away on a tiny partition of my laptop (which of course includes PuTTY).

I have actually installed PuTTY on the XDA as well and it works great but it’s only really practical for quick fixes or real emergencies when it’ll “have to do”.

The use of the XDA as a modem for accessing the Internet from a PC via USB is not very intuitive but fortunately someone else had done all the work: http://www.pcurtis.com/xda.htm. Their instructions are reproduced below for prosperity. The Internet access point for O2 in Ireland is internet and the GPRS username/password is gprs/gprs.

Use of XDA as a Modem for accessing the Internet from a PC via USB

Although the comprehensive handbook and advertising material indicated this was possible it took considerable research on the Internet to implement the use of the XDA as a modem for GPRS/3G access from a PC. This was not entirely unexpected as it took a very long time to find out how to do the same with our Sony T610 phone. We have finally succeeded and are providing detailed instructions which work for a USB cable.

The following procedures have only been tested with the XDA so far but should be applicable to any PDA running Windows Mobile 5. I have therefore used PDA instead of XDA throughout this section.

Firstly one must understand that use of PDA as a modem for accessing the Internet involves running a built in program called Wireless Modem on the PDA which simulates a modem on the PDA USB port (or on the Bluetooth or IR connections). When this program is running it is just like an old fashioned external modem box with a screen display with an online and data lights – when the program is running the USB connection is completely different and when it is plugged in it will be recognised as a new modem device. One must therefore install a USB modem on your computer which uses the special driver supplied on the applications disk with the PDA. Only then can you set up a Dial-up internet connection using the PDA.

The use of GPRS/3G requires the setting up the PDA modem by a special initialisation string with details of the GPRS access point or, in some cases it will be already set if you have just made a connection using the required access point. You then connect (dial) using a magic code of *99# instead of an ordinary telephone number. You need to know the access point, username and password for you providers GPRS/3G service to do this in the case of O2 the access point is mobile.o2.co.uk , the user name is web, o2web or faster and the password for all of them is password. In the case of Vodafone UK prepay the access point (APN) is pp.vodafone.co.uk , the username is wap and the password is also wap . Try Ross Barkman’s Page or http://www.formatc.de/roaming/gprs.htm for complete sets of GPRS settings world wide.

So in detail:

  1. Disconnect the PDA USB cable from your computer if it is plugged in
  2. If you have installed ActiveSync on the computer it should be disabled by Right clicking the ActiveSync icon in your computer tooltray, open “Connection Settings” and Uncheck “Allow USB Connection”.
  3. On your PDA: Go to “Beam” (in Settings/Connections), and uncheck “Receive all Incoming beams”.
  4. Turn on the telephone in Wireless settings and check you have a signal.
  5. Then in Programs, click on “Wireless Modem” click on Tools/Options and Check the Preset GPRS connections and write in the APN, your network provider’s internet access point (mobile.o2.co.uk for O2 and pp.vodafone.co.uk for Vodafone) and click OK. Set the Connection type to USB and click on “Start” and leave the program running.
  6. Now, connect your PDA to the computer with the USB cable and after a few seconds the “Found New Hardware Wizard” should start on your computer. Click “Install from a specific location”, then “Don’t search. I will choose.”, then click on “Modems”, and “Have Disk.” and go to the folder on the CD provided which contains the HTC USB Driver. The HTC USB Modem will be shown in the compatible hardware: click to install. Continue if you receive the alert of the software not passing the Windows XP compatibility test (Continue anyway). Click Finish.
  7. Now, in Control Panel of your computer, open “Phone and Modem Options”. Select the “HTC USB Modem” and click on “Properties”. In the “Advanced” tab type or cut and paste into “Extra initialization commands” the following string AT+CGDCONT=1,”IP”,”xxx”,””,0,0 where xxx is your internet access point ie for Vodafone the string is AT+CGDCONT=1,”IP”,”pp.vodafone.co.uk”,””,0,0 . Click Ok to finish.
  8. Finally, on your computer, go to “Network Connections” and click “Create a new connection”. Choose “Connect to the internet”, then “Set up my connection manually”. Click “Connect using a dial-up modem”, then give a name to your ISP. In phone number type *99#. Get username and password from your network provider – for Vodafone UK use wap for both
  9. Make a shortcut of this connection on your desktop for quick access.
  10. NB ALWAYS use the same USB connector or you will need to set up another!

    Every time you want to use the PDA as a modem:

    1. Disconnect PDA USB cable if connected
    2. If necessary Disable ActiveSync on your computer as above by unchecking the “Allow USB connection”
    3. If necessary turn on the telephone and check for a signal.
    4. Open Wireless Modem program on your device and push on Start (with the Connection Type on USB) and leave it running.
    5. Connect your device USB cable (always using the same connector on the PC )
    6. Dial from your desktop using the shortcut you created

    After you have finished:

    1. Close the connection on the computer to stop being charged then
    2. Remove the USB cable before you
    3. Close the Wireless Modem program but remember to
    4. Enable ActiveSync by re-enabling the USB connection before you
    5. reconnect the USB cable (always using the same connector on the PC) when you want to start synchronisation.

Coming soon: O2 XDA Exec, GPRS, USB and Linux.