bl_stop.ksh


#################################################################
#                                                               #
#                                                               #
#################################################################
#                                                               #
#                                                               #
# USAGE : To be executed as oracle user                         #
#         (pbrun to oracle or pboracle)                         #
#                                                               #
# Manual execution                                              #
#       sh bl_stop.ksh                                          #
#                                                               #
# Via job scheduler tools and by-passing the prompt             #
#       sh bl_stop.ksh y                                        #
#                                                               #
#---------------------------------------------------------------#
#                                                               #
# //Generic information about the batch of scripts developed//  #
#       bl_status.ksh                                           #
#       bl_stop.ksh                                             #
#       bl_stop_crs.ksh                                         #
#       bl_start_crs.ksh                                        #
#       bl_start.ksh                                            #
#                                                               #
# Versions/Combinations Supported                               #
#       Clustware 11g                                           #
#       Clustware 10g                                           #
#       NON-RAC with ASM 11g                                    #
#       NON-RAC with ASM 10g                                    #
#       NON-RAC & NON-ASM                                       #
#                                                               #
# Components in scope                                           #
#       Clusterware                                             #
#       ASM Instance                                            #
#       Database Instance                                       #
#       Cluster Listener                                        #
#       Local Listener                                          #
#       Manager Recovery Process                                #
#       GoldenGate Replication                                  #
#       Port Utility                                            #
#       Enterprise Manager Agent                                #
#       OSWatcher Utility                                       #
#       Foglight Monitoring Agent                               #
#       Filesystem                                              #
#       Rawdisk                                                 #
#                                                               #
#################################################################


export OS=`uname -a | awk '{ print $1 }'`
eval timevalue=`date '+%m%d%Y_%H%M%S'`
eval filename=$(echo $(hostname) | cut -d. -f1)_$timevalue

# Check for Redhat
if [[ -f /etc/redhat-release ]]; then
   export RH_VER=`cat /etc/redhat-release | grep "Linux Server release" | sed -n 's/.*\(Linux Server release.*\).*/\1/p' | awk '{ print $4 }'`
else
   export RH_VER=0
fi

# CHECK IF AAR DATABASE SERVER
if [ ! -f $OUTPUT_DIRECTORY/execute.flag ]
then
        sleep 2
elif [ "$(cat $OUTPUT_DIRECTORY/execute.flag)" = "no" ]
then
        echo "This is an AAR Database Server, exiting cleanly."         | tee -a ${LOG}
        #insert_repdb
        exit 0
else
        sleep 2
fi


# CHECK IF MYSQL DATABASE SERVER
if [ "$(ps -ef | grep -i mysql | grep -v grep | wc -l)" -ge 1 ]
then
        echo "This is a MySQL Database Server"  | tee -a ${LOG}
        #insert_repdb
        exit 0
fi


# Check if user really wants to STOP the services
if [ $# -eq 0 ]
    then
   read -r -p "Are you sure you want to STOP the Oracle services ? [y/n] " response
   if [ $response = y ]
      then
      echo "";   echo "Please wait while the services are brought down... ";      echo ""
      export oracred=oracle:dba
      export oraowner=oracle
      export oragroup=dba
   else
      exit; >/dev/null
   fi
elif [ $1 == y ]
    then
                            echo "";   echo "Please wait while the services are brought down... ";      echo ""
                            #Handle second Argument
                            if [[ ! -z $2 ]]; then
                               export oracred=$2
                               export oraowner=`echo $oracred | cut -d":" -f1 -s`
                               export oragroup=`echo $oracred | cut -d":" -f2 -s`
                            echo "Oracle Ownership Captured ARGUMENTS ...
                             oracred=$oracred
                             oraowner=$oraowner
                             oragroup=$oragroup
                            "
                            else
                               export oracred=oracle:dba
                               export oraowner=oracle
                               export oragroup=dba
                            fi
else
                exit; >/dev/null
fi

# CHECK IF THE USER IS Correct as per Arg#2

echo " "
if [ $(whoami) != "${oraowner}" ]
then
        echo "ATTENTION : Please execute this script as ${oraowner} user"
        exit 0;
fi


# SCRIPT AND OUTPUT DIRECTORIES INITIALIZATION

SCRIPT_DIRECTORY=/var/opt/oracle/BACS/script
if [[ ${oraowner} = "oracle" ]]; then
   if [[ -d /oracle/dba/output/generic ]]; then
      OUTPUT_DIRECTORY=/oracle/dba/output/generic
   else
      OUTPUT_DIRECTORY=/var/bl_ora/log
      if [[ ! -d $OUTPUT_DIRECTORY ]]; then
         echo "OUTPUT_DIRECTORY: $OUTPUT_DIRECTORY was not found on this server ... Exiting"
         exit 0
      fi
   fi
else
   if [[ -d /oracle/dba/output/generic ]]; then
      OUTPUT_DIRECTORY=/oracle/dba/output/generic/${oraowner}
   else
      OUTPUT_DIRECTORY=/var/bl_ora/log/${oraowner}
      if [[ ! -d $OUTPUT_DIRECTORY ]]; then
         echo "OUTPUT_DIRECTORY: $OUTPUT_DIRECTORY was not found on this server ... Exiting"
         exit 0
      fi
   fi
fi

# REMOVE THE TEMPORARY LOG FILE
        rm -f $OUTPUT_DIRECTORY/stop.log
#Purge pre/post files from previous executions before moving on ...
if [[ -d ${OUTPUT_DIRECTORY} ]]; then
   rm -f ${OUTPUT_DIRECTORY}/pre_stop*.log
   rm -f ${OUTPUT_DIRECTORY}/post_stop*.log
   rm -f ${OUTPUT_DIRECTORY}/post_start*.log
fi


# ASSIGN A NEW LOG FILE
        LOG=$OUTPUT_DIRECTORY/stop.log;                                      echo "" >> ${LOG};


# CHECK THE SERVER TYPE

if [ -f $OUTPUT_DIRECTORY/server_type.log ];
then
        server_type=$(<$OUTPUT_DIRECTORY/server_type.log)
else
         if [ "$(ps -ef | grep crsd.bin | grep -v grep | wc -l)" -eq 1 ]
                then
                        if [ "$(ps -ef | grep crsd.bin | grep -v grep | grep "12\." | wc -l)" -eq 1 ]
                        then
                                export server_type="rac_12c"
                        elif [ "$(ps -ef | grep crsd.bin | grep -v grep | grep "11\." | wc -l)" -eq 1 ]
                        then
                                export server_type="rac_11g"
                        else
                                export server_type="rac_10g"
                        fi
        else
                        if [ "$(ps -ef | grep ohasd.bin | grep -v grep | grep "12\." | wc -l)" -eq 1 ]
                        then
                                export server_type="norac_asm12c"
                        elif [ "$(ps -ef | grep ohasd.bin | grep -v grep | grep "11\." | wc -l)" -eq 1 ]
                        then
                                export server_type="norac_asm11g"
                        elif [ "$(ps -ef | grep pmon | grep -v grep | grep "+ASM" | grep -v onitor | wc -l)" -eq 1 ]
                        then
                                export server_type="norac_asm10g"
                        else
                                export server_type="norac_noasm"
                        fi
        fi

# Address rare issue seen where crsd.bin not picked up even though it is in fact a RAC system!
if [[ "$server_type" == norac_asm* ]] && [[ -f /etc/oratab ]]; then
   ASM_LAST_CHAR=`grep "^\+ASM" /etc/oratab | cut -d":" -f1 -s | sed -e "s/.*\(.\)/\1/"`
   #ASM_LAST_CHAR=`ps -ef | grep "asm_pmon_" | grep -v grep | awk '{ print $NF }' | cut -d"_" -f3 -s | sed -e "s/.*\(.\)/\1/"`
   echo $ASM_LAST_CHAR | egrep '^[0-9]+$'
   if [ $? -eq 0 ]; then
      #echo "$ASM_LAST_CHAR is a number .. So it's a RAC "
      server_str_2=`echo $server_type | sed 's/asm//g' |cut -d"_" -f2 -s`
      export server_type="rac_${server_str_2}"
   fi
fi

   echo $server_type > $OUTPUT_DIRECTORY/server_type.log
fi



# COLLECT PRE-STATUS INFORMATION BEFORE STOPPING THE SERVICES

#       echo "Collecting pre-status report before stopping the services.... "  ;        echo "";
#       sh $SCRIPT_DIRECTORY/bl_status.ksh $oracred                                     ;echo ""


collect_lsnr()
{
   if [ $when = pre ]
   then
        if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
        then
                touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                ps -ef | grep lsnr | grep "^.*${oraowner} " | grep -v grep |sed -n 's/.*\(tnslsnr.*\).*/\1/p' | awk '{ print $2 }' | sed '/^\s*$/d' >$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        #elif [ $(( (`date +%s` - `stat -L --format %Y $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log`) > (2880*60) )) -eq 1 ]
        #then
                #rm -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                #ps -ef | grep lsnr | grep "^.*${oraowner} " | grep -v grep |sed -n 's/.*\(tnslsnr.*\).*/\1/p' | awk '{ print $2 }' | sed '/^\s*$/d' >$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                for LSNR in `ps -ef | grep lsnr | grep "^.*${oraowner} " | grep -v grep |sed -n 's/.*\(tnslsnr.*\).*/\1/p' | awk '{ print $2 }' | sed '/^\s*$/d'`
                do
                        if [ $(grep -w "$LSNR" $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log | wc -l) -eq 1 ]
                        then
                                sleep 0
                        else
                                echo $LSNR >> $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                done
        fi

        #Capture lsnr name and OHs on this host
        if [[ -f $OUTPUT_DIRECTORY/pre_config_lsnr.log ]]; then
           rm $OUTPUT_DIRECTORY/pre_config_lsnr.log
        fi
        ps -ef | grep tnslsnr | grep "^.*${oraowner} " | grep -v grep | while read LSNR_PROC
        do
         #LSNR_NAME=`echo $LSNR_PROC | grep -oP '(?<=tnslsnr )\w+'`
         #LSNR_HOME=`echo $LSNR_PROC | grep -oP '....................................................../bin/tnslsnr ' | awk '{ print $NF }' | awk -F'/' '{for(i = 1; i <= NF - 2; i++) printf("%s%s", $i, i == NF - 2 ? "" : OFS)}' OFS="/"`
         LSNR_NAME=`echo $LSNR_PROC | sed -n 's/.*\(tnslsnr.*\).*/\1/p' | awk '{ print $2 }'`
         LSNR_HOME=`echo $LSNR_PROC | sed -n 's/.*\(..........................................................tnslsnr\).*/\1/p' | awk '{ print $NF }' | grep -v "\.\.\.\.tnslsnr" | sed 's/\/bin\/tnslsnr//g' | head -1`
         echo "${LSNR_NAME}:${LSNR_HOME}" >> $OUTPUT_DIRECTORY/pre_config_lsnr.log
        done

   elif [ $when = post ]
        then
             ps -ef | grep lsnr | grep "^.*${oraowner} " | grep -v grep | sed -n 's/.*\(tnslsnr.*\).*/\1/p' | awk '{ print $2 }' | sed '/^\s*$/d' >$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}

collect_mrp()
{
   if [ $when = pre ]
   then
        if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
        then
                touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                ps -ef | grep mrp | sort -k9 | grep "^.*${oraowner} " | grep -v grep | awk '{ print $NF }' | cut -d"_" -f3,4 -s >$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log

        else
                for MRP in `ps -ef | grep mrp | sort -k9 | grep "^.*${oraowner} " | awk '{ print $NF }' | cut -d"_" -f3,4 -s`
                do
                        if [ $(grep -w "$MRP" $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log | wc -l) -eq 1 ]
                        then
                                sleep 1
                        else
                                echo $MRP >> $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                done
        fi
   elif [ $when = post ]
        then
                ps -ef | grep mrp | sort -k9 | grep "^.*${oraowner} " | grep -v grep | awk '{ print $NF }' | cut -d"_" -f3,4 -s >$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}

collect_dbinst()
{
   if [ $when = pre ]
   then
        if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
        then
                touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s>$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log

        else
                for DBINST in `ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s`
                do
                        if [ $(grep -w "$DBINST" $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log | wc -l) -eq 1 ]
                        then
                                sleep 1
                        else
                                echo $DBINST >> $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                done
        fi
   elif [ $when = post ]
   then
                ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s>$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}

collect_asm()
{
   if [ $when = pre ]
   then
        if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
        then
                touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}'>$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
         else
        for ASM in `ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}'`
                do
                        if [ $(grep -w "$ASM" $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log | wc -l) -eq 1 ]
                        then
                                sleep 1
                        else
                                echo $ASM >> $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                done
        fi
        elif [ $when = post ]
        then
                ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}'>$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}

