#!/bin/bash
#
# For use with BIP Kampany SMS API. This is a basic script that calls 
# the 'api.php' script on the BIP Kampany server
# then parses the result to see if the message was sent or rejected.
#
# You need to handle the following script exit codes:
#  0  - OK
#  1  - ERROR 
#  65 - bad number of parameters OR unexpected BIP API answer
#
# Requirements:
#   - BIP SMS account (signup at www.bipkampany.hu)
#   - curl for calling remote URLs
#   - iconv for converting message to UTF-8
#
# This is intended for use with Nagios notifications but could be adapted to other uses.
#
# Note: a positive message using BIP SMS means that it was accepted and queued. It is up to the
# BIP Kampany to process the SMS queue, determine if the account has credits.
# This method has the potential to overload your SMS account if used with
# high numbers of Nagios Notifications.
# 
# -----------------------------------------------------------------------
# 
# Copy this file to your Nagios plugin folder
# On a Centos install this is /usr/lib/nagios/plugins or 
# /usr/lib64/nagios/plugins, other distributions may vary.
#
# SETUP
#
# 1. Set your BIP SMS credentials below:
#    (you need a BIP account to use the service)
EMAIL="youremail@example.com"
PASSWORD="yourpassword"
SENDERID="BipKampany" 

# SENDERID will be shown as the sender on the mobile device.
# Set your own sender ID in your account through the website.
#
# 2. Create the SMS notification commands.  (Commonly found in commands.cfg)
#
# define command{
#         command_name    notify-by-sms
#         command_line    $USER1$/bipSMS.sh $CONTACTPAGER$ "Service: $SERVICEDESC$ Host: $HOSTNAME$ Address: $HOSTADDRESS$ State: $SERVICESTATE$ Info: $SERVICEOUTPUT$ Date: $LONGDATETIME$"
# }
# define command{
#         command_name    host-notify-by-sms
#         command_line    $USER1$/bipSMS.sh $CONTACTPAGER$ "Host $HOSTNAME$ is $HOSTSTATE$ Info: $HOSTOUTPUT$ Time: $LONGDATETIME$"
# }
#
# 3. In your nagios contacts (Commonly found on contacts.cfg) add 
#    the SMS notification commands:
#
#    service_notification_commands	notify-by-sms
#    host_notification_commands		host-notify-by-sms
#
# 4. Add a phone number to your contacts
#
#    pager	36201234567
#
# ------------------
#
# For more information on the BIP SMS gateway, please refer to
# www.bipkampany.hu
#

#CURL_CONNECTION_TIMEOUT=30	# set the timeout for the connection phase of the curl; in seconds
#CURL_MAX_TIME=40		# set the max length of the curl connection; in seconds

## Parameter Checking
EXPECTED_ARGS=2         #expected number of arguments
EXIT_ERRORCODE=65       #exit code with bad arguments or unexpected results

if [ $# -ne $EXPECTED_ARGS ]; then
  echo
  echo "Usage: $0 <phone number> <message text>."
  echo "<phone number>:   Phone Number (international format, eg. 36301234567)"
  echo "<message text>:   The text of the message in quotes. ex. \"message content\""
  echo
  echo "For use with BIP Kampany SMS (www.bipkampany.hu)."
  echo "Uses curl to call http://api.bipkampany.hu/ targets,"
  echo "parses the result, returns 0 for positive, 1 for negative, and 65 for error"
  echo
  exit $EXIT_ERRORCODE
fi

## Initialize variables..  Should not need to modify.
NUMBER=$1
MESSAGE=`echo "$2" | iconv -t "UTF-8"`
REMOTE_URL=http://api.bipkampany.hu/sendsms

## Send variables to url
RESULT=`curl --get --data-urlencode "number=$NUMBER" --data-urlencode "message=$MESSAGE" --data-urlencode "email=$EMAIL" --data-urlencode "password=$PASSWORD" --data-urlencode "senderid=$SENDERID" -s -S $REMOTE_URL`
# echo "$RESULT"

## grep and sed result to parse 1 for confirmed positive, 2 for negative, nothing for error.
PARSED_RESULT=`echo "$RESULT" | grep "result=" | sed -e "s/.*result=OK.*/1/" -e "s/.*result=ERR.*/2/"`

# echo "$PARSED_RESULT"

# Case statement.  1 for confirmed, 2 for negative, default case for error.
case "$PARSED_RESULT" in
        1       )
                #echo "positive"
                exit 0
                ;;

        2       )
                #echo "negative"
                exit 1
                ;;

        *       )
                #echo "unexpected text or error"
                exit $EXIT_ERRORCODE
                ;;
esac
