#!/bin/sh
#
# EDT
#
# Check status of Oracle Application Server Components
# You must set the Oracle Home, see above

# Only set the Oracle HOME
ORACLE_HOME=/app/oas10g_r2


# libs
.  `dirname $0`/nagios_libs.sh
#NAGIOS_DEBUG=0


# Constantes
CMD="$ORACLE_HOME/opmn/bin/opmnctl status"

# Defaults values
CHECK_ALIVE=""
CHECK_ALIVE_STR="Alive"

function help()
{
	echo "check_OAS.sh"
	echo "check:"
	echo "  -c Components to check"
	echo ""
	echo "Example:"
	echo "  check_OAS.sh -c HTTP_Server -c dcm-daemon -c home -c OC4J_BI_Forms"
	exit 2
}


function check_OAS_component()
{
	proc=$1
	NAGIOS_Debug "Check_OAS '$proc'"	
	unknown=1

	R=`echo "$OAS_RESULT" | sed -n "s/^$proc|\(.*\)/\1/p"`

	for r in $R
        do
	#	echo "r: $r"
       		if [ "$r" = "" ]
               	then
			NAGIOS_Add "$N_COLOR_CLEAR status de $proc non present\n"
			NAGIOS_SetStatus $STATE_UNKNOWN "status de $proc non present"

		elif [ "$r" != "$CHECK_ALIVE_STR" ]
                then
			NAGIOS_Add "$N_COLOR_RED $proc status != $CHECK_ALIVE_STR\n"
			NAGIOS_SetStatus $STATE_CRITICAL "$proc != $CHECK_ALIVE_STR"
			
		else
			NAGIOS_Add "$N_COLOR_GREEN $proc $CHECK_ALIVE_STR\n"
			NAGIOS_SetStatus $STATE_OK "$proc ok"
		fi

		unknown=0

	done

	if [ "$unknown" = "1" ]
	then
		NAGIOS_Add "$N_COLOR_CLEAR $proc non present\n"
		NAGIOS_SetStatus $STATE_UNKNOWN "$proc non present"
	fi
}


	#############
	# Démarrage #
	#############
        while getopts "c:" OPT ;do
                case "$OPT" in

			"c")	
				OAS_Component="$OAS_Component "$OPTARG
				;;


                        *)      help
                                exit 2
                                ;;

                esac
        done


	if [ "$OAS_Component" = "" ]
	then
                NAGIOS_Add "$N_COLOR_RED rien a checker\n"
                NAGIOS_SetStatus $STATE_CRITICAL "rien a checked"
                NAGIOS_Send
                exit $NAGIOS_STATUS
	fi

	# Lancement de la commande et on recupère la colonne process-type
	OAS_RESULT=`sudo -u oracle $CMD 2>&1`

	if [ $? -ne 0 ]
	then
		NAGIOS_Add "$N_COLOR_RED Erreur pendant le lancement de '$sudo -u oracle $CMD'\nOutput:\n$OAS_RESULT"
                NAGIOS_SetStatus $STATE_CRITICAL "Erreur pendant le lancement"
		NAGIOS_Send
		exit $NAGIOS_STATUS
	fi

        OAS_RESULT=`echo -e "$OAS_RESULT" | sed -n 's/^.*| \([^ |]*\).*|.*| \([^ |]*\).*$/\1|\2/p'`


	# On check tout les composants
	for Component in $OAS_Component
	do
		check_OAS_component $Component
	done

	NAGIOS_Send
	exit $NAGIOS_STATUS

