#!/bin/bash

# kannel monitoring
# made by Dju - 11/2013

PLUGIN_VER="1.3"
PROGNAME=$(basename $0)
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
KANNEL_HOST=""
KANNEL_PORT=13000
CURL_TIMEOUT=10
# check QUEUE_CHECK=1 to check smpp messages in queue, and how many to set warning/critical treshold
QUEUE_CHECK=1
QUEUE_WARN=4500
QUEUE_CRIT=5000

while [ -n "$1" ]; do
  case "$1" in 
    -H)
      KANNEL_HOST=$2
      shift
      ;;
    -t)
      CURL_TIMEOUT=$2
      shift
      ;;
    -p)
      KANNEL_PORT=$2
      shift
      ;;
    -qw)
      QUEUE_WARN=$2
      shift
      ;;
    -qc)
      QUEUE_CRIT=$2
      shift
      ;;
    -h|--help)
      echo "Usage: $PROGNAME -H kannel_host [-t timeout] [-p port] [-qw queue_warning] [ -qc queue_critical ]"
      exit 0
      ;;
  esac
  shift
done

. $PROGPATH/utils.sh

if [ -z "$KANNEL_HOST" ]; then
  echo "ERROR: no host given"
  exit $STATE_UNKNOWN
elif [ -z "$KANNEL_PORT" ]; then
  echo "ERROR: wrong port number"
  exit $STATE_UNKNOWN
elif [ -z "$CURL_TIMEOUT" ]; then
  echo "ERROR: wrong timeout"
  exit $STATE_UNKNOWN
fi

KANNEL_URL="http://${KANNEL_HOST}:${KANNEL_PORT}/status"
CURL_BIN=$(which curl)
if [ -z "$CURL_BIN" ]; then CURL_OK=0; else CURL_OK=1; fi

get_status()
{
   KANNEL_STATUS=$($CURL_BIN -s -m $CURL_TIMEOUT $KANNEL_URL 2>&1)
   status_get=$?
   if [ $status_get -ne 0 -o -z "$KANNEL_STATUS" ]; then
      echo "ERROR: no http response"
      exit $STATE_CRITICAL
   fi
   LIST_SMPP=$(echo "$KANNEL_STATUS" | grep 'SMPP:.*(' | sed 's/,//g')
   DLR_NB=$(echo "$KANNEL_STATUS" | awk '/^DLR:/ {print $2}')
   DLR_ST=$(echo "$KANNEL_STATUS" | awk '/^DLR:/ {print $5}') 
   KANNEL_VER=$(echo "$KANNEL_STATUS" | awk '/^Kannel/ {print substr($NF,2,5);}')
}

if [ $CURL_OK -eq 1 ]; then
   get_status
   check=$(echo "$KANNEL_STATUS" | grep 'Status: running')
   if [ -z "$check" ]; then
      echo "WARNING: kannel status NOT running"
      exit $STATE_WARNING
   fi
   no_boxes=$(echo "$KANNEL_STATUS" | grep -i "no boxes connected")
   if [ -n "$no_boxes" ]; then
      echo "CRITICAL: No boxes connected"
      exit $STATE_CRITICAL
   fi
   echo "$LIST_SMPP" | while read line; do
      op=$(echo $line | awk '{print $1}')
      vals=$(echo $line | grep -o '(online.*)')
      smpp_host=$(echo $line | awk '{ split($2,s,":"); print s[2]"/"s[4] }')
      if [ -z "$vals" ]; then
         echo "WARNING: smpp ${op} (${smpp_host}) NOT online"
         exit $STATE_WARNING
      else
         if [ $QUEUE_CHECK -eq 1 ]; then
            nb_q=$(echo $vals | sed 's/.*queued \([0-9]*\) .*/\1/')
            if [ $nb_q -ge $QUEUE_CRIT ]; then
               echo "CRITICAL: $nb_q queued on $op/$smpp_host"
               exit $STATE_CRITICAL
            elif [ $nb_q -ge $QUEUE_WARN ]; then
               echo "WARNING: $nb_q queued on $op/$smpp_host"
               exit $STATE_WARNING
            fi
         fi
      fi
   done
   status=$?
   if [ $status -eq 0 ]; then
     echo "Kannel OK - version: ${KANNEL_VER} - dlr storage: ${DLR_ST} - dlr nb: ${DLR_NB} | dlr_nb=${DLR_NB}"
   fi
   exit $status
else
   echo "cant connect to kannel (curl not found/executable)"
   exit $STATE_UNKNOWN
fi