collect_agent()
{
        if [ $(ps -ef | grep emagent | grep "^.*${oraowner} " | grep -v grep | wc -l) -ne 0 ]
        then
                ORACLE_SID=`cat /etc/oratab | awk 'BEGIN{FS=":"}{print $1}' | grep OEM | grep -v "^#"`
                export ORAENV_ASK=NO                                                    >/dev/null;
                if [[ ! -z $ORACLE_SID ]]; then
                . oraenv                                                                >/dev/null
                if [ $when = pre ]
                then
                        if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
                        then
                                touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                                emctl status agent | grep "Agent is Running and Ready" | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                         else
                                if [ $(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log) -gt 0 ]
                                then
                                        sleep 1
                                else
                                        emctl status agent | grep "Agent is Running and Ready" | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                                fi
                        fi
                elif [ $when = post ]
                then
                        emctl status agent | grep "Agent is Running and Ready" | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                else
                        sleep 0
                fi

                fi
        else
                echo 0 > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        fi
}

collect_fgl()
{
        if [ $when = pre ]
        then
                if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
                then
                        touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        ps -ef | grep -i foglight | grep "^.*${oraowner} " | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                 else
                        if [ $(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log) -gt 0 ]
                        then
                                sleep 1
                        else
                                ps -ef | grep -i foglight | grep "^.*${oraowner} " | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                fi
        elif [ $when = post ]
        then
                ps -ef | grep -i foglight | grep "^.*${oraowner} " | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}

collect_portutil()
{
        if [ $when = pre ]
        then
                if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
                then
                        touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                         ps -ef | grep "portdaemon.pl" | grep -v grep | grep port | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                 else
                        if [ $(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log) -gt 0 ]
                        then
                                sleep 1
                        else
                                 ps -ef | grep "portdaemon.pl" | grep -v grep | grep port | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                fi
        elif [ $when = post ]
        then
                ps -ef | grep "portdaemon.pl" | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi

        #Capturing the port information
        if [ ! -f $OUTPUT_DIRECTORY/portnum.val ]
        then
                touch $OUTPUT_DIRECTORY/portnum.val
                ps -ef | grep portdaemon.pl | grep -v grep | awk 'BEGIN{FS=" "}{print $NF}' > $OUTPUT_DIRECTORY/portnum.val
        elif [ $(( (`date +%s` - `stat -L --format %Y $OUTPUT_DIRECTORY/portnum.val`) > (2880*60) )) = 1 ]
        then
                rm -f $OUTPUT_DIRECTORY/portnum.val
                ps -ef | grep portdaemon.pl | grep -v grep | awk 'BEGIN{FS=" "}{print $NF}' > $OUTPUT_DIRECTORY/portnum.val
        else
                sleep 1
        fi

}

collect_osw()
{
        if [ $when = pre ]
        then
                if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
                then
                        touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        ps -ef | grep -i OSW | grep "^.*${oraowner} " | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                else
                        if [ $(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log) -gt 0 ]
                        then
                                sleep 1
                        else
                                ps -ef | grep -i OSW | grep "^.*${oraowner} " | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                fi
        elif [ $when = post ]
        then
                ps -ef | grep -i OSW | grep "^.*${oraowner} " | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}


