#!/bin/bash
#
# Christophe DENEUX - Linagora
#
#  This Nagios plugin was created to check Petals BC SOAP status
#

PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`

. $PROGPATH/utils.sh


print_usage() {
  echo "Usage:"
  echo "  $PROGNAME -h <host> -n <jmx_port> -u <jmx_user> -p <jmx_passwd> -n <petals-bcsoap-id> --http-thread-pool-current-allocated-threads -w <warn> -c <critical>"
  echo "  $PROGNAME -h <host> -n <jmx_port> -u <jmx_user> -p <jmx_passwd> -n <petals-bcsoap-id> --http-thread-pool-current-enqueued-requests"
  echo "  $PROGNAME --help"
}

print_help() {
  echo ""
  print_usage
  echo ""
  echo "Check Petals BC SOAP status"
  echo ""
  echo "-h <host>"
  echo "   The Petals container hostanme where the Petals BC SOAP is running. Default value: localhost"
  echo "-n <jmx_port>"
  echo "   The JMX port of the Petals ESB container: Default value: 7700"
  echo "-u <jmx_user>"
  echo "   The JMX user with which the connecion must be done. Default value: petals"
  echo "-p <jmx_passwd>"
  echo "   The password of the JMX user with which the connection must be done. Default value: petals"
  echo "-i <petals-bcsoap-id>"
  echo "   The unique identifier of the Petals BC SOAP on the Petals ESB container. Default value: petals-bc-soap"
  echo "-w <warn>"
  echo "   The warning threshold"
  echo "-c <critical>"
  echo "   The critical threshold"
  echo "--http-thread-pool-current-allocated-threads"
  echo "   Check the current number of allocated threads of the HTTP thread pool"
  echo "--http-thread-pool-current-enqueued-requests"
  echo "   Check the current number of enqueued requests of the HTTP thread pool"
  echo "--help"
  echo "   Print this help screen"
  echo ""
}

HOSTNAME="localhost"
JMXPORT="7700"
JMXUSER="petals"
JMXPWD="petals"
BCSOAPID="petals-bc-soap"
while (( "$#" )); do
   case $1 in
      -h) if [ ! -z "$2" ]; then
             HOSTNAME=$2
             shift 2
          else
             echo "Hostname is missing"
             exit $STATE_UNKNOWN
          fi
          ;;
      -n) if [ ! -z "$2" ]; then
             JMXPORT=$2
             shift 2
          else
             echo "JMX Port is missing"
             exit $STATE_UNKNOWN
          fi
          ;;
      -u) if [ ! -z "$2" ]; then
             JMXUSER=$2
             shift 2
          else
             echo "JMX User is missing"
             exit $STATE_UNKNOWN
          fi
          ;;
      -p) if [ ! -z "$2" ]; then
             JMXPWD=$2
             shift 2
          else
             echo "JMX Password is missing"
             exit $STATE_UNKNOWN
          fi
          ;;
      -i) if [ ! -z "$2" ]; then
             BCSOAPID=$2
             shift 2
          else
             echo "Petals BC SOAP is missing"
             exit $STATE_UNKNOWN
          fi
          ;;
      -w) if [ ! -z "$2" ]; then
             warn_threshold=$2
             shift 2
          else
             echo "Warning threshold is missing"
             exit $STATE_UNKNOWN
          fi
          ;;
      -c) if [ ! -z "$2" ]; then
             critical_threshold=$2
             shift 2
          else
             echo "Critical threshold is missing"
             exit $STATE_UNKNOWN
          fi
          ;;
      --help)
	  print_help
          exit $STATE_OK
          ;;
      --http-thread-pool-current-allocated-threads)
          cmd=$1
          shift
          ;;
      *) echo "Unknown argument: $1"
         print_usage
         exit $STATE_UNKNOWN
   esac
done

if [ -z "$warn_threshold" ]; then
   echo "Warning threshold is missing"
   exit $STATE_UNKNOWN
fi

if [ -z "$critical_threshold" ]; then
   echo "Critical threshold is missing"
   exit $STATE_UNKNOWN
fi

# Information options
case "$cmd" in
--http-thread-pool-current-allocated-threads)
    httpthreadpoolstats=`petals-cli -h $HOSTNAME -n $JMXPORT -u $JMXUSER -p $JMXPWD -c -- monitoring -o petals-bc-soap -f http-thread-pool -- -n $BCSOAPID 2>&1`
    res=`echo $?`
    if [ $res -eq 0 ]; then
        currentallocatedthreads=`echo $httpthreadpoolstats | tr ' ' '\n' | grep HttpServerThreadPoolAllocatedThreadsCurrent | cut -d ":" -f 2`
        if [ $currentallocatedthreads -ge $warn_threshold ]; then
           echo "WARN - Current allocated threads: $currentallocatedthreads"
           exit $STATE_WARNING
        elif [ $currentallocatedthreads -ge $critical_threshold ]; then
           echo "CRITICAL - Current allocated threads: $currentallocatedthreads"
           exit $STATE_WARNING
        else
           echo "OK - Current allocated threads: $currentallocatedthreads"
           exit $STATE_OK
        fi
    else
        echo $httpthreadpoolstats
	exit $STATE_UNKNOWN
    fi
    ;;
*)
    print_usage
    exit $STATE_UNKNOWN
esac
