#!/bin/sh
#
# Script to send an SMS email notifcation to Esendex's HTTP gateways
# Based on: SMS HTTP Gateway - http://www.nagiosexchange.org/Notifications.35.0.html?&amp;tx_netnagext_pi1[p_view]=183
# Esendex version by Bhozar

# Example commands:
#define command{
#command_name service_notify_with_sms
#command_line /usr/lib/nagios/plugins/notify_sms -a EsendexAccount -u EsendexUsername -p EsendexPassword -m '$NOTIFICATIONTYPE$: $HOSTNAME$ is $SERVICESTATE$ ($SERVICEOUTPUT$)' -n $CONTACTPAGER$
#}
#define command{
#command_name host_notify_with_sms
#command_line /usr/lib/nagios/plugins/notify_sms -a EsendexAccount -u EsendexUsername -p EsendexPassword -m '$NOTIFICATIONTYPE$: $HOSTNAME$ is $HOSTSTATE$ ($HOSTOUTPUT$)' -n $CONTACTPAGER$
#}

# Username, password and account reference associated with Esendex account
# Modify these values to match your account credentials if you don't want to
# specify them as command line arguments.
username=
password=
account=

number=
message=""

#Server URL for Esendex. Seperate multiple URLs with space
serverURLs="https://www.esendex.com/secure/messenger/formpost/SendSMS.aspx"

#Set default state for debug
debug=0

# Show usage if necessary
if [ $# -eq 0 ]; then
    echo "";
    echo "Usage: $0 -n [number] -m [message] -u [username] -p [password] -a [account] -t [test] -d [debug]";
    echo "";
    echo "[number]   = SMS number to send message to";
    echo "[message]  = Text of message you want to send";
    echo "[username] = Username assocated with Esendex account";
    echo "[password] = Password assocated with Esendex account";
    echo "[account] = Account reference assocated with Esendex account";
    echo "[test] = Optional: Specify -t  and no SMS message will be sent";
    echo "[debug] = Optional: Specify -d  and more detailed responses will be returned";
    echo "";
    echo "               Username, Password and Account options are REQUIRED unless defined";
    echo "               in this script. Defining them and run will override the ones in this script.";
    echo "";
    exit 1;
fi

# Get command line arguments
while [ "$1" != "" ] ; do
    case $1
    in
        -n)
            # Get the SMS number that we should send message to
            number=$2;
            shift 2;
            ;;
        -m)
            # Get the message we should send
            message=$2;
            shift 2;
            ;;
        -u)
            # Get the username
            username=$2;
            shift 2;
            ;;
        -p)
            # Get the password
            password=$2;
            shift 2;
            ;;
        -a)
            # Get the account reference
            account=$2;
            shift 2;
            ;;
        -t)
            # Set test mode
            test=1;
            shift 1;
            ;;
        -d)
            # Set debug mode
            debug=1;
            shift 1;
            ;;
        *)
            echo "Unknown option: $1"
            exit 1;
            ;;
    esac
done

# We haven't sent the message yet
message_sent_ok=0;

# Return plain text results from Esendex
plaintext=1;

# Try to send an HTTP POST message (try all servers until successful)
for server in $serverURLs; do

if [ $debug = 1 ]; then
        echo "===== Debug Info ======";
        echo " Server URL: $server [REQUIRED]";
        echo "-n Recipient Number: $number [REQUIRED]";
        echo "-m SMS Message: $message [REQUIRED]";
        echo "-u Username: $username [REQUIRED]";
        echo "-p Password: $password [REQUIRED]";
        echo "-a Account Reference: $account [REQUIRED]";
        echo "-t Test Mode: $test";
        echo "-d Debug: $debug";
        echo "=======================";
        echo "";
fi

#Check required options are not null
if [ "$number" = '' ] || [ "$username" = '' ] || [ "$password" = '' ] || [ "$account" = '' ] || [ "$message" = '' ]; then
        echo "======= ERROR ========";
        echo "Required option missing";
        echo "Run with -d for debug info";
        echo "======================";
        echo "";
        exit 1;
fi

    RESPONSE=`curl -s -d EsendexUsername=$username -d EsendexPassword=$password -d EsendexAccount=$account -d EsendexRecipient=$number -d EsendexBody=$message $serverURLs`

    # Curl was able to post okay...
    if [ "$?" -eq "0" ]; then

        # Test the response from the Esendex server
        case $RESPONSE
        in
            *Result=OK*)
                # Message was queued ok
                message_sent_ok=1;
                echo "Message posted OK to HTTP gateway."
                        if [ $debug = 1 ]; then
                                echo "";
                                echo "====== Response =======";
                                echo "$RESPONSE"
                                echo "=======================";
                        fi
                exit 0;
                ;;
            *Test)
                # Test message received ok
                echo "TEST message posted OK to HTTP gateway"
                        if [ $debug = 1 ]; then
                                echo "";
                                echo "====== Response =======";
                                echo "$RESPONSE"
                                echo "=======================";
                        fi
                exit 0;
                ;;

            *AuthenticationFailedException)
                # Some kind of Authentication error occurred
                echo "Authentication error received from HTTP gateway. Check -u and -p options"
                        if [ $debug = 1 ]; then
                                echo "";
                                echo "====== Response =======";
                                echo "$RESPONSE"
                                echo "=======================";
                        fi
                exit 1;
                ;;
            *Account+Management+Item+Not+Found)
                # Some kind of Account error occurred
                echo "Account reference error received from HTTP gateway. Check -a option"
                        if [ $debug = 1 ]; then
                                echo "";
                                echo "====== Response =======";
                                echo "$RESPONSE"
                                echo "=======================";
                        fi
                exit 1;
                ;;

            *Result=Error*)
                # Some kind of fatal error occurred
                echo "Fatal error received from HTTP gateway"
                echo "";
                echo "====== Response =======";
                echo "$RESPONSE"
                echo "=======================";
                exit 1;
                ;;
            *)
                # No response or invalid response
                ;;
        esac

    fi

done
