Tuesday, March 18, 2014

Send sms from Nagios/Icinga through Kannel

Sending notifications from nagios via sms through kannel is not as trivial as it looks first.
Some basics related to this topic:
  1. Nagios accepts only an "one-liner" command to send notifications (command_line parameter of command object). Note that ; means comment here.
  2. Kannel has a web interface which expects the text to be sent urlencoded
  3. curl is a very nice tool to connect to http interfaces, but it is too smart in this case. It means that it has a very useful argument --data-urlencode which URLencodes the text given to this parameter preceeding an "&".
    That "&" causes problem. Kannel expets the message in "text=messagetobesent" format in the url, but data-urlencode encodes "text=" to "text%3D" which kannel does not understand.
So I endedd up with a simple wrapper script, which does the URLencoding only where it is needed.

#!/bin/bash
# ZsZs 2014

trap cleanup EXIT

cleanup() {
  rm -F $TMP 2>/dev/null
}

MSISDN=$1
MSG="$2"

TMP=$(mktemp)
printf '%b' "$MSG" >$TMP
MSGU=$( perl -p -e 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg' $TMP )
curl -G -s "http://localhost:13131/cgi-bin/sendsms?username=KANNELUSER&password=KANNELPW&validity=60&to=$MSISDN&text=$MSGU"

Nagios configuration should look like this:

define command{
        command_name    notify-host-by-sms
        command_line    /usr/local/bin/smssend $CONTACTPAGER$ 'Host: $HOSTNAME$\nState: $HOSTSTATE$\nInfo: $HOSTOUTPUT$\nTime: $LONGDATETIME$'
        }

define command{
        command_name    notify-service-by-sms
        command_line    /usr/local/bin/smssend $CONTACTPAGER$ 'Service: $SERVICEDESC$\nHost: $HOSTNAME$\nState: $SERVICESTATE$\nInfo: $SERVICEOUTPUT$\nTime: $LONGDATETIME$'
        } 

No comments:

Post a Comment