collect_instancestatus()
{
if [ "$(ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | wc -l)" -ne 0 ]
then

        rm -f $OUTPUT_DIRECTORY/db:inst
        rm -f $OUTPUT_DIRECTORY/db:inst.log
        rm -f $OUTPUT_DIRECTORY/unidb:inst.log
        rm -f $OUTPUT_DIRECTORY/pre_gg_inst.log
        rm -f $OUTPUT_DIRECTORY/pre_stop_dbopen.log
        rm -f $OUTPUT_DIRECTORY/pre_stop_dbopenronly.log
        rm -f $OUTPUT_DIRECTORY/pre_stop_dbmount.log


        # Grab unique name and other info from each runing Instance
        for SID in `ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s`
                        do

                                export ORACLE_SID=$SID
                                export ORAENV_ASK=NO                                                    >/dev/null
                                . oraenv                                                                >/dev/null

                                op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba'<< EOFUDB1
                                set echo off;
                                set heading off;
                                set linesize 200 pages 100;
                                select 'DBINST_ENTRY:'||d.DB_UNIQUE_NAME||':'||i.INSTANCE_NAME from v\\$database d, v\\$instance i;
                                select 'PRE_OGG_DBINST:'||username||':${ORACLE_SID}' from gv\\$session where upper(username) like 'GGSADMIN%' and rownum < 2;
EOFUDB1`
                                echo "$op" | grep "DBINST_ENTRY:"    >> $OUTPUT_DIRECTORY/unidb:inst.log
                                echo "$op" | grep "PRE_OGG_DBINST:"  >> $OUTPUT_DIRECTORY/pre_gg_inst.log
                        done

 if [[ $server_type = "rac_12c" ]] || [[ $server_type = "rac_11g" ]] || [[ $server_type = "rac_10g" ]]
        then
                        export ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}'`
                        export ORAENV_ASK=NO                                                    >/dev/null
                        . oraenv                                                                >/dev/null
                        for SID in `ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s`
                        do
                            if [[ ! -f $ORACLE_HOME/bin/crs_stat ]]; then
                                # To support 10gRAC where CRS may be runing from different home than +ASM
                                CRSD=`ps -ef | grep -v grep | grep root | sed -n 's/.*\(......................................................crsd.bin\).*/\1/p' | awk '{ print $NF }' | tail -1`
                                CRS_BIN=`dirname $CRSD`
                                UNIQUE_NAME=`echo $(crs_stat | grep "ora." | grep ".db$" | grep -i ${SID%?} | tr "[:lower:]" "[:upper:]")`

                                if [[ ! -z $UNIQUE_NAME ]]; then
                                   echo $(${CRS_BIN}/crs_stat | grep "ora." | grep ".db$" | grep -i ${SID%?} | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')":"$SID >> $OUTPUT_DIRECTORY/db:inst
                                else
                                   if [[ -f $OUTPUT_DIRECTORY/unidb:inst.log ]]; then
                                        #Do lookup from status file to find correct Unique name
                                        UNI_NAME=`grep "DBINST_ENTRY:" $OUTPUT_DIRECTORY/unidb:inst.log | grep ":${SID}$"| cut -d":" -f2 -s`
                                        if [[ ! -z $UNI_NAME ]]; then
                                           echo $(${CRS_BIN}/crs_stat | grep "ora." | grep ".db$" | grep -i ${UNI_NAME} | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')":"$SID >> $OUTPUT_DIRECTORY/db:inst
                                        else
                                           echo $(${CRS_BIN}/crs_stat | grep "ora." | grep ".db$" | grep -i ${SID%?} | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')":"$SID >> $OUTPUT_DIRECTORY/db:inst
                                        fi
                                   else
                                        echo $(${CRS_BIN}/crs_stat | grep "ora." | grep ".db$" | grep -i ${SID%?} | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')":"$SID >> $OUTPUT_DIRECTORY/db:inst
                                   fi
                                fi
                            else


                                R_DBNAME=` echo $SID | sed "s/[1-9]$//g"|sed "s/_$//g"` ;ONENode_DB="";

                                if [ "$(srvctl config database -d $R_DBNAME |grep -w "RACOneNode" | wc -l)" -eq 1 ]
                                then
                                        export LOCAL_NODE=`hostname -s`
                                        export ONENode_DB=`echo ":$LOCAL_NODE"`
                                fi

                                UNIQUE_NAME=`echo $(crs_stat | grep "ora." | grep ".db$" | grep -i $R_DBNAME | tr "[:lower:]" "[:upper:]")`

                                if [[ -f $OUTPUT_DIRECTORY/unidb:inst.log ]] && [[ ! -z $UNIQUE_NAME ]]; then
                                     UNIQUE_SID=`echo $UNIQUE_NAME | cut -d"." -f2 -s`
                                     CHK_UNI_LOG=`grep ":${UNIQUE_SID}:" $OUTPUT_DIRECTORY/unidb:inst.log`
                                     if [[ -z $CHK_UNI_LOG ]]; then
                                        UNIQUE_NAME=""
                                     fi
                                fi

                                if [[ ! -z $UNIQUE_NAME ]]; then
                                   echo $(crs_stat | grep "ora." | grep ".db$" | grep -i $R_DBNAME | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')":"$SID$ONENode_DB >> $OUTPUT_DIRECTORY/db:inst
                                else
                                   if [[ -f $OUTPUT_DIRECTORY/unidb:inst.log ]]; then
                                        #Do lookup from status file to find correct Unique name and then try to match it with crs "*.db" resource .. if nothing then default UNIQ NAME as ${SID%?}
                                        UNI_NAME=`grep "DBINST_ENTRY:" $OUTPUT_DIRECTORY/unidb:inst.log | grep ":${SID}$"| cut -d":" -f2 -s`
                                        if [[ ! -z $UNI_NAME ]]; then
                                           CRS_UNI_MATCH=`echo $(crs_stat | grep "ora." | grep ".db$" | grep -i ${UNI_NAME} | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')`
###                                           if [[ ! -z $CRS_UNI_MATCH ]]; then
                                        if [[ "$(crs_stat | grep "ora." | grep ".db$" | grep -i ${UNI_NAME} | wc -l)" -eq 1 ]]; then
                                              echo $(crs_stat | grep "ora." | grep ".db$" | grep -i ${UNI_NAME} | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')":"$SID >> $OUTPUT_DIRECTORY/db:inst
                                           else
                                              echo $(crs_stat | grep "ora." | grep ".db$" | grep -i $R_DBNAME | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')":"$SID >> $OUTPUT_DIRECTORY/db:inst
                                           fi
                                        else
                                           echo $(crs_stat | grep "ora." | grep ".db$" | grep -i $R_DBNAME | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')":"$SID >> $OUTPUT_DIRECTORY/db:inst
                                        fi
                                   else
                                        echo $(crs_stat | grep "ora." | grep ".db$" | grep -i $R_DBNAME | tr "[:lower:]" "[:upper:]" | awk 'BEGIN{FS="."}{print $2}')":"$SID >> $OUTPUT_DIRECTORY/db:inst
                                   fi
                                fi
                            fi
                        done
        else
                for SID in `ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s`
                        do
                        echo $SID":"$SID >> $OUTPUT_DIRECTORY/db:inst
                done
        fi

        cp $OUTPUT_DIRECTORY/db:inst $OUTPUT_DIRECTORY/db:inst.log

find_instancestatus()
{
     while read data
     do
        export ORACLE_DB=$(echo $data | awk 'BEGIN{FS=":"}{print $1}')       >/dev/null
        export ORACLE_SID=$(echo $data | awk 'BEGIN{FS=":"}{print $2}')       >/dev/null
        export RAC_ONEDB=$(echo $data | awk 'BEGIN{FS=":"}{print $3}')       >/dev/null
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
                op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba'<< EOFSTATUS
                set echo off;
                set head off;
                select open_mode from v\\$database;
EOFSTATUS`

        if [ $when = pre ]
        then
                if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$dbopmnt".log ]
                then
                        touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$dbopmnt".log
                        if [ "$(echo "$op" | grep "$STATUS" | wc -l)" -eq 1 ]
                        then
                                echo $ORACLE_DB":"$ORACLE_SID":"$RAC_ONEDB > $OUTPUT_DIRECTORY/"$when"_"$why"_"$dbopmnt".log
                        fi

                else
                        if [ "$(echo "$op" | grep "$STATUS" | wc -l)" -eq 1 ]
                        then
                                if [ $(grep -w "$ORACLE_DB" $OUTPUT_DIRECTORY/"$when"_"$why"_"$dbopmnt".log | wc -l) -eq 1 ]
                                then
                                        sleep 1
                                else
                                        echo $ORACLE_DB":"$ORACLE_SID":"$RAC_ONEDB >> $OUTPUT_DIRECTORY/"$when"_"$why"_"$dbopmnt".log

                                fi
                        fi
                fi
        elif [ $when = post ]
        then
                if [ "$(echo "$op" | grep "$STATUS" | wc -l)" -eq 1 ]
                then
                        echo $ORACLE_DB":"$ORACLE_SID":"$RAC_ONEDB >> $OUTPUT_DIRECTORY/"$when"_"$why"_"$dbopmnt".log
                fi
        else
        sleep 0
        fi

     done < $OUTPUT_DIRECTORY/db:inst.log
}

        STATUS="READ WRITE"; dbopmnt=dbopen; find_instancestatus
        STATUS="READ ONLY"; dbopmnt=dbopenronly; find_instancestatus
        STATUS=MOUNT; dbopmnt=dbmount; find_instancestatus
else
        touch $OUTPUT_DIRECTORY/pre_stop_dbopen.log
        touch $OUTPUT_DIRECTORY/pre_stop_dbmount.log
        touch $OUTPUT_DIRECTORY/pre_stop_dbopenronly.log
fi
        echo "OPEN READ WRITE mode " '  ' ":" $(cat $OUTPUT_DIRECTORY/pre_stop_dbopen.log)
        echo "OPEN READ ONLY mode " '  ' ":" $(cat $OUTPUT_DIRECTORY/pre_stop_dbopenronly.log)
        echo "MOUNT mode " '  ' ":" $(cat $OUTPUT_DIRECTORY/pre_stop_dbmount.log)

        echo "" >> ${LOG}
        echo "OPEN READ WRITE mode " '  ' ":" $(cat $OUTPUT_DIRECTORY/pre_stop_dbopen.log) >> ${LOG}
        echo "OPEN READ ONLY mode " '  ' ":" $(cat $OUTPUT_DIRECTORY/pre_stop_dbopenronly.log) >> ${LOG}
        echo "MOUNT mode " '  ' ":" $(cat $OUTPUT_DIRECTORY/pre_stop_dbmount.log) >> ${LOG}
        echo "" >> ${LOG}
}


collect_dbservices()
{
        rm -f $OUTPUT_DIRECTORY/"$when"_"$why"_dbservices.log
        while read data
        do
                export ORACLE_DB=$(echo $data | awk 'BEGIN{FS=":"}{print $1}')       >/dev/null
                export ORACLE_SID=$(echo $data | awk 'BEGIN{FS=":"}{print $2}')       >/dev/null
                export ORAENV_ASK=NO                                                    >/dev/null
                . oraenv                                                                >/dev/null
                #echo $ORACLE_DB:$ORACLE_SID":"$(srvctl status service -d $ORACLE_DB | grep $ORACLE_SID | awk 'BEGIN{FS=" "}{print $(NF-5)}') >> $OUTPUT_DIRECTORY/"$when"_"$why"_dbservices.log
                echo $ORACLE_DB:$ORACLE_SID":"$(srvctl status service -d $ORACLE_DB | grep $ORACLE_SID | grep -i "is running" | awk '{ print $2 }') >> $OUTPUT_DIRECTORY/"$when"_"$why"_dbservices.log
        done < $OUTPUT_DIRECTORY/db:inst.log
}

# OGG Support
##############
collect_ogg()
{
ps -ef | grep -v grep | grep "mgr PARAMFILE" | grep "^.*${oraowner} " | sed -n 's/.*\(....................................................................\/....prm \).*/\1/p' | awk '{ print $NF }' | grep -v "\.\.\.\.mgr.prm" >$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
}

# DBFS Support
###############
collect_dbfs()
{
ps -ef | grep -v grep | grep "dbfs_client" | grep "^.*${oraowner} " | sed -n 's/.*\(................................................................\/bin\/dbfs_client\).*/\1/p' | awk '{ print $NF }' | grep -v "\.\.\.\.dbfs_client"  >$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
}

display_pre_post()
{
if [ $when = pre ]
     then
          if [ $what = fgl ] || [ $what = agent ] || [ $what = mrp ] || [ $what = osw ] || [ $what = ogg ] || [ $what = dbfs ]
          then
               if [ "$(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log)" = 0 ] || [ ! -s $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
               then
                    echo "PRE stop" $what "running " ":" "NO"                                | tee -a ${LOG}
               else
                    echo "PRE stop" $what "running " ":" "YES"                               | tee -a ${LOG}
               fi
          elif [ $what = asm ] || [ $what = dbinst ] || [ $what = lsnr ]
          then
               if [ "$(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log)" = 0 ] || [ ! -s $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
               then
                    echo "PRE stop" $what "running " ":" "NO"                                | tee -a ${LOG}
               else
                    echo "PRE stop" $what "running " ":" $(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log) | tee -a ${LOG}
               fi
          fi
elif [ $when = post ]
     then
          if [ $what = fgl ] || [ $what = agent ] || [ $what = mrp ] || [ $what = osw ] || [ $what = ogg ] || [ $what = dbfs ]
          then
               if [ "$(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log)" = 0 ] || [ ! -s $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
               then
                    echo "POST stop" $what "running " ":" "NO"                          | tee -a ${LOG}
                    echo ""                    | tee -a ${LOG}
               else
                    echo "POST stop" $what "running " ":" "YES"                         | tee -a ${LOG}
                    echo ""                    | tee -a ${LOG}
               fi
          elif [ $what = asm ] || [ $what = dbinst ] || [ $what = lsnr ]
          then
               if [ "$(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log)" = 0 ] || [ ! -s $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
               then
                    echo "POST stop" $what "running " ":" "NO"                          | tee -a ${LOG}
                    echo ""                    | tee -a ${LOG}
               else
                    echo "POST stop" $what "running " ":" $(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log) | tee -a ${LOG}
                    echo ""                    | tee -a ${LOG}
               fi
          fi
fi
}

stopped_status()
{
if [[ -f $OUTPUT_DIRECTORY/post_"$why"_"$what".log ]]; then
        if [ "$(cat $OUTPUT_DIRECTORY/post_"$why"_"$what".log)" = 0 ] || [ ! -s $OUTPUT_DIRECTORY/post_"$why"_"$what".log ]
        then
                echo "yes" > $OUTPUT_DIRECTORY/stopped_$what.log
                echo "OK : successfully stopped $what"                        | tee -a ${LOG}
        else
                echo "no" > $OUTPUT_DIRECTORY/stopped_$what.log
                echo "ATTENTION : $what didn't stop. Please check"            | tee -a ${LOG}
        fi
fi
}


# STOPPING THE SERVICES

stop_asm_10g()
{
echo "STOPPING" $what        | tee -a ${LOG}

                for SID in `ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}'`
                        do
                export ORACLE_SID=${SID}                                                >/dev/null
                export ORAENV_ASK=NO                                                    >/dev/null
               . oraenv                                                                >/dev/null
                echo "Stopping instance " $ORACLE_SID                      | tee -a ${LOG}
                op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba'<< EOF
                set echo off;
                set linesize 200 pages 100;
                shutdown immediate;
EOF`
                        echo "$op"                                                        >> ${LOG};echo "" >> ${LOG}
                        done
        sleep 5
}

stop_db_rac()
{
echo "STOPPING" $what        | tee -a ${LOG}

     while read data
     do
        export ORACLE_DB=$(echo $data | awk 'BEGIN{FS=":"}{print $1}')       >/dev/null
        export ORACLE_SID=$(echo $data | awk 'BEGIN{FS=":"}{print $2}')       >/dev/null
        export RACONE_DB=$(echo $data | awk 'BEGIN{FS=":"}{print $3}')       >/dev/null
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
        if [ $ORACLE_DB = $ORACLE_SID ] || [[ ! -z $RACONE_DB ]]
        then
                echo "Stopping Database " $ORACLE_SID                      | tee -a ${LOG}
                srvctl stop database -d $ORACLE_DB                         | tee -a ${LOG}
        else
                echo "Stopping instance " $ORACLE_SID                      | tee -a ${LOG}
                srvctl stop instance -d $ORACLE_DB -i $ORACLE_SID             | tee -a ${LOG}    # >/dev/null
        fi
        sleep 5
     done < $OUTPUT_DIRECTORY/db:inst.log
}

stop_dbservices()
{
echo "STOPPING ANY DB SERVICE(S) on " $what        | tee -a ${LOG}

     while read data
     do
        export ORACLE_DB=$(echo $data | awk 'BEGIN{FS=":"}{print $1}')       >/dev/null
        export ORACLE_SID=$(echo $data | awk 'BEGIN{FS=":"}{print $2}')       >/dev/null
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
        export LOCAL_NODE=`hostname -s`
        if [[ "$server_type" == rac* ]]; then
                     # Stop Any services for $ORACLE_DB on this node
                     if [ $(srvctl status service -d $ORACLE_DB | grep $ORACLE_SID | grep -i "is running" | awk '{ print $2 }' | wc -l) -ne 0 ]
                     then
                        #Loop Through Running Services
                        srvctl status service -d $ORACLE_DB | grep $ORACLE_SID | grep -i "is running" | awk '{ print $2 }' | while read DB_SERV_NAME
                        do
                           echo "srvctl disable service -d $ORACLE_DB -s $DB_SERV_NAME" >> ${LOG}
                           srvctl disable service -d $ORACLE_DB -s $DB_SERV_NAME  >> ${LOG}
                           echo " srvctl stop service -d $ORACLE_DB -s $DB_SERV_NAME -n $LOCAL_NODE" >> ${LOG}
                           srvctl stop service -d $ORACLE_DB -s $DB_SERV_NAME -n $LOCAL_NODE >> ${LOG}
                        done
                     fi
        else
                     # Stop Any services for $ORACLE_DB on this node
                     if [ $(srvctl status service -d $ORACLE_DB | grep -i "is running" | awk '{ print $2 }' | wc -l) -ne 0 ]
                     then
                        echo "srvctl stop service -d $ORACLE_DB" >> ${LOG}
                        srvctl stop service -d $ORACLE_DB  >> ${LOG}
                     fi
        fi
        sleep 5
     done < $OUTPUT_DIRECTORY/db:inst.log
}

stop_db_standalone()
{
echo "STOPPING" $what        | tee -a ${LOG}

                for SID in `ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s`
                        do
                export ORACLE_SID=${SID}                                                >/dev/null
                export ORAENV_ASK=NO                                                    >/dev/null
               . oraenv                                                                >/dev/null
                echo "Stopping instance " $ORACLE_SID                      | tee -a ${LOG}
                op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba'<< EOF
                set echo off;
                shutdown immediate;
EOF`
                        echo "$op"                                                        >> ${LOG};echo "" >> ${LOG}
                        done
        sleep 5
}

stop_mrp()
{
echo "STOPPING" $what           | tee -a ${LOG}
                for SID in `ps -ef | grep mrp | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s`
                        do
                export ORACLE_SID=${SID}                                                >/dev/null
                export ORAENV_ASK=NO                                                    >/dev/null
               . oraenv                                                                >/dev/null
                echo "Stopping MRP " $ORACLE_SID                      | tee -a ${LOG}
                op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba'<< EOF
                set echo off;
                alter database recover managed standby database cancel;
EOF`
                        echo "$op"                                                        >> ${LOG};echo "" >> ${LOG}
                        done
        sleep 5
}


stop_lsnr()
{
echo "STOPPING" $what           | tee -a ${LOG}

        if [[ $server_type = "rac_12c" ]] || [[ $server_type = "rac_11g" ]] || [[ $server_type = "norac_asm11g" ]] || [[ $server_type = "norac_asm12c" ]]
        then
                ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}' | head -1`
        else
                ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s | head -1`
        fi
                export ORAENV_ASK=NO                                                    >/dev/null
               . oraenv                                                                >/dev/null
                        #for lsnr in `ps -ef | grep lsnr | grep "^.*${oraowner} " | grep -v grep | grep $ORACLE_HOME | grep -oP '(?<=tnslsnr )\w+'`
        for lsnr in `ps -ef | grep lsnr | grep "^.*${oraowner} " | grep -v grep | grep $ORACLE_HOME | sed -n 's/.*\(tnslsnr.*\).*/\1/p' | awk '{ print $2 }' | sed '/^\s*$/d'`
                        do
                                echo ""                                     >> ${LOG}
                                echo "Stopping listener $lsnr running from OH=${ORACLE_HOME} "             | tee -a ${LOG}
                                echo "lsnrctl stop $lsnr"                   >> ${LOG}
                                echo ""                                     >> ${LOG}
                                lsnrctl stop $lsnr                          >> ${LOG}
                                sleep 10
                        done

        for lsnr in `ps -ef | grep lsnr | grep "^.*${oraowner} " | grep -v grep | sed -n 's/.*\(tnslsnr.*\).*/\1/p' | awk '{ print $2 }' | sed '/^\s*$/d'`
                        do
                                #echo "Stopping listener " $lsnr             | tee -a ${LOG}
                                if [[ -f $OUTPUT_DIRECTORY/pre_config_lsnr.log ]]; then
                                   LSNR_HOME=`cat $OUTPUT_DIRECTORY/pre_config_lsnr.log | grep "^${lsnr}:" | cut -d":" -f2 -s |tail -1`
                                   if [[ $ORACLE_HOME != $LSNR_HOME ]]; then
                                      if [[ -f /etc/oratab ]]; then
                                         ORACLE_SID=`grep $LSNR_HOME /etc/oratab | grep -v "^#" | cut -d":" -f1 -s |tail -1`
                                         export ORAENV_ASK=NO                                                    >/dev/null
                                         if [[ ! -z $ORACLE_SID ]]; then
                                            . oraenv                                                                >/dev/null
                                         fi
                                      fi
                                   fi
                                fi
                                echo ""                                     >> ${LOG}
                                echo "Stopping listener $lsnr running from OH=${ORACLE_HOME} "             | tee -a ${LOG}
                                echo "lsnrctl stop $lsnr"  >> ${LOG}
                                lsnrctl stop $lsnr                 >> ${LOG}
                                echo ""  >> ${LOG}
                        done
        sleep 5
}

stop_agent()
{
echo "STOPPING" $what                   | tee -a ${LOG}

        if [ $(ps -ef | grep emagent | grep "^.*${oraowner} " | grep -v grep | wc -l) -ne 0 ]
        then
                ORACLE_SID=`cat /etc/oratab | awk 'BEGIN{FS=":"}{print $1}' | grep OEM | grep -v "^#"`
                export ORAENV_ASK=NO                                                    >/dev/null;
                if [[ ! -z $ORACLE_SID ]]; then
                . oraenv                                                                >/dev/null
                if [[ -f $ORACLE_HOME/bin/emctl ]]; then
                emctl stop agent                                                        >/dev/null;
                sleep 5
                fi
                fi
        else
                sleep 1
        fi
}

stop_fgl()
{
echo "STOPPING" $what           | tee -a ${LOG}

        if [[ -f /oracle/dba/foglight/fgl_5/bin/fglam ]]; then
           /oracle/dba/foglight/fgl_5/bin/fglam --stop                                  >/dev/null
           sleep 60
        else
           # Search for non standard Foglight bin and execute stop
           if [ "$(ps -ef | grep -i foglight | grep "^.*${oraowner} " | grep -v grep | wc -l)" -eq 0 ]
           then
               echo "Foglight is not running"                                          >> ${LOG} ; echo "" >> ${LOG}
           else
               FOGLIGHTBASE=`ps -ef | grep -i foglight | grep "^.*${oraowner} " | grep -v grep | sed -n 's/.*\(...............................................\/foglight\/fgl_5\).*/\1/p' | awk '{ print $NF }' | tail -1`
               if [[ ! -z $FOGLIGHTBASE ]]; then
                  FOGLIGHTBIN=${FOGLIGHTBASE}/bin/fglam
                  if [[ -f $FOGLIGHTBIN ]]; then
                     $FOGLIGHTBIN --stop  >/dev/null
                     sleep 60
                     echo "$FOGLIGHTBIN -d" > $OUTPUT_DIRECTORY/non_standard_fgl_start.cmd
                     chmod 755 $OUTPUT_DIRECTORY/non_standard_fgl_start.cmd
                  fi
               fi
           fi
        fi
}

stop_portutil()
{
echo -e ' \t ' "STOPPING" $what         | tee -a ${LOG}

        if [ "$(cat $OUTPUT_DIRECTORY/portnum.val | wc -l)" != 0 ]
        then
                while read portnum
                do
                        /opt/portdaemon/bin/stop_port_daemon $portnum       >/dev/null;
                        sleep 10
                done < $OUTPUT_DIRECTORY/portnum.val
        fi
}

stop_osw()
{
echo "STOPPING" $what         | tee -a ${LOG}

        if [ $(ps -ef | grep -i OSW | grep "^.*${oraowner} " | grep -v grep | wc -l) -ne 0 ]
        then
                ps -ef | grep -i OSW | grep "^.*${oraowner} " | grep -v grep| grep -v osw | awk 'BEGIN{FS=" "}{print $(NF-3)" "$(NF-2)" "$(NF-1)" "$(NF-0)}' > $OUTPUT_DIRECTORY/start_osw.cmd
                if [[ -f /oracle/dba/diagtools/oswbb/stopOSWbb.sh ]]; then
                /oracle/dba/diagtools/oswbb/stopOSWbb.sh                >/dev/null
                sleep 5
                fi
                # Non standard path
                OSW_PIDVALUE=`ps -e | grep -i OSWatcher.sh | awk '{print $1}'`
                OSW_BASEPATH=`ls -l /proc/$OSW_PIDVALUE/cwd | awk '{print $NF}'`
                OSWNONSTOP=$OSW_BASEPATH/stopOSWbb.sh
                  if [[ -f $OSWNONSTOP ]]; then
                     $OSWNONSTOP  >/dev/null
                     sleep 1
                  fi
        else
                touch $OUTPUT_DIRECTORY/start_osw.cmd
        fi
}

######################
# DBFS and OGG Support
######################
stop_ogg()
{
echo "STOPPING" $what                   | tee -a ${LOG}

#Initialise any previous gg config files
if [[ -f ${OUTPUT_DIRECTORY}/gg_config.txt ]]; then
       rm ${OUTPUT_DIRECTORY}/gg_config.txt
fi
if [[ -f ${OUTPUT_DIRECTORY}/pre_stop_gg_autostart.prm ]]; then
       rm ${OUTPUT_DIRECTORY}/pre_stop_gg_autostart.prm
fi

# Set Ora env
ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep "+ASM" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s`
export ORAENV_ASK=NO                                                    >/dev/null;
if [[ ! -z $ORACLE_SID ]]; then
   . oraenv                                                                >/dev/null
else
   ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s| tail -1`
   . oraenv                                                                >/dev/null
fi

ps -ef | grep -v grep | grep "mgr PARAMFILE" | grep "^.*${oraowner} " | sed -n 's/.*\(...................................................................\/....prm \).*/\1/p' | awk '{ print $NF }' | grep -v "\.\.\.\.mgr.prm" | while read GG_MGRPROC
        do

        # Proceed only if Golden Gate Manager is running
        if [[ ! -z $GG_MGRPROC ]]; then

                 GG_HOME=`echo $GG_MGRPROC | sed 's/\/dirprm\/mgr.prm//g' | sed 's/\/dirprm\/MGR.prm//g'`
                #############################

                LOCAL_NODE=`hostname -s`

                # Loop through any GG CRS res and shut them down
                if [[ -f ${ORACLE_HOME}/bin/crsctl ]]; then

                ${ORACLE_HOME}/bin/crsctl stat res -p |grep "^NAME=" | grep -iE 'gg|golden' | grep -v vip | grep -v "\.svc" | grep -v "\.db" | grep -v "ora\." | cut -d"=" -f2 -s | while read CRS_RES_GG
                do
                if [[ ! -z $CRS_RES_GG ]]; then
                   CRS_STATE=`${ORACLE_HOME}/bin/crsctl stat res $CRS_RES_GG | grep "STATE=" | awk '{ print $1 }' | cut -d"=" -f2 -s`
                   if [[ "$CRS_STATE" == "ONLINE" ]]; then
                      echo "${ORACLE_HOME}/bin/crsctl stop res $CRS_RES_GG" >> ${LOG}
                      ${ORACLE_HOME}/bin/crsctl stop res $CRS_RES_GG >> ${LOG}
                      sleep 60
                      # Capture gg start config for each GG_HOME on this node
                      echo "GGCRS@${CRS_RES_GG}:${GG_HOME}:${LOCAL_NODE}" >> ${OUTPUT_DIRECTORY}/gg_config.txt

                      if [[ -n `find ${GG_HOME}/dirprm/ -maxdepth 1 -iname mgr.prm` ]]; then
                         if [[ -f ${GG_HOME}/dirprm/mgr.prm ]]; then
                            grep -i "AUTOSTART " ${GG_HOME}/dirprm/mgr.prm | grep -v "^#" | grep -v "^--" >> ${OUTPUT_DIRECTORY}/pre_stop_gg_autostart.prm
                         else
                            grep -i "AUTOSTART " ${GG_HOME}/dirprm/MGR.prm | grep -v "^#" | grep -v "^--" >> ${OUTPUT_DIRECTORY}/pre_stop_gg_autostart.prm
                         fi
                      fi
                   fi
                fi
                #sleep 5
                done

                fi

                #############################
                # Check to see if GG mgr is still running from this GG_HOME - if so shutdown manually ..
                if [[ -f ${GG_HOME}/dirpcs/MGR.pcm ]]; then

                if [[ -f ${OUTPUT_DIRECTORY}/pre_gg_inst.log ]]; then
                   ORACLE_GG_SID=`cat ${OUTPUT_DIRECTORY}/pre_gg_inst.log | grep "PRE_OGG_DBINST:" | cut -d":" -f3 -s | tail -1`
                fi

                if [[ ! -z $ORACLE_GG_SID ]]; then
                   ORACLE_SID=$ORACLE_GG_SID
                   . oraenv
                else
                  ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s | tail -1`
                  if [[ ! -z $ORACLE_SID ]]; then
                     . oraenv
                  fi
                fi

${GG_HOME}/ggsci << EOF >>${LOG}
stop *
stop mgr !
exit
EOF
                  sleep 60
                  echo "GGNOCRS:${GG_HOME}:${LOCAL_NODE}" >> ${OUTPUT_DIRECTORY}/gg_config.txt
                  if [[ -n `find ${GG_HOME}/dirprm/ -maxdepth 1 -iname mgr.prm` ]]; then
                     if [[ -f ${GG_HOME}/dirprm/mgr.prm ]]; then
                        grep -i "AUTOSTART " ${GG_HOME}/dirprm/mgr.prm | grep -v "^#" | grep -v "^--" >> ${OUTPUT_DIRECTORY}/pre_stop_gg_autostart.prm
                     else
                        grep -i "AUTOSTART " ${GG_HOME}/dirprm/MGR.prm | grep -v "^#" | grep -v "^--" >> ${OUTPUT_DIRECTORY}/pre_stop_gg_autostart.prm
                     fi
                  fi
                fi

        else
                echo " No Oracle Golden Gate processes detected on this host ... " >> ${LOG}
        fi

        done
}


stop_dbfs()
{
echo "STOPPING" $what                   | tee -a ${LOG}

#Initialise any previous dbfs config files
if [[ -f ${OUTPUT_DIRECTORY}/dbfs_config.txt ]]; then
       rm ${OUTPUT_DIRECTORY}/dbfs_config.txt
fi

# Set Ora env
ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}'`
export ORAENV_ASK=NO                                                    >/dev/null;
if [[ ! -z $ORACLE_SID ]]; then
   . oraenv                                                                >/dev/null
else
   ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s | tail -1`
   . oraenv                                                                >/dev/null
fi

        ps -ef | grep -v grep | grep "dbfs_client" | grep "^.*${oraowner} " | sed -n 's/.*\(................................................................\/bin\/dbfs_client\).*/\1/p' | awk '{ print $NF }' | grep -v "\.\.\.\.dbfs_client" | sort -u | while read DBFS_PROC
        do

        # Proceed only if DBFS is running
        if [[ ! -z $DBFS_PROC ]]; then
                #Check if this dbfs is setup as a crs resource
                DBFS_HOME=`echo $DBFS_PROC | sed 's/\/bin\/dbfs_client//g'`
                #############################

                LOCAL_NODE=`hostname -s`

                if [[ -f ${ORACLE_HOME}/bin/crsctl ]]; then

                # Loop through any DBFS CRS res and shut them down
                ${ORACLE_HOME}/bin/crsctl stat res -p |grep "^NAME=" | grep -i dbfs | grep -v "\.svc" | grep -v "\.db" | grep -v "ora\." | cut -d"=" -f2 -s | while read CRS_RES_DBFS
                do
                if [[ ! -z $CRS_RES_DBFS ]]; then
                   CRS_STATE=`${ORACLE_HOME}/bin/crsctl stat res $CRS_RES_DBFS | grep "STATE=" | awk '{ print $1 }' | cut -d"=" -f2 -s`
                   if [[ "$CRS_STATE" == "ONLINE" ]]; then
                      echo "DBFS CRS Resource: $CRS_RES_DBFS was detected. Now stopping this resource ..." >>${LOG}
                      echo "${ORACLE_HOME}/bin/crsctl stop res $CRS_RES_DBFS -f" >>${LOG}
                      ${ORACLE_HOME}/bin/crsctl stop res $CRS_RES_DBFS -f >>${LOG}
                      # Capture dbfs start config for each DBFS_HOME on this node
                      echo "DBFSCRS@${CRS_RES_DBFS}:${DBFS_HOME}:${LOCAL_NODE}" >> ${OUTPUT_DIRECTORY}/dbfs_config.txt
                   else
                      echo "" >>${LOG}
                      echo "DBFS CRS Resource: $CRS_RES_DBFS was detected but found to be in $CRS_STATE STATE ... No action taken! " >>${LOG}
                      echo "" >>${LOG}
                   fi
                fi
                sleep 30
                done

                fi

                #############################
                # Check to see if dbfs is still running from this DBFS_HOME - if so shutdown manually ..
                if [ $(ps -ef | grep "^.*${oraowner} " | grep -v grep | grep "${DBFS_HOME}/bin/dbfs_client" | wc -l) -ne 0 ]; then

                   #get DBFS_MOUNT
                   #DBFS_MOUNT=`ps -ef | grep "^.*${oraowner} " | grep -v grep | grep "dbfs_client" | awk '{ print $NF }'`
                   ps -ef | grep "^.*${oraowner} " | grep -v grep | grep "${DBFS_HOME}/bin/dbfs_client" | awk '{ print $NF }' | while read DBFS_MOUNT
                   do

                   if [[ ! -z $DBFS_MOUNT ]]; then
                      echo "" >>${LOG}
                      echo "Will Try to stop dbfs processes manually. Because either dbfs CRS resources not found  OR  DBFS still running even after CRS resource stopped .." >>${LOG}
                      echo "Manually Stopping dbfs mount dir: $DBFS_MOUNT ..." >>${LOG}
                      echo "/bin/fusermount -u -z $DBFS_MOUNT" >>${LOG}
                      /bin/fusermount -u -z $DBFS_MOUNT >>${LOG}
                      echo "" >>${LOG}
                   fi

                   sleep 10
                   # Capture dbfs start config for each GG_HOME on this node
                   if [[ -f ${OUTPUT_DIRECTORY}/dbfs_config.txt ]]; then
                      CHK_DBFSHOME_CONFIGFILE=`grep ":${DBFS_HOME}:" ${OUTPUT_DIRECTORY}/dbfs_config.txt`
                   fi
                   if [[ -z $CHK_DBFSHOME_CONFIGFILE ]]; then
                       echo "DBFSNOCRS:${DBFS_HOME}:${LOCAL_NODE}" >> ${OUTPUT_DIRECTORY}/dbfs_config.txt
                   fi

                   done

                   #Check if any dbfs process still running on this host and kill it ..
                   ps -ef | grep "^.*${oraowner} " | grep -v grep | grep "${DBFS_HOME}/bin/dbfs_client" | awk '{ print $2 }' | while read DBFS_PID
                   do
                       if [[ ! -z $DBFS_PID ]]; then
                          echo "" >>${LOG}
                          echo "No Luck ... Looks like below DBFS OSPID:$DBFS_PID is still Running! ..." >>${LOG}
                          echo "ps -ef | grep -v grep | grep $DBFS_PID | grep dbfs_client   =>" >>${LOG}
                          ps -ef | grep -v grep | grep "$DBFS_PID" | grep dbfs_client  >> ${LOG}
                          echo "" >>${LOG}
                          echo "Will try to KILL it .." >>${LOG}
                          echo "kill -9 $DBFS_PID" >>${LOG}
                          kill -9 $DBFS_PID >> ${LOG}
                          sleep 5
                          echo "Checking if DBFS OSPID:$DBFS_PID has gone ..." >>${LOG}
                          echo "ps -ef | grep -v grep | grep $DBFS_PID   =>" >>${LOG}
                          ps -ef | grep -v grep | grep "$DBFS_PID"  >> ${LOG}
                          echo "" >>${LOG}
                       fi
                   done
                fi

        else
                echo " No dbfs processes detected on this host ... " >/dev/null
        fi

        done
}

fgl_Autosys_blackout()
{
if [[ $OS != "AIX" ]]; then

export EFS_BLK_SCRIPT=/efs/dist/gmrtdba/dbascripts/scripts/common/bl_fgl_autosys_blkout.ksh

#Set Env with any running Instance;
for SID in `ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s | tail -1`
do
   export ORACLE_SID=${SID}                                                >/dev/null
   export ORAENV_ASK=NO                                                    >/dev/null
   . oraenv                                                                >/dev/null
done

if [[ ! -z $ORACLE_SID ]]; then

if [[ -f $EFS_BLK_SCRIPT ]]; then

echo "Attempting to apply HOST level BLACKOUTS for Foglight and Autosys via efs script: bl_fgl_autosys_blkout.ksh "                   | tee -a ${LOG}
echo " Foglight_Autosys_blackout Call Starting @ `date` " >> ${LOG}

export BLK_USR=ORA_BL_AUTO
export BLK_PHOST=`hostname -s`
export BLK_HOURS=4
export BLK_TYPE=h

sh $EFS_BLK_SCRIPT << ANSWERS
${BLK_USR}
${BLK_TYPE}
${BLK_PHOST}
${BLK_HOURS}
ANSWERS

echo " Foglight_Autosys_blackout Call Finished @ `date` " >> ${LOG}
echo "" >> ${LOG}

fi

fi

fi
}


#INSERT INTO REPOSITORY DATABASE
#ASSIGNING THE OUTPUT VARIABLES

assign_log()
{
if [ -s $OUTPUT_DIRECTORY/pre_stop_crs.log ]
then
        crs_output=`cat $OUTPUT_DIRECTORY/pre_stop_crs.log`
else
        crs_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_asm.log ]
then
        asm_output=`cat $OUTPUT_DIRECTORY/pre_stop_asm.log`
else
        asm_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_esm.log ]
then
        esm_agent_output=`cat $OUTPUT_DIRECTORY/pre_stop_esm.log`
else
        esm_agent_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_dma.log ]
then
        dma_agent_output=`cat $OUTPUT_DIRECTORY/pre_stop_dma.log`
else
        dma_agent_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_lsnr.log ]
then
        listener_output=`cat $OUTPUT_DIRECTORY/pre_stop_lsnr.log`
else
        listener_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_dbinst.log ]
then
        db_instance_output=`cat $OUTPUT_DIRECTORY/pre_stop_dbinst.log`
else
        db_instance_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/unidb:inst.log ]
then
        db_unique_output=`cat $OUTPUT_DIRECTORY/unidb:inst.log`
else
        db_unique_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_dbopen.log ]
then
        db_open_output=`cat $OUTPUT_DIRECTORY/pre_stop_dbopen.log`
else
        db_open_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_dbopenro.log ]
then
        db_openro_output=`cat $OUTPUT_DIRECTORY/pre_stop_dbopenro.log`
else
        db_openro_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_dbmount.log ]
then
        db_mount_output=`cat $OUTPUT_DIRECTORY/pre_stop_dbmount.log`
else
        db_mount_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_dbservices.log ]
then
        service_alignment_output=`cat $OUTPUT_DIRECTORY/pre_stop_dbservices.log`
else
        service_alignment_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_mrp.log ]
then
        mrp_output=`cat $OUTPUT_DIRECTORY/pre_stop_mrp.log`
else
        mrp_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_agent.log ]
then
        oem_agent_output=`cat $OUTPUT_DIRECTORY/pre_stop_agent.log`
else
        oem_agent_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_portutil.log ]
then
        port_utility_output=`cat $OUTPUT_DIRECTORY/pre_stop_portutil.log`
else
        port_utility_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_fgl.log ]
then
        foglight_output=`cat $OUTPUT_DIRECTORY/pre_stop_fgl.log`
else
        foglight_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_osw.log ]
then
        os_watcher_output=`cat $OUTPUT_DIRECTORY/pre_stop_osw.log`
else
        os_watcher_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/pre_stop_usb.log ]
then
        usb_output=`cat $OUTPUT_DIRECTORY/pre_stop_usb.log`
else
        usb_output="no_status"
fi

if [ -s /oracle/dba/foglight/fgl_5/bin/fglam ]
then
        fglam_output=`/oracle/dba/foglight/fgl_5/bin/fglam --check-connection`
else
        fglam_output="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/server_type.log ]
then
                instance_type=`cat $OUTPUT_DIRECTORY/server_type.log`
else
                instance_type="no_status"
fi

if [ -s $OUTPUT_DIRECTORY/version_executed_status.log ]
then
                script_version=`cat $OUTPUT_DIRECTORY/version_executed_status.log`
else
                script_version="no_status"
fi

}

insert_repdb()
{
        for what in fgl osw agent portutil lsnr mrp dbinst asm
                do
                        if [ -f $OUTPUT_DIRECTORY/stopped_$what.log ]
                        then
                                if [ "$(cat $OUTPUT_DIRECTORY/stopped_$what.log)" = yes ]
                                then
                                        eval var_$what="success"
                                fi
                                if [ "$(cat $OUTPUT_DIRECTORY/stopped_$what.log)" = no ]
                                then
                                        eval var_$what="failure"
                                fi
                        else
                                        eval var_$what="no_status"
                        fi
                done

                var_dma="na"
                var_esm="na"
                var_crs="na"

        if [[ $server_type = "rac_11g" ]] || [[ $server_type = "norac_asm11g" ]]
        then
                var_lsnr="na"
                var_asm="na"
        elif [[ $server_type = "norac_noasm" ]]
        then
                var_asm="na"
        fi



#PREPARING THE INSERT STATEMENT

if [ $(cat /etc/oratab | grep oracle | grep -v -i asm | grep -v -i crs | grep -v -i oem | grep -v "#" | awk 'BEGIN{FS=":"}{print $1}' | head -1 | wc -l) = 1 ]
then
export ORACLE_SID=`cat /etc/oratab | grep oracle | grep -v -i asm | grep -v -i crs | grep -v -i oem | grep -v "#" | awk 'BEGIN{FS=":"}{print $1}' | head -1`
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
        $ORACLE_HOME/bin/sqlplus -s OSH_SEH/dbaas_operations1@SUNDVD94 << EOFSTATUS
                set echo off;
                set head off;
                set feedback off;
insert into OSH_SEH.dbss_log (DBSS_ID,
SCRIPT_VERSION,
INSTANCE_TYPE,
STOP_OR_START,
CRS_OR_DB,
SERVER_NAME,
SERVER_TIMESTAMP,
CRS,
ASM,
LISTENER,
DB_INSTANCE,
MRP_PROCESS,
ESM_AGENT,
DMA_AGENT,
OEM_AGENT,
PORT_UTILITY,
FOGLIGHT,
OS_WATCHER,
SENT_MAIL,
CRS_OUTPUT,
ASM_OUTPUT,
LISTENER_OUTPUT,
DB_INSTANCE_OUTPUT,
DB_OPEN_OUTPUT,
DB_OPENRO_OUTPUT,
DB_MOUNT_OUTPUT,
SERVICE_ALIGNMENT_OUTPUT,
MRP_OUTPUT,
ESM_AGENT_OUTPUT,
DMA_AGENT_OUTPUT,
OEM_AGENT_OUTPUT,
PORT_UTILITY_OUTPUT,
FOGLIGHT_OUTPUT,
OS_WATCHER_OUTPUT,
USB_OUTPUT,
FGLAM_OUTPUT,
DB_UNIQUE_OUTPUT)
values (DBSS_LOG_SEQ.nextval,
'$script_version',
'$instance_type',
'stop',
'db',
'$(hostname -f)',
'$timevalue',
'$var_crs',
'$var_asm',
'$var_lsnr',
'$var_dbinst',
'$var_mrp',
'$var_esm',
'$var_dma',
'$var_agent',
'$var_portutil',
'$var_fgl',
'$var_osw',
0,
'$crs_output',
'$asm_output',
'$listener_output',
'$db_instance_output',
'$db_open_output',
'$db_openro_output',
'$db_mount_output',
'$service_alignment_output',
'$mrp_output',
'$esm_agent_output',
'$dma_agent_output',
'$oem_agent_output',
'$port_utility_output',
'$foglight_output',
'$os_watcher_output',
'$usb_output',
'$fglam_output',
'$db_unique_output');
commit;
EOFSTATUS
fi
}


# STOP THE APPROPRIATE SERVICES BASED ON SERVER TYPE

echo "";   echo "Please wait while the services are brought down... ";      echo ""

case $server_type in
        "rac_12c")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "---------------------------> THIS IS A 12c CLUSTERWARE SYSTEM <------------------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}

                why=stop;

                #Apply FGL and Autosys Blackout for defined X Hours
                fgl_Autosys_blackout

                for what in fgl osw agent mrp ogg dbfs
                do
                        when=pre; collect_$what; display_pre_post
                        stop_$what;
                        when=post; collect_$what; display_pre_post
                done

                        when=pre; what=dbinst; collect_$what; display_pre_post
                        collect_instancestatus
                        collect_dbservices
                        # Stop and disable db services before the instance for RAC
                        stop_dbservices
                        stop_db_rac;
                        when=post; what=dbinst; collect_$what; display_pre_post

                echo "      "

                echo "******************** SUMMARY ********************"
                echo ""

                for what in fgl osw agent mrp ogg dbfs dbinst
                        do
                        stopped_status
                        done

                if [ "$(cat $OUTPUT_DIRECTORY/stopped_fgl.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_osw.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_agent.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_mrp.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbinst.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_ogg.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbfs.log)" = yes ]
                then
                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                else
                        echo "0" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                fi
                assign_log
                insert_repdb
                echo "**************************************************"

                ;;

        "rac_11g")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "---------------------------> THIS IS A 11g CLUSTERWARE SYSTEM <------------------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}

                why=stop;

                #Apply FGL and Autosys Blackout for defined X Hours
                fgl_Autosys_blackout

                for what in fgl osw agent mrp ogg dbfs
                do
                        when=pre; collect_$what; display_pre_post
                        stop_$what;
                        when=post; collect_$what; display_pre_post
                done

                        when=pre; what=dbinst; collect_$what; display_pre_post
                        collect_instancestatus
                        collect_dbservices
                        stop_dbservices
                        stop_db_rac;
                        when=post; what=dbinst; collect_$what; display_pre_post

                echo "      "

                echo "******************** SUMMARY ********************"
                echo ""

                for what in fgl osw agent mrp ogg dbfs dbinst
                        do
                        stopped_status
                        done

                if [ "$(cat $OUTPUT_DIRECTORY/stopped_fgl.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_osw.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_agent.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_mrp.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbinst.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_ogg.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbfs.log)" = yes ]
                then
                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                else
                        echo "0" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                fi
                assign_log
                insert_repdb
                echo "**************************************************"

                ;;
        "rac_10g")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "---------------------------> THIS IS A 10g CLUSTERWARE SYSTEM <------------------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}

                why=stop;

                #Apply FGL and Autosys Blackout for defined X Hours
                fgl_Autosys_blackout

                for what in fgl osw agent lsnr mrp ogg
                do
                        when=pre; collect_$what; display_pre_post
                        stop_$what;
                        when=post; collect_$what; display_pre_post
                done

                        when=pre; what=dbinst; collect_$what; display_pre_post
                        collect_instancestatus
                        collect_dbservices
                        stop_db_rac;
                        when=post; what=dbinst; collect_$what; display_pre_post

                        when=pre; what=asm; collect_$what; display_pre_post
                        collect_instancestatus
                        stop_asm_10g;
                        when=post; what=asm; collect_$what; display_pre_post

                echo "      "

                echo "******************** SUMMARY ********************"
                echo ""

                for what in fgl osw agent lsnr mrp dbinst asm ogg
                        do
                        stopped_status
                        done

                if [ "$(cat $OUTPUT_DIRECTORY/stopped_fgl.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_osw.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_agent.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_lsnr.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_mrp.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbinst.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_asm.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_ogg.log)" = yes ]
                then
                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                else
                        echo "0" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                fi
                assign_log
                insert_repdb
                echo "**************************************************"

                ;;
        "norac_asm12c")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "-------------------------> THIS IS A NON-RAC SYSTEM BUT WITH ASM 12c <----------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}

                why=stop;

                #Apply FGL and Autosys Blackout for defined X Hours
                fgl_Autosys_blackout

                for what in fgl osw agent mrp ogg dbfs lsnr
                do
                        when=pre; collect_$what; display_pre_post
                        stop_$what;
                        when=post; collect_$what; display_pre_post
                done

                        when=pre; what=dbinst; collect_$what; display_pre_post
                        collect_instancestatus
                        collect_dbservices
                        stop_dbservices
                        stop_db_standalone;
                        when=post; what=dbinst; collect_$what; display_pre_post

                echo "      "

                echo "******************** SUMMARY ********************"
                echo ""
                for what in fgl osw agent mrp ogg dbfs dbinst lsnr
                do
                        stopped_status
                done

                if [ "$(cat $OUTPUT_DIRECTORY/stopped_fgl.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_osw.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_agent.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_mrp.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbinst.log)" = yes ]&& [ "$(cat $OUTPUT_DIRECTORY/stopped_ogg.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbfs.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_lsnr.log)" = yes ]
                then
                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                else
                        echo "0" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                fi
                assign_log
                insert_repdb
                echo "**************************************************"

                ;;
        "norac_asm11g")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "-------------------------> THIS IS A NON-RAC SYSTEM BUT WITH ASM 11g <----------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}

                why=stop;

                #Apply FGL and Autosys Blackout for defined X Hours
                fgl_Autosys_blackout

                for what in fgl osw agent mrp ogg dbfs lsnr
                do
                        when=pre; collect_$what; display_pre_post
                        stop_$what;
                        when=post; collect_$what; display_pre_post
                done

                        when=pre; what=dbinst; collect_$what; display_pre_post
                        collect_instancestatus
                        collect_dbservices
                        stop_db_standalone;
                        when=post; what=dbinst; collect_$what; display_pre_post

                echo "      "

                echo "******************** SUMMARY ********************"
                echo ""
                for what in fgl osw agent mrp ogg dbfs dbinst lsnr
                do
                        stopped_status
                done

                if [ "$(cat $OUTPUT_DIRECTORY/stopped_fgl.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_osw.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_agent.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_mrp.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbinst.log)" = yes ]&& [ "$(cat $OUTPUT_DIRECTORY/stopped_ogg.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbfs.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_lsnr.log)" = yes ]
                then
                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                else
                        echo "0" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                fi
                assign_log
                insert_repdb
                echo "**************************************************"

                ;;
        "norac_asm10g")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "-------------------------> THIS IS A NON-RAC SYSTEM BUT WITH ASM 10g <----------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}

                why=stop;

                #Apply FGL and Autosys Blackout for defined X Hours
                fgl_Autosys_blackout

                for what in fgl osw agent lsnr mrp ogg
                        do
                        when=pre; collect_$what; display_pre_post
                stop_$what;
                when=post; collect_$what; display_pre_post
                        done

                        when=pre; what=dbinst; collect_$what; display_pre_post
                        collect_instancestatus
                        #stop_db_rac;
                        stop_db_standalone;
                        when=post; what=dbinst; collect_$what; display_pre_post

                        when=pre; what=asm; collect_$what; display_pre_post
                        collect_instancestatus
                        stop_asm_10g;
                        when=post; what=asm; collect_$what; display_pre_post

                echo "      "

                echo "******************** SUMMARY ********************"
                echo ""

                        for what in fgl osw agent lsnr mrp dbinst asm ogg
                        do
                        stopped_status
                        done

                if [ "$(cat $OUTPUT_DIRECTORY/stopped_fgl.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_osw.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_agent.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_lsnr.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_mrp.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbinst.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_asm.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_ogg.log)" = yes ]
                then
                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                else
                        echo "0" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                fi
                assign_log
                insert_repdb
                echo "**************************************************"

                ;;
        "norac_noasm")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "--------------------------> THIS IS A NON-RAC & NON-ASM SYSTEM <----------------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}

                why=stop;

                #Apply FGL and Autosys Blackout for defined X Hours
                fgl_Autosys_blackout

                for what in fgl osw agent lsnr mrp ogg
                do
                        when=pre; collect_$what; display_pre_post
                        stop_$what;
                        when=post; collect_$what; display_pre_post
                done

                echo "" >> ${LOG}
                if [[ "$RH_VER" == 7.* ]]; then
                        when=pre; what=dbinst; collect_$what; display_pre_post
                        collect_instancestatus
                        echo "***********" >> ${LOG}
                        echo "RH7 System .. No db stop will be performed here .." >> ${LOG}
                        echo "oracle_db.service Status: " >> $LOG
                        systemctl status oracle_db.service | grep "Active: "   >> $LOG
                        echo "***********" >> ${LOG}
                else
                        when=pre; what=dbinst; collect_$what; display_pre_post
                        collect_instancestatus
                        stop_db_standalone;
                        when=post; what=dbinst; collect_$what; display_pre_post
                fi
                echo "" >> ${LOG}

                echo "      "

                echo "******************** SUMMARY ********************"
                echo ""

                for what in fgl osw agent lsnr mrp dbinst ogg
                do
                        stopped_status
                done

                if [ "$(cat $OUTPUT_DIRECTORY/stopped_fgl.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_osw.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_agent.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_lsnr.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_mrp.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_dbinst.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_ogg.log)" = yes ]
                then
                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                else
                        echo "0" > $OUTPUT_DIRECTORY/bl_stop_flag.log
                        echo ""
                        echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                fi
                assign_log
                insert_repdb
                echo "**************************************************"

                ;;
        *)
                echo "Invalid Selection : $server_type "
esac


# RENAME THE LOGS


chmod 755 $OUTPUT_DIRECTORY/pre_stop*.log 1> /dev/null 2>&1
chmod 755 $OUTPUT_DIRECTORY/post_stop*.log 1> /dev/null 2>&1
chmod 755 $OUTPUT_DIRECTORY/stopped*.log 1> /dev/null 2>&1
chmod 755 $OUTPUT_DIRECTORY/bl_stop*.log 1> /dev/null 2>&1

chown $oracred $OUTPUT_DIRECTORY/pre_stop*.log 1> /dev/null 2>&1
chown $oracred $OUTPUT_DIRECTORY/post_stop*.log 1> /dev/null 2>&1
chown $oracred $OUTPUT_DIRECTORY/stopped*.log 1> /dev/null 2>&1
chown $oracred $OUTPUT_DIRECTORY/bl_stop*.log 1> /dev/null 2>&1


filename=$(echo $(hostname) | cut -d. -f1)_$(date '+%m%d%Y_%H%M%S')
mv ${LOG} $OUTPUT_DIRECTORY/stop_$filename.log


echo ""
echo ""
echo "****************************************************************************************************";
echo "Please check the detailed STOP report at $OUTPUT_DIRECTORY/stop_$filename.log"
echo "****************************************************************************************************";
echo ""