a=$@

help=`echo $a | awk '{print $1}'`
HOST=`echo $a | awk '{print $2}'`
PORT=`echo $a | awk '{print $4}'`
bucket=`echo $a | awk '{print $6}'`
COMMAND=`echo $a | awk '{print $8}'`
NODE=`echo $a | awk '{print $10}'`
WARN=`echo $a | awk '{print $12}'`
CRITICAL=`echo $a | awk '{print $14}'`

        WARN=`echo $WARN | cut -d "%" -f1`
        CRITICAL=`echo $CRITICAL | cut -d "%" -f1`

# Return codes:
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3


function printHelp {
  echo -e \\n"Help for check_couch -H X.X.X.X -p xxxx -b abcdef -c abc_def -n node1 -W WARN -C CRITICAL"\\n
  echo "-H - Couch server Address (Hostname or IP )"
  echo "-p - Couch Port Number"
  echo "-b - Vbucket Name that you want to monitor"
  echo "-c - Command that want to execute"
  echo -e  \\n"Here are the commands that you can use for flag -c"\\n
  echo "--------> 1. mem_used (Memory usage all nodes or any particular node)"
  echo "--------> 2. ep_mem_low_wat (Low water mark for auto-evitions)"
  echo "--------> 3. ep_mem_high_wat (High water mark for auto-evitions)"
  echo "--------> 4. ep_mem_high_wat"
  echo "--------> 5. curr_items (Number of Unique items in this bucket - ONly active item)"
  echo "--------> 6. couch_docs_data_size (The size of active data in this bucket)"
  echo "--------> 7. couch_docs_actual_disk_size (The size of all data files for this bucket, including the data itself, meta data and temporary files"
  echo "--------> 8. couch_total_disk_size (The total size on disk of all data and view files for this bucket)"
  echo "--------> 9. couch_views_data_size (the size of active data on for all the indexes in this bucket)"
  echo "-------> 10. couch_views_actual_disk_size (The size of all active items in all the indexes for this bucket on disk)"
  echo "-------> 11. couch_views_fragmentation (How much fragmented data there is to be compacted compared to real data for the view index files in this bucket)"
  echo "-n - You can specify node (node1 or node2 or node3 or ALL )"
  echo "-w - Warn Level"
  echo "-c - Critical Level"
  echo -e "help  - Displays this help message"\\n
  exit 1
}

function precehck ()
{
if [[ ! -z "${HOST}" && ! -z "${PORT}" && ! -z "${bucket}" && ! -z "${COMMAND}" &&  ! -z "${NODE}" && ! -z "${WARN}" && ! -z "${CRITICAL}" ]]; then
        if [ "${WARN}" -lt "${CRITICAL}" ]; then
        echo test >> /dev/null
#       WARN=`echo $WARN | cut -d "%" -f1`
#       CRITICAL=`echo $CRITICAL | cut -d "%" -f1`
        else
        echo "Warn Level should be less than Critical Level"
        exitstatus=$STATE_CRITICAL
        exit $exitstatus
        exit
        fi
else
echo "Wrong combination... Please use -help option to get more details"
exit
fi
}

function alert ()
{
        if [ "${output}" -lt "${WARN}" ]; then
        echo "${pre_message} OK - ${output} ${message}"
        exitstatus=$STATE_OK
        exit $exitstatus
                elif [ ${output} -gt ${CRITICAL} ]; then
                echo "${pre_message} CRITICAL - ${output}  ${message}"
                exitstatus=$STATE_CRITICAL
                exit $exitstatus
                        elif [[ ${output} -gt ${WARN} && ${output} -lt ${CRITICAL} ]]; then
                        echo "${pre_message} WARN - ${output} ${message}"
                        exitstatus=$STATE_WARNING
                        exit $exitstatus
        else
        echo "${pre_message} UNKNOWN - ${output} ${message}"
        exitstatus=$STATE_UNKNOWN
        exit $exitstatus
fi
}

function splitter ()
{
NODE1=`echo ${stats} | cut -d "]" -f2 | cut -d "[" -f2 | cut -d "," -f1`
NODE2=`echo ${stats} | cut -d "]" -f3 | cut -d "[" -f2 | cut -d "," -f1`
NODE3=`echo ${stats} | cut -d "]" -f4 | cut -d "[" -f2 | cut -d "," -f1`
TOT_BYTES=$(expr ${NODE1} + ${NODE2} + ${NODE3})
}


