#! /bin/bash

#This Plug-in monitors Cassandra ConcurrentMarkSweep Garbage Collection happening in particuler duration ;
# This take four  parameters as input
# 1) -d Duration within which the count needs to be monitores (in seconds )
# 2)  -f Log file location of cassandra status.log
# 3) Warning Value
# 4)Critical Values.

# Author - Juned Memon

# Please make sure your log entry for CMS GC is like
# " INFO [ScheduledTasks:1] 2011-10-21 00:09:22,753 GCInspector.java (line 128) GC for ConcurrentMarkSweep: 15828 ms, 11324439288 reclaimed leaving 1007293704 used; max is 17232297984"

#Otherwise adjust the coloumns to be cut in line where varibale TIME is poppulated.

#########THIS part is for Nagios ################################
PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1749 $' | sed -e 's/[^0-9.]//g'`
#. $PROGPATH/utils.sh
.  /usr/local/nagios/libexec/utils.sh


######################################################################

#Function to print Usage
function usage
{
usage="Usage: $0  -w <WARN> -c <CRIT> -f <LOG FILE>"
echo $usage
usage="<WARN> WARNING threshold.(in seconds) Default is 90 seconds."
echo $usage
usage="<CRIT> is Rate/second for Critical  state  Default is 120 seconds."
echo $usage
usage="<LOG FILE> location of cassandra log file. Default is /BigData/lib/cassandra/log/system.log"
echo $usage
exit $STATE_UNKNOWN
}


WARN=90
CRIT=120
LOG_FILE="/BigData/lib/cassandra/log/system.log"

#####################################################################
# get parameter values in Variables

while test -n "$1"; do
    case "$1" in
        -c )
            CRIT=$2
            shift
            ;;
        -w )
            WARN=$2
            shift
            ;;
         -f )
            LOG_FILE=$2
            shift
            ;;

        -h)
            usage
            ;;
        *)
            echo "Unknown argument: $1"
            usage
            ;;
    esac
    shift
done

#####################################################################

TIME=$( grep ConcurrentMarkSweep $LOG_FILE | tail -n 1  | cut -d " " -f12)

TIME_SEC=`expr $TIME / 1000`


echo "Last ConcurrentMarkSweep Garbage Collection Occoured in Cassandra have taken $TIME_SEC seconds."



####################################################################################
#if CRIT > TIME_SEC >WARN then WARNing
if [ $TIME_SEC -ge $WARN ]; then
if [ $TIME_SEC -lt $CRIT ]; then
exitstatus=$STATE_WARNING
exit $exitstatus
fi
fi
# TIME_SEC>CRIT then CRITical
if [ $TIME_SEC -ge $CRIT ]; then
exitstatus=$STATE_CRITICAL
exit $exitstatus
fi

# 0<=TIME_SEC <WARN
if [ $TIME_SEC -le $WARN ]; then
exitstatus=$STATE_OK
exit $exitstatus
fi

root@ip-10-92-47-100:~# vi ./cass-time-taken-for-GC-CMS
root@ip-10-92-47-100:~# cat  ./cass-time-taken-for-GC-CMS
#! /bin/bash

#This Plug-in monitors how much time Cassandra ConcurrentMarkSweep type of Garbage Collection is taking ;
# This take four  parameters as input
# 1)  -f Log file location of cassandra status.log
# 2) Warning Value
# 3)Critical Values.

# Author - Juned Memon

# Please make sure your log entry for CMS GC is like
# " INFO [ScheduledTasks:1] 2011-10-21 00:09:22,753 GCInspector.java (line 128) GC for ConcurrentMarkSweep: 15828 ms, 11324439288 reclaimed leaving 1007293704 used; max is 17232297984"

#Otherwise adjust the coloumns to be cut in line where varibale TIME is poppulated.

#########THIS part is for Nagios ################################
PROGNAME=`/usr/bin/basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1749 $' | sed -e 's/[^0-9.]//g'`
#. $PROGPATH/utils.sh
.  /usr/local/nagios/libexec/utils.sh


######################################################################

#Function to print Usage
function usage
{
usage="Usage: $0  -w <WARN> -c <CRIT> -f <LOG FILE>"
echo $usage
usage="<WARN> WARNING threshold.(in seconds) Default is 90 seconds."
echo $usage
usage="<CRIT> is Rate/second for Critical  state  Default is 120 seconds."
echo $usage
usage="<LOG FILE> location of cassandra log file. Default is /BigData/lib/cassandra/log/system.log"
echo $usage
exit $STATE_UNKNOWN
}


WARN=90
CRIT=120
LOG_FILE="/BigData/lib/cassandra/log/system.log"

#####################################################################
# get parameter values in Variables

while test -n "$1"; do
    case "$1" in
        -c )
            CRIT=$2
            shift
            ;;
        -w )
            WARN=$2
            shift
            ;;
         -f )
            LOG_FILE=$2
            shift
            ;;

        -h)
            usage
            ;;
        *)
            echo "Unknown argument: $1"
            usage
            ;;
    esac
    shift
done

#####################################################################

TIME=$( grep ConcurrentMarkSweep $LOG_FILE | tail -n 1  | cut -d " " -f12)

TIME_SEC=`expr $TIME / 1000`


echo "Last ConcurrentMarkSweep Garbage Collection Occoured in Cassandra have taken $TIME_SEC seconds."



####################################################################################
#if CRIT > TIME_SEC >WARN then WARNing
if [ $TIME_SEC -ge $WARN ]; then
if [ $TIME_SEC -lt $CRIT ]; then
exitstatus=$STATE_WARNING
exit $exitstatus
fi
fi
# TIME_SEC>CRIT then CRITical
if [ $TIME_SEC -ge $CRIT ]; then
exitstatus=$STATE_CRITICAL
exit $exitstatus
fi

# 0<=TIME_SEC <WARN
if [ $TIME_SEC -le $WARN ]; then
exitstatus=$STATE_OK
exit $exitstatus
fi


