#################################################################
#                                                               #
#                                                               #
#################################################################
#                                                               #
# Modified by EMEA OracleDBA Team for enhancements & automation #
# APRIL 2016                                                    #
#                                                               #
#################################################################
#                                                               #
#                                                               #
# USAGE : To be executed as root user                           #
#                                                               #
# Manual execution                                              #
#       sh bl_stop_crs.ksh                                      #
#                                                               #
# Via job scheduler tools and by-passing the prompt             #
#       sh bl_stop_crs.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 12c
#       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

export PATH=$PATH:/usr/local/bin
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 MYSQL DATABASE SERVER

if [ "$(ps -ef | grep -i mysql | grep -v grep | wc -l)" -ge 1 ]
then
        echo "This is a MySQL Database Server"
        exit 0
fi

# CHECK IF THE USER IS ROOT

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


# SCRIPT AND OUTPUT DIRECTORIES INITIALIZATION

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

# Check if user really wants to STOP the services
if [ $# -eq 0 ]
    then
        read -r -p "Are you sure you want to STOP the Clusterware services ? [y/n] " response
                if [ $response = y ]
                        then
                            echo "";   echo "Please wait while the services are brought down... ";      echo ""
                else
                    exit; >/dev/null
                fi
elif [ $1 == y ]
    then
                            echo "";   echo "Please wait while the services are brought down... ";      echo ""
else
                exit; >/dev/null
fi


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_dma()
{
        if [ $when = pre ]
        then
                if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
                then
                        touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        ps -ef | grep opsware | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                elif [ $(( (`date +%s` - `stat -L --format %Y $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log`) > (2880*60) )) = 1 ]
                then
                        rm -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        ps -ef | grep opsware | 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 opsware | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                fi
        elif [ $when = post ]
        then
                ps -ef | grep opsware | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}

collect_esm()
{
        if [ $when = pre ]
        then
                if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
                then
                        touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        ps -ef | grep -i esm | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                elif [ $(( (`date +%s` - `stat -L --format %Y $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log`) > (2880*60) )) = 1 ]
                then
                        rm -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        ps -ef | grep -i esm | 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 esm | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                fi
        elif [ $when = post ]
        then
                ps -ef | grep -i esm | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}

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 -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 -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 -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
   elif [ $when = post ]
        then
                ps -ef | grep lsnr |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_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 -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}'>$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 pmon | sort -k9 | 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 -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 -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}'>$OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}

