#!/bin/sh
#
# Use crs_stat for check Oracle RAC status
#
#  CONFIG SUDO
#
#User_Alias NAGIOS = nagios
#Cmnd_Alias CRSSTAT = /app/oracle/crs102/bin/crs_stat
#NAGIOS ALL=(ALL)  NOPASSWD:CRSSTAT

#
#########################
DEBUG=0

PROGNAME=$(basename $0)
PROGPATH=$(echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,')
#CRSSTAT_PATH="./crsstat"
#CRSSTAT_PATH="/usr/bin/sudo /app/crs/oracle/product/10.2.0/crs/bin/crsstat"
CRSSTAT_BIN=/app/oracle/crs102/bin/crs_stat
CRSSTAT_USER=oracle
EGREP_PATH=`which egrep`
CUT_PATH=`which cut`
TR_PATH=`which tr`

EXIT_ERROR="1"
EXIT_OK="0"
EXIT_USAGE="64"
LOCAL_VERSION="0.81"


#. $PROGPATH/utils.sh
. /usr/lib64/nagios/plugins/utils.sh

exitstatus=$STATE_WARNING #default

#########################
# A mettre dans une lib #
#########################
STATE_OK_STR="OK"
STATE_WARNING_STR="WARNING"
STATE_CRITICAL_STR="CRITICAL"
STATE_UNKNOWN_STR="UNKNOWN"

NAGIOS_STATUS=$STATE_OK
NAGIOS_STATUS_STR=$STATE_OK_STR
NAGIOS_STATUS_SHORT=""
NAGIOS_MESSAGE=""

# Colors
N_COLOR_GREEN="<img src=images/green.gif>"
N_COLOR_RED="<img src=images/red.gif>"
N_COLOR_YELLOW="<img src=images/yellow.gif>"
N_COLOR_CLEAR="<img src=images/clear.gif>"


function debug()
{
        if [ "$DEBUG" = "1" ]
        then
                echo -e "$@"
        fi
}



function NAGIOS_Add()
{
        NAGIOS_MESSAGE="$NAGIOS_MESSAGE$*"
}

function NAGIOS_Send()
{
        echo -e "$NAGIOS_STATUS_STR $NAGIOS_STATUS_SHORT\n$NAGIOS_MESSAGE"
}


function NAGIOS_SetStatus()
{
        if [ "$2" != "" ]
        then
		if [ "$NAGIOS_STATUS_SHORT" != "" ]
		then
			NAGIOS_STATUS_SHORT="$NAGIOS_STATUS_SHORT, "
		fi
                NAGIOS_STATUS_SHORT="$NAGIOS_STATUS_SHORT$2"
        fi


        if [ $1 -gt $NAGIOS_STATUS ]
        then
                NAGIOS_STATUS=$1


                if [ "$NAGIOS_STATUS" = "$STATE_WARNING" ]
                then
                        NAGIOS_STATUS_STR=$STATE_WARNING_STR
                else
                        if [ "$NAGIOS_STATUS" = "$STATE_CRITICAL"  ]
                        then
                                NAGIOS_STATUS_STR=$STATE_CRITICAL_STR
                        else
                                if  [  "$NAGIOS_STATUS" = "$STATE_OK"  ]
                                then
                                        NAGIOS_STATUS_STR=$STATE_WARNING_OK

                                fi
                        fi


                fi

        fi
}







help()
{
cat<<EOF
`basename $0` Version $LOCAL_VERSION
	
usage: `basename $0` -n [options]

Global Options:
 -n -- node

Options:
 -a	--	ASM
 -l	--	LISTENER	
 -g	--	GSD
 -o	--	ONS
 -v	--	VIP

Ex.

`basename $0` -n dbnode1 -a

EOF
exit $EXIT_USAGE
}



##########################################
# Lancement du check du service/instance #
##########################################
command_check()
{
TYPE_OF_STATUS="$1";shift
SERVICE="$1";shift
NODE="$1"
if    [ $TYPE_OF_STATUS = "s" ];then
    #SERVICE_STATUS=$( $CRSSTAT_PATH 2>&1 |awk '$0 ~ /'${SERVICE}'/ && $5 ~ /'${NODE}'/ { print $2 }' )

    # Debug
    debug "SERVICE: ${SERVICE}  Node: ${NODE}"

    SERVICE_STATUS=$( echo "$CRSSTAT_OUT" |awk '$0 ~ /'${SERVICE}'/ && $0 ~ /'ora.${NODE}'/ { print $2 }' )
    debug "S: $SERVICE_STATUS"

elif  [ $TYPE_OF_STATUS = "i" ];then       
    #INSTANCE_STATUS=$( $CRSSTAT_PATH 2>&1 |awk '$0 ~ /'${SERVICE}'/ && $5 ~ /'${NODE}'/ { print $3 }' )
    INSTANCE_STATUS=$( echo "$CRSSTAT_OUT"  |awk '$0 ~ /'${SERVICE}'/ && $0 ~ /'ora.${NODE}'/ { print $3 }' )

    debug "I: $INSTANCE_STATUS"

    NODE_CUR=$( echo "$CRSSTAT_OUT" |awk '$0 ~ /'${SERVICE}'/ && $0 ~ /'ora.${NODE}'/ { print $5 }' )


    if [ "$NODE_CUR" != "" ] &&  [ "$NODE_CUR" != "$NODE" ]
    then
	INSTANCE_STATUS="RELOCATED on $NODE_CUR"
    fi
else  
    echo "UNKNOW TYPE"    

fi    
}

###################
# Check du status #
###################
status_check()
{
	SERVICE="$1";shift
	SERVICE_STATUS="$1";shift        
	INSTANCE_STATUS="$1"

	if [ "$SERVICE_STATUS" = "ONLINE" -a "$INSTANCE_STATUS" = "ONLINE" ];then
		NAGIOS_Add "$N_COLOR_GREEN ${SERVICE} OK: 0 problem(s)\n"
		NAGIOS_SetStatus $STATE_OK "${SERVICE} ok"

#		exit $STATE_OK
	elif [ "$SERVICE_STATUS" = "ONLINE" -a "$INSTANCE_STATUS" = "OFFLINE" ];then
		NAGIOS_Add "$N_COLOR_RED ${SERVICE} CRITICAL: Instance FAILED\n"
		NAGIOS_SetStatus $STATE_CRITICAL "<b>${SERVICE} critical</b>"
		#exit $STATE_CRITICAL

	elif  [ "$SERVICE_STATUS" = "ONLINE" -a  "$INSTANCE_STATUS" = "RELOCATED" ];then
		NAGIOS_Add "$N_COLOR_YELLOW ${SERVICE} relocated on $3\n"
		NAGIOS_SetStatus $STATE_WARNING "<b>${SERVICE} warning</b>"

	elif [ "$SERVICE_STATUS" = "OFFLINE" ];then
		NAGIOS_Add "$N_COLOR_RED ${SERVICE} CRITICAL: Service FAILED\n"
		NAGIOS_SetStatus $STATE_CRITICAL "<b>${SERVICE} critical</b>"
		#exit $STATE_CRITICAL
	else
		NAGIOS_Add "$N_COLOR_YELLOW ${SERVICE} WARNING: UNKNOW problem(s)\n"
		NAGIOS_SetStatus $STATE_WARNING "<b>${SERVICE} warning</b>"
		#exit $STATE_WARNING
	fi
}

#######################
# Lancement des tests #
#######################
run_check()
{
	NODE="$1";shift
	OPTIONS="$1"

	if   [ "$OPTIONS" = "a" ];then
	    SERVICE="asm"    
	    command_check s $SERVICE $NODE
	    command_check i $SERVICE $NODE
	    status_check $SERVICE $SERVICE_STATUS $INSTANCE_STATUS
	elif [ "$OPTIONS" = "l" ];then
	    SERVICE="lsnr"    
	    command_check s $SERVICE $NODE
	    command_check i $SERVICE $NODE
	    status_check $SERVICE $SERVICE_STATUS $INSTANCE_STATUS

	elif [ "$OPTIONS" = "g" ];then
	    SERVICE="gsd"    
	    command_check s $SERVICE $NODE
	    command_check i $SERVICE $NODE
	    status_check $SERVICE $SERVICE_STATUS $INSTANCE_STATUS
	elif [ "$OPTIONS" = "o" ];then
    		SERVICE="ons"    
    		command_check s $SERVICE $NODE
    		command_check i $SERVICE $NODE
		status_check $SERVICE $SERVICE_STATUS $INSTANCE_STATUS
	elif [ "$OPTIONS" = "v" ];then
    		SERVICE="vip"    
	    command_check s $SERVICE $NODE
	    command_check i $SERVICE $NODE
	    status_check $SERVICE $SERVICE_STATUS $INSTANCE_STATUS
	fi
}




	#############
	# Demarrage #
	#############

	if [ $# -eq 0 ];then
       	 help
	fi

	###########
	# Options #
	###########
	while getopts "halgovn:" OPT ;do
		case "$OPT" in

			"n")	node_name=$OPTARG
				;;


			[algov])
			
				node_action="$node_action $OPT"
				;;

			*)	help
				exit 2
				;;

		esac
	done

	######################
	# Checks des options #
	######################
	if [ "$node_name" = "" ]
	then
		echo "node_name est obligatoire!"
		help
		exit 3
	fi

	#############
	# Lancement #
	#############

	CRSSTAT_OUT=`sudo $CRSSTAT_BIN -u 2>&1`

	if [ $? -ne 0 ]
	then
		NAGIOS_Add "$N_COLOR_RED Erreur pendant le lancement de crs_stat!\nOutput:\n$CRSSTAT_OUT"
                NAGIOS_SetStatus $STATE_CRITICAL "<b>Erreur pendant le lancement de crs_stat"
		NAGIOS_Send
		exit $NAGIOS_STATUS
	fi

	CRSSTAT_OUT=`echo "$CRSSTAT_OUT" | awk \
 'BEGIN { FS="="; state = 0; }
  $1~/NAME/ && $2~/'$RSC_KEY'/ {appname = $2; state=1};
  state == 0 {next;}
  $1~/TARGET/ && state == 1 {apptarget = $2; state=2;}
  $1~/STATE/ && state == 2 {appstate = $2; state=3;}
  state == 3 {printf "%-45s %-10s %-18s\n", appname, apptarget, appstate; state=0;}'`

        if [ $? -ne 0 ]
        then
                NAGIOS_Add "$N_COLOR_RED Erreur pendant le lancement de awk!\nOutput:\n$CRSSTAT_OUT"
                NAGIOS_SetStatus $STATE_CRITICAL "<b>Erreur pendant le lancement de awk"
                NAGIOS_Send
                exit $NAGIOS_STATUS
        fi


	# Debug
 #	echo "$CRSSTAT_OUT" > /tmp/check_oracle_cluster.debug

	for action in $node_action
	do
		#echo "running '$action' on $node_name"
		run_check $node_name $action
	done

	NAGIOS_Send
	exit $NAGIOS_STATUS


                    #"-l")
                    #            init $OPTARG $3