function command ()
{
case ${COMMAND} in
                        mem_used)
                                TOT=`curl -s http://${HOST}:${PORT}/pools/default/buckets/${bucket} | /usr/bin/jq ".quota.ram"`
                                TOTAL=`echo ${TOT}/1024/1024 | bc`
                                pre_message="Memory Usage"
                                stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                if [ ${NODE} == ALL ]; then
                                #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                splitter

                                message="% | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                output1=`echo ${TOT_BYTES}/1024/1024 | bc`
                                output=$((100*${output1}/${TOTAL}))
                                alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output1=`echo ${NODE1}/1024/1024 | bc`
                                        output=$((100*${output1}/${TOTAL}))
                                        message="% | Usage-Node1="${NODE1}";;; Allocated-Node1="${TOT}";;; Usage-AllNodes="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output1=`echo ${NODE2}/1024/1024 | bc`
                                        output=$((100*${output1}/${TOTAL}))
                                        message="% | Node2="${NODE2}";;; Total="${TOT}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="% | Node3="${NODE3}";;; Total="${TOT}";;;"
                                        output1=`echo $NODE3/1024/1024 | bc`
                                        output=$((100*${output1}/${TOTAL}))
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;

             ep_cache_miss_rate)
                                        pre_message="cache miss ratio"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message=" | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output="${TOT_BYTES}"
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output="${NODE1}"
                                        message=" | Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output="${NODE2}"
                                        message=" | Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message=" | Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output="${NODE3}"
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;

              ep_mem_low_wat)
                                        pre_message="Low water mark"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message="MB | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo ${TOT_BYTES}/1024/1024 | bc`
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output=`echo ${NODE1}/1024/1024 | bc`
                                        message="MB | Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output=`echo ${NODE2}/1024/1024 | bc`
                                        message="MB | Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="MB | Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo $NODE3/1024/1024 | bc`
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;

                      ep_mem_high_wat)
                                        pre_message="High water mark"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message="MB | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo ${TOT_BYTES}/1024/1024 | bc`
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output=`echo ${NODE1}/1024/1024 | bc`
                                        message="MB | Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output=`echo ${NODE2}/1024/1024 | bc`
                                        message="MB | Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="MB | Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo $NODE3/1024/1024 | bc`
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;


                      curr_items)
                                        pre_message="Items"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message="MB | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo ${TOT_BYTES}/1024/1024 | bc`
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output=`echo ${NODE1}/1024/1024 | bc`
                                        message="MB | Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output=`echo ${NODE2}/1024/1024 | bc`
                                        message="MB | Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="MB | Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo $NODE3/1024/1024 | bc`
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;

                   couch_docs_data_size)
                                        pre_message="docs data size"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message="MB | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo ${TOT_BYTES}/1024/1024 | bc`
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output=`echo ${NODE1}/1024/1024 | bc`
                                        message="MB | Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output=`echo ${NODE2}/1024/1024 | bc`
                                        message="MB | Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="MB | Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo $NODE3/1024/1024 | bc`
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;

                   couch_docs_actual_disk_size)
                                        pre_message="docs total disk size"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message="MB | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo ${TOT_BYTES}/1024/1024 | bc`
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output=`echo ${NODE1}/1024/1024 | bc`
                                        message="MB | Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output=`echo ${NODE2}/1024/1024 | bc`
                                        message="MB | Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="MB | Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo $NODE3/1024/1024 | bc`
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;

                   couch_total_disk_size)
                                        pre_message="docs total disk size"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message="MB | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo ${TOT_BYTES}/1024/1024 | bc`
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output=`echo ${NODE1}/1024/1024 | bc`
                                        message="MB | Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output=`echo ${NODE2}/1024/1024 | bc`
                                        message="MB | Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="MB | Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo $NODE3/1024/1024 | bc`
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;

                   couch_views_data_size)
                                        pre_message="Views data size"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message="MB | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo ${TOT_BYTES}/1024/1024 | bc`
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output=`echo ${NODE1}/1024/1024 | bc`
                                        message="MB | Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output=`echo ${NODE2}/1024/1024 | bc`
                                        message="MB | Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="MB | Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo $NODE3/1024/1024 | bc`
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;

             couch_views_actual_disk_size)
                                        pre_message="Views total disk size"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message="MB | Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo ${TOT_BYTES}/1024/1024 | bc`
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output=`echo ${NODE1}/1024/1024 | bc`
                                        message="MB | Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output=`echo ${NODE2}/1024/1024 | bc`
                                        message="MB | Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="MB | Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=`echo $NODE3/1024/1024 | bc`
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;


                couch_views_fragmentation)
                                        pre_message="Views fragmentation"
                                        stats=`curl -m 120 -s http://${HOST}:${PORT}/pools/default/buckets/${bucket}/stats/${COMMAND}`
                                        if [ ${NODE} == ALL ]; then
                                        #####Calling function splitter to get the Node1, Node2, Node3 and Total Values
                                        splitter

                                        message="| Node1="${NODE1}";;; Node2="${NODE2}";;; Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=${TOT_BYTES}
                                        alert

                                        elif [ ${NODE} = node1 ]; then
                                        #####Calling function splitter to get the Node1 and Total Values
                                        splitter

                                        output=${NODE1}
                                        message="| Node1="${NODE1}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node2 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        output=${NODE2}
                                        message="| Node2="${NODE2}";;; Total="${TOT_BYTES}";;;"
                                        alert

                                        elif [ ${NODE} = node3 ]; then
                                        #####Calling function splitter to get the Node2 and Total Values
                                        splitter

                                        message="| Node3="${NODE3}";;; Total="${TOT_BYTES}";;;"
                                        output=$NODE3
                                        alert

                                        else
                                        echo "Improper Node specified"
                                        exitstatus=$STATE_UNKNOWN
                                        exit $exitstatus

                                        exit
                                        fi
                                        ;;

esac


}

if [ ${help} = help ]; then
printHelp
else
precehck
command
fi