collect_crs()
{
        if [ $when = pre ]
        then
                if [ ! -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
                then
                        touch $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        ps -ef | grep crs | grep -v grep | grep -v tfa | wc -l > $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 crs | grep -v grep | grep -v tfa | 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 crs | grep -v grep | grep -v tfa | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
                        fi
                fi
        elif [ $when = post ]
        then
                ps -ef | grep crs | grep -v grep |  grep -v tfa | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
        else
                sleep 0
        fi
}

#Portdaemon detection
collect_portutil()
{
       ps -ef | grep "portdaemon.pl" | grep -v grep | grep port | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
}


display_pre_post()
{
if [ $when = pre ]
     then
         if [ $what = esm ] || [ $what = dma ]
          then
               if [ "$(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log)" = 0 ] || [ ! -s $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
               then
                    echo -e ' \t ' "PRE stop" $what "running " ' \t ' ":" ' \t \t' "NO"                                | tee -a ${LOG}
               else
                    echo -e ' \t ' "PRE stop" $what "running " ' \t ' ":" ' \t \t' "YES"                               | tee -a ${LOG}
               fi

          elif [ $what = asm ] || [ $what = lsnr ] || [ $what = portutil ]
          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
          elif [ $what = crs ]
          then
               if [ "$(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log)" -lt 3 ] || [ ! -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
          fi
elif [ $when = post ]
     then
          if [ $what = asm ] || [ $what = lsnr ] || [ $what = portutil ]
          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}
               else
                    echo "POST stop" $what "running " ":" $(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log) | tee -a ${LOG}
               fi
          elif [ $what = crs ]
          then
               if [ "$(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log)" -lt 3 ] || [ ! -s $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
               then
                    echo "POST stop" $what "running " ":" "NO"                                | tee -a ${LOG}
               else
                    echo "POST stop" $what "running " ":" "YES"                               | tee -a ${LOG}
               fi
          fi
fi
}

stopped_status()
{
    if [ $what = asm ] || [ $what = lsnr ] || [ $what = portutil ]
    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
    elif [ $what = crs ]
    then
        if [ "$(cat $OUTPUT_DIRECTORY/post_"$why"_"$what".log)" -lt 3 ] || [ ! -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
}



stop_has()
{
                export ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep -v grep | grep "+ASM" | grep -v onitor | awk 'BEGIN{FS="_"}{print $NF}'`
                #ORACLE_SID=`cat /etc/oratab | awk 'BEGIN{FS=":"}{print $1}' | grep ASM | grep -v "#"`
                export ORAENV_ASK=NO                                                    >/dev/null;
                . oraenv                                                                >/dev/null
                crs_en_dis=`cat /etc/oracle/scls_scr/$(echo $(hostname) | cut -d. -f1)/root/crsstart | awk 'BEGIN{FS=":"}{print $1}'`
                echo "CRS status : $crs_en_dis"                                         >> ${LOG}; echo "" >> ${LOG}
                crsctl stop has                                                        >>  ${LOG} ; echo "" >> ${LOG}

        sleep 10
                if [ "$(crsctl check crs | grep online || crsctl check crs | grep healthy | wc -l)" -eq 0 ]
                then
                        echo "Clusterware Services stopped"               >> ${LOG}; echo "" >> ${LOG}
                        crsctl disable has                                | tee -a ${LOG}
                        crs_en_dis=`cat /etc/oracle/scls_scr/$(echo $(hostname) | cut -d. -f1)/root/crsstart | awk 'BEGIN{FS=":"}{print $1}'`
                        echo "CRS status : $crs_en_dis"                                         >> ${LOG}; echo "" >> ${LOG}
                        echo "CRS is now disabled"                                              >> ${LOG}
        else
                echo "ATTENTION : Clusterware services do not appear to have stopped properly" >>  ${LOG} ; echo "" >> ${LOG}
                echo "ATTENTION : CRS not disabled"                                     >>  ${LOG} ; echo "" >> ${LOG}
        fi
}

stop_crs()
{
               ORACLE_SID=`cat /etc/oratab | awk 'BEGIN{FS=":"}{print $1}' | grep CRS | grep -v "#"`
                export ORAENV_ASK=NO                                                    >/dev/null;
                . oraenv                                                                >/dev/null
                crsctl stop crs                                                        >>  ${LOG} ; echo "" >> ${LOG}

        sleep 10
                if [ "$(crsctl check crs | grep online || crsctl check crs | grep healthy | wc -l)" -eq 0 ]
                then
                        echo "Clusterware Services stopped"               >> ${LOG}; echo "" >> ${LOG}
                        crsctl disable crs                                             | tee -a ${LOG}
                        crs_en_dis=`cat /etc/oracle/scls_scr/$(echo $(hostname) | cut -d. -f1)/root/crsstart | awk 'BEGIN{FS=":"}{print $1}'`
                        echo "CRS status : $crs_en_dis"                                         >> ${LOG}; echo "" >> ${LOG}
                        echo "CRS is now disabled"                                              >> ${LOG}
        else
                echo "ATTENTION : Clusterware services do not appear to have stopped properly" >>  ${LOG} ; echo "" >> ${LOG}
                echo "ATTENTION : CRS not disabled"                                     >>  ${LOG} ; echo "" >> ${LOG}
        fi
}

stop_portutil()
{
# Initialise any previous Portdaemon Config files
if [[ -f $OUTPUT_DIRECTORY/Portdaemon_config.txt ]]; then
   rm $OUTPUT_DIRECTORY/Portdaemon_config.txt
fi
if [[ -f $OUTPUT_DIRECTORY/Portdaemon_stop.sh ]]; then
   rm $OUTPUT_DIRECTORY/Portdaemon_stop.sh
fi

# Capture all Runtime Portdaemon parameters for running Portdaemon processes
ps -ef | grep "portdaemon.pl" | grep -v grep | grep port | while read PORT_CONFIG
     do
           PORT_USER=`echo $PORT_CONFIG | awk '{ print $1 }'`
           #PORT_PROC=`echo $PORT_CONFIG | grep -o '..................................................portdaemon.pl' | awk '{ print $NF }'`
           PORT_PROC=`echo $PORT_CONFIG | sed -n 's/.*\(..................................................portdaemon.pl\).*/\1/p' | awk '{ print $NF }'| grep -v "\.\.\.\.portdaemon.pl"`
           PORT_DIR=`dirname $PORT_PROC`
           PORT_NUMB=`echo $PORT_CONFIG | awk '{ print $NF }'`
           echo "${PORT_USER}:${PORT_PROC}:${PORT_NUMB}" >> $OUTPUT_DIRECTORY/Portdaemon_config.txt

           # Stop the Portdaemon for this POrt ..
           echo "su - $PORT_USER -c 'sh ${PORT_DIR}/stop_port_daemon ${PORT_NUMB}'" >> $OUTPUT_DIRECTORY/Portdaemon_stop.sh
     done

# Loop through and stop all detected Portdaemon(s)
if [[ -f $OUTPUT_DIRECTORY/Portdaemon_stop.sh ]]; then
   chmod 744 $OUTPUT_DIRECTORY/Portdaemon_stop.sh
   sh $OUTPUT_DIRECTORY/Portdaemon_stop.sh > /dev/null 2>&1
fi

}

stop_tfa()
{
if [[ "$(ps -ef | grep tfa_home | grep -v grep |  wc -l)" -ne 0 ]]; then
   if [[ -f /etc/init.d/init.tfa ]]; then
      /etc/init.d/init.tfa stop > /dev/null 2>&1
   fi
fi
}

disable_standalone_autostart()
{
DDMMYY=`date '+%m%d%y_%H:%M:%S'`
if [[ -f /etc/oratab ]]; then
   oracred=`ls -l /etc/oratab | awk '{ print $3":"$4 }'`
   if [[ -f /etc/oratab_bl_backup ]]; then
      rm -rf /etc/oratab_bl_backup
   fi
   cp -p /etc/oratab $OUTPUT_DIRECTORY/oratab_${DDMMYY}
   mv /etc/oratab /etc/oratab_bl_backup
   if [[ -f /etc/oratab_bl_backup ]]; then
      sed '/^$/d' /etc/oratab_bl_backup | grep -v "^#" | sed 's/:Y$/:N/g' | sed 's/:M$/:N/g' | sed 's/:Y:/:N:/g' | sed 's/:A$/:N/g' > /etc/oratab
      if [[ -f /etc/oratab ]]; then
         chown $oracred /etc/oratab
      fi
   fi
fi

if [[ -f /etc/lsnrtab ]]; then
   oracred=`ls -l /etc/lsnrtab | awk '{ print $3":"$4 }'`
   if [[ -f /etc/lsnrtab_bl_backup ]]; then
     rm -rf /etc/lsnrtab_bl_backup
   fi
   cp -p /etc/lsnrtab $OUTPUT_DIRECTORY/lsnrtab_${DDMMYY}
   mv /etc/lsnrtab /etc/lsnrtab_bl_backup
   if [[ -f /etc/lsnrtab_bl_backup ]]; then
      sed '/^$/d' /etc/lsnrtab_bl_backup | grep -v "^#" | sed 's/:Y$/:N/g' > /etc/lsnrtab
      if [[ -f /etc/lsnrtab ]]; then
         chown $oracred /etc/lsnrtab
      fi
   fi
fi
}

stop_rh7_dbserv()
{
if [[ -f /etc/redhat-release ]] && [[ "$RH_VER" == 7.* ]]; then
   export RH7_ORASTR=`systemctl status oracle_db.service | grep "Active: " | awk '{ print $1,$2,$3 }'`
   #echo " RH_VER => $RH_VER :: RH7_ORASTR => $RH7_ORASTR "
   echo "" >> $LOG
   echo "########################################################################" >> $LOG
   echo "`date`" >> $LOG
   echo "Note: RedHat7 Oracle Service: oracle_db.service will be stopped here ... " >> $LOG
   echo "" >> $LOG
   echo "PRE Shutdown Status: " >> $LOG
   systemctl status oracle_db.service | grep "Active: "   >> $LOG
   echo "" >> $LOG
   echo "    => Attempting to Stop it ..." >> $LOG
   echo "" >> $LOG
   systemctl stop oracle_db.service >> $LOG
   sleep 3
   echo "POST Shutdown Status: " >> $LOG
   systemctl status oracle_db.service | grep "Active: "   >> $LOG
   echo "########################################################################" >> $LOG
   echo "" >> $LOG
fi
}


# Remove the temporary log file
        rm -f $OUTPUT_DIRECTORY/stop_crs.log

# Assign a new log file
        LOG=$OUTPUT_DIRECTORY/stop_crs.log;
#INSERT INTO REPOSITORY DATABASE
#ASSIGNING THE OUTPUT VARIABLES

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

if [ -s $OUTPUT_DIRECTORY/post_start_usb.log ]
then
        usb_output=`cat $OUTPUT_DIRECTORY/post_start_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 dma esm lsnr asm crs
                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


        if [[ $server_type = "rac_10g" ]]
        then
                var_lsnr="na"
                var_asm="na"
        elif [[ $server_type = "norac_asm10" ]] || [[ $server_type = "norac_noasm" ]]
        then
                var_dma="na"
                var_esm="na"
                var_lsnr="na"
                var_asm="na"
                var_crs="na"

        fi

        for what in fgl osw agent portutil mrp dbinst
                do
                    eval var_$what="na"
                done




#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)
values (DBSS_LOG_SEQ.nextval,
'$script_version',
'$instance_type',
'stop',
'crs',
'$(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');
commit;
EOFSTATUS
fi

}
                                  echo "" >> ${LOG};

echo "************************* START TIME : $(date) ************************"  >> ${LOG}; echo "" >> ${LOG}

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

                #if [ "$(ps -ef | grep pmon | grep -v grep |grep -v "+ASM" | grep -v onitor |  wc -l)" -eq 0 ]
                #then
                        why=stop;
                        for what in dma esm lsnr asm crs portutil
                        do
                                when=pre; collect_$what; display_pre_post
                        done

                        echo "";
                        echo "Stopping High Availability Services"; echo "";
                        stop_has

                        echo ""
                        echo "Stopping Any Portdaemon Services if any "; echo "";
                        stop_portutil

                        echo " "


                     if [[ $OS != "AIX" ]]; then
                        echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                        for i in {1..2}
                        do
                                echo -n -e '  '
                                for i in {1..60}
                                do
                                        echo -n ".";sleep 1;
                                done
                                echo "";
                        done
                     else
                        echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                                i=1
                                while [[ $i -le 60 ]] ; do
                                      echo ".\c";sleep 1;
                                      i=$(expr $i + 1)
                                done

                                echo ""

                                i=1
                                while [[ $i -le 60 ]] ; do
                                echo ".\c";sleep 1;
                                i=$(expr $i + 1)
                                done
                     fi

                        for what in lsnr asm crs portutil
                        do
                                when=post; collect_$what; display_pre_post
                        done

                        # Stop TFA if running
                        stop_tfa

                        echo "      "

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

                        for what in lsnr asm crs portutil
                        do
                                stopped_status
                        done

                        echo ""

                        if [ "$(cat $OUTPUT_DIRECTORY/stopped_lsnr.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_asm.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_crs.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_portutil.log)" = yes ]
                        then
                                echo "1" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                echo ""
                                echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                                echo ""
                        else
                                echo "0" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                echo ""
                                echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                                echo ""
                        fi
                        assign_log
                        insert_repdb
                #else
                #echo "";
                        #echo "ATTENTION : Please stop Oracle instances using 'stop.ksh' and then rerun this script"         ;echo ""
                #fi
                echo "**************************************************"

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

                #if [ "$(ps -ef | grep pmon | grep -v grep |grep -v "+ASM" | grep -v onitor |  wc -l)" -eq 0 ]
                #then
                        why=stop;
                        for what in dma esm lsnr asm crs portutil
                        do
                                when=pre; collect_$what; display_pre_post
                        done

                        echo "";
                        echo "Stopping High Availability Services"; echo "";
                        stop_has

                        echo ""

                        echo "";
                        echo "Stopping Any Portdaemon Services if any "; echo "";
                        stop_portutil

                        echo " "


                     if [[ $OS != "AIX" ]]; then
                        echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                        for i in {1..2}
                        do
                                echo -n -e '  '
                                for i in {1..60}
                                do
                                        echo -n ".";sleep 1;
                                done
                                echo "";
                        done
                     else
                        echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                                i=1
                                while [[ $i -le 60 ]] ; do
                                      echo ".\c";sleep 1;
                                      i=$(expr $i + 1)
                                done

                                echo ""

                                i=1
                                while [[ $i -le 60 ]] ; do
                                echo ".\c";sleep 1;
                                i=$(expr $i + 1)
                                done
                     fi

                        for what in lsnr asm crs portutil
                        do
                                when=post; collect_$what; display_pre_post
                        done

                        # Stop TFA if running
                        stop_tfa

                        echo "      "

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

                        for what in lsnr asm crs portutil
                        do
                                stopped_status
                        done

                        echo ""

                        if [ "$(cat $OUTPUT_DIRECTORY/stopped_lsnr.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_asm.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_crs.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_portutil.log)" = yes ]
                        then
                                echo "1" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                echo ""
                                echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                                echo ""
                        else
                                echo "0" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                echo ""
                                echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                                echo ""
                        fi
                        assign_log
                        insert_repdb
                #else
                #echo "";
                        #echo "ATTENTION : Please stop Oracle instances using 'stop.ksh' and then rerun this script"         ;echo ""
                #fi
                echo "**************************************************"

                ;;
        "rac_10g")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "---------------------------> THIS IS A 10g CLUSTERWARE SYSTEM <------------------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}
                #if [ "$(ps -ef | grep pmon | grep -v grep |grep -v "+ASM" | grep -v onitor |  wc -l)" -eq 0 ]
                #then
                        why=stop;
                        for what in dma esm crs portutil
                        do
                                when=pre; collect_$what; display_pre_post
                        done

                        echo "";
                        echo "Stopping CRS"; echo "";
                        stop_crs

                        echo ""
                        echo "";
                        echo "Stopping Any Portdaemon Services if any "; echo "";
                        stop_portutil

                     if [[ $OS != "AIX" ]]; then
                        echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                        for i in {1..2}
                        do
                                echo -n -e '  '
                                for i in {1..60}
                                do
                                        echo -n ".";sleep 1;
                                done
                                echo "";
                        done
                     else
                        echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                                i=1
                                while [[ $i -le 60 ]] ; do
                                      echo ".\c";sleep 1;
                                      i=$(expr $i + 1)
                                done

                                echo ""

                                i=1
                                while [[ $i -le 60 ]] ; do
                                echo ".\c";sleep 1;
                                i=$(expr $i + 1)
                                done
                     fi

                        for what in crs portutil
                        do
                                when=post; collect_$what; display_pre_post
                        done

                        echo "      "

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

                        for what in crs portutil
                        do
                                stopped_status
                        done

                        echo ""

                        if [ "$(cat $OUTPUT_DIRECTORY/stopped_crs.log)" = yes ]
                        then
                                echo "1" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                echo ""
                                echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                                echo ""
                        else
                                echo "0" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                echo ""
                                echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                                echo ""
                        fi
                        assign_log
                        insert_repdb
                #else
                #echo "";
                        #echo "ATTENTION : Please stop Oracle instances using 'stop.ksh' and then rerun this script"         ;echo ""
                #fi
                echo "**************************************************"
                ;;
        "norac_asm12c")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "-------------------------> THIS IS A NON-RAC SYSTEM BUT WITH ASM 12c <----------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}
                #if [ "$(ps -ef | grep pmon | grep -v grep |grep -v "+ASM" | grep -v onitor |  wc -l)" -eq 0 ]
                #then
                        if [ "$(cat /etc/oratab | grep "^+ASM:" | wc -l)" -eq 1 ]
                        then
                                why=stop;
                                for what in dma esm lsnr asm crs portutil
                                do
                                        when=pre; collect_$what; display_pre_post
                                done

                                echo "";
                                echo "Stopping High Availability Services"; echo "";
                                stop_has

                                echo " "
                                echo "";
                                echo "Stopping Any Portdaemon Services if any "; echo "";
                                stop_portutil


                             if [[ $OS != "AIX" ]]; then
                                echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                                for i in {1..2}
                                do
                                        echo -n -e '  '
                                        for i in {1..60}
                                        do
                                                echo -n ".";sleep 1;
                                        done
                                        echo "";
                                done
                             else
                                echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                                i=1
                                while [[ $i -le 60 ]] ; do
                                      echo ".\c";sleep 1;
                                      i=$(expr $i + 1)
                                done

                                echo ""

                                i=1
                                while [[ $i -le 60 ]] ; do
                                echo ".\c";sleep 1;
                                i=$(expr $i + 1)
                                done
                             fi

                                for what in lsnr asm crs portutil
                                do
                                        when=post; collect_$what; display_pre_post
                                done

                                # Stop TFA if running
                                stop_tfa

                                echo "      "

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

                                for what in lsnr asm crs portutil
                                do
                                        stopped_status
                                done

                                echo ""

                                if [ "$(cat $OUTPUT_DIRECTORY/stopped_crs.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_asm.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_lsnr.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_portutil.log)" = yes ]
                                then
                                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                        echo ""
                                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                                        echo ""
                                else
                                        echo "0" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                        echo ""
                                        echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                                        echo ""
                                fi
                        else
                                echo "";
                                echo " No +ASM entry found in /etc/oratab -> This script has no relevance to this server"               >> ${LOG}; echo "" >> ${LOG}
                        fi
                        assign_log
                        insert_repdb
                #else
                        #echo "ATTENTION : Please stop Oracle instances using 'stop.ksh' and then rerun this script"        >> ${LOG}; echo "" >> ${LOG}
                #fi
                echo "**************************************************"
                ;;
        "norac_asm11g")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "-------------------------> THIS IS A NON-RAC SYSTEM BUT WITH ASM 11g <----------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}
                #if [ "$(ps -ef | grep pmon | grep -v grep |grep -v "+ASM" | grep -v onitor |  wc -l)" -eq 0 ]
                #then
                        if [ "$(cat /etc/oratab | grep "^+ASM:" | wc -l)" -eq 1 ]
                        then
                                why=stop;
                                for what in dma esm lsnr asm crs portutil
                                do
                                        when=pre; collect_$what; display_pre_post
                                done

                                echo "";
                                echo "Stopping High Availability Services"; echo "";
                                stop_has

                                echo " "
                                echo "";
                                echo "Stopping Any Portdaemon Services if any "; echo "";
                                stop_portutil



                             if [[ $OS != "AIX" ]]; then
                                echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                                for i in {1..2}
                                do
                                        echo -n -e
                                        for i in {1..60}
                                        do
                                                echo -n ".";sleep 1;
                                        done
                                        echo "";
                                done
                             else
                                echo "Waiting for 2 minutes to allow all the CRS processes to go down"
                                i=1
                                while [[ $i -le 60 ]] ; do
                                      echo ".\c";sleep 1;
                                      i=$(expr $i + 1)
                                done

                                echo ""

                                i=1
                                while [[ $i -le 60 ]] ; do
                                echo ".\c";sleep 1;
                                i=$(expr $i + 1)
                                done
                             fi

                                for what in lsnr asm crs portutil
                                do
                                        when=post; collect_$what; display_pre_post
                                done

                                # Stop TFA if running
                                stop_tfa

                                echo "      "

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

                                for what in lsnr asm crs portutil
                                do
                                        stopped_status
                                done

                                echo ""

                                if [ "$(cat $OUTPUT_DIRECTORY/stopped_crs.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_asm.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_lsnr.log)" = yes ] && [ "$(cat $OUTPUT_DIRECTORY/stopped_portutil.log)" = yes ]
                                then
                                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                        echo ""
                                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                                        echo ""
                                else
                                        echo "0" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                                        echo ""
                                        echo "ATTENTION : Some services didn't stop. Please check"    | tee -a ${LOG}
                                        echo ""
                                fi
                        else
                                echo "";
                                echo " No +ASM entry found in /etc/oratab -> This script has no relevance to this server"               >> ${LOG}; echo "" >> ${LOG}
                        fi
                        assign_log
                        insert_repdb
                #else
                        #echo "ATTENTION : Please stop Oracle instances using 'stop.ksh' and then rerun this script"        >> ${LOG}; echo "" >> ${LOG}
                #fi
                echo "**************************************************"
                ;;
        "norac_asm10g")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "--------------------------> THIS IS A NON-RAC SYSTEM BUT WITH ASM 10g <---------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}
        echo " This script has no relevance to this server"               | tee -a ${LOG}; echo "" | tee -a ${LOG}
                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                        echo ""
                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                        echo ""
                        assign_log
                        insert_repdb
                ;;
        "norac_noasm")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "--------------------------> THIS IS A NON-RAC & NON-ASM SYSTEM <----------------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}
        echo " This script will only check/stop any Port daemons that may be running and if RH7 system -> stop db service, disable db/lsnr Autostart - otherwise it has no relevance to this server "               | tee -a ${LOG}; echo "" | tee -a ${LOG}

                        why=stop;
                        for what in portutil
                        do
                           when=pre; collect_$what; display_pre_post
                        done

                        echo " "
                        echo "";
                        echo "Stopping Any Portdaemon Services if any "; echo "";
                        stop_portutil

                        for what in portutil
                        do
                            when=post; collect_$what; display_pre_post
                        done

                        if [[ "$RH_VER" == 7.* ]]; then
                           echo "";
                           echo "RedHat7 ... Stopping RH7 db Service "
                           stop_rh7_dbserv
                        fi

                        echo "";
                        echo "Disabling Auto-start for this standalone host : "; echo "";
                        disable_standalone_autostart
                        echo "New content of ORATAB on this host "; echo "";
                        if [[ -f /etc/oratab ]]; then
                           echo "`cat /etc/oratab `"
                        fi
                        echo "";
                        echo "New content of LSNRTAB on this host : "; echo "";
                        if [[ -f /etc/lsnrtab ]]; then
                           echo "`cat /etc/lsnrtab `"
                        fi
                        echo "";

                        echo "1" > $OUTPUT_DIRECTORY/bl_stop_crs_flag.log
                        echo ""
                        echo "SUCCESS : Ready to move to next step"                   | tee -a ${LOG}
                        echo ""
                        assign_log
                        insert_repdb
                ;;
        *)
                echo "Invalid Selection"
esac

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

#chown oracle:dba $OUTPUT_DIRECTORY/pre_stop*.log 1> /dev/null 2>&1
#chown oracle:dba $OUTPUT_DIRECTORY/post_stop*.log 1> /dev/null 2>&1
#chown oracle:dba $OUTPUT_DIRECTORY/stopped*.log 1> /dev/null 2>&1
#chown oracle:dba $OUTPUT_DIRECTORY/stop_crs*.log 1> /dev/null 2>&1
#chown oracle:dba $OUTPUT_DIRECTORY/bl_stop*.log 1> /dev/null 2>&1

if ls $OUTPUT_DIRECTORY/Portdaemon* 1> /dev/null 2>&1; then
   chmod 777 $OUTPUT_DIRECTORY/Portdaemon*
   #chown oracle:dba $OUTPUT_DIRECTORY/Portdaemon*
fi

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

echo ""
echo ""


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

echo ""
echo " INFO: Remaining "ora" processes on this host are ... "
echo "`ps -ef | grep ora | grep -v grep | grep -v "^root " | grep -v pbrun | grep -v "ksh"`"
echo ""


Ready for Action?

LET'S GO!
Copyright 2024 IT Remote dot com
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram