#################################################################
# Modified by EMEA OracleDBA Team for enhancements & automation #
# DEC 2016                                                      #
#################################################################
#                                                               #
# USAGE : To be executed as oracle user                         #
#         (pbrun to oracle or pboracle)                         #
#                                                               #
# Manual execution                                              #
#       sh bl_start.ksh                                         #
#                                                               #
# Via job scheduler tools and by-passing the prompt             #
#       sh bl_start.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

# Check if user really wants to START the services

# 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

if [ $# -eq 0 ]
    then
        read -r -p "Are you sure you want to START the Oracle services ? [y/n] " response
                if [ $response = y ]
                        then
                            echo "";   echo "Please wait while the services are started up... ";      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 started up... ";      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

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/start.log

# Assign a new log file
        LOG=$OUTPUT_DIRECTORY/start.log;                                      echo "" >> ${LOG};



# Check the server type

export server_type=$(cat $OUTPUT_DIRECTORY/server_type.log)



# Collecting service runnning information

collect_lsnr()
{
#ps -ef | grep lsnr | grep "^.*${oraowner} " | grep -v grep | grep -oP '(?<=tnslsnr )\w+' >$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
}

collect_mrp()
{
        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
}

collect_dbinst()
{
        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
}

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 | grep -i "is running" | awk '{ print $2 }') >> $OUTPUT_DIRECTORY/"$when"_"$why"_dbservices.log
        done < $OUTPUT_DIRECTORY/db:inst.log
}

collect_asm()
{
        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
}

collect_agent()
{
        sleep 5
        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 status agent | grep "Agent is Running and Ready" | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
           fi
        fi
}

collect_fgl()
{
        sleep 10
        ps -ef | grep -i foglight | grep "^.*${oraowner} " | grep -v grep | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log
}

collect_portutil()
{
        ps -ef | grep "portdaemon.pl" | grep -v grep | grep port | wc -l > $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".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
}

started_status()
{
        rm -f $OUTPUT_DIRECTORY/diff_file;         rm -f $OUTPUT_DIRECTORY/diff_1;         rm -f $OUTPUT_DIRECTORY/diff_2
        if [[ -f $OUTPUT_DIRECTORY/pre_stop_$what.log ]]; then
        cat $OUTPUT_DIRECTORY/pre_stop_$what.log | sort > $OUTPUT_DIRECTORY/diff_1;
        fi
        if [[ -f $OUTPUT_DIRECTORY/post_start_$what.log ]]; then
        cat  $OUTPUT_DIRECTORY/post_start_$what.log | sort > $OUTPUT_DIRECTORY/diff_2;
        fi
        if [[ -f $OUTPUT_DIRECTORY/diff_2 ]]; then
        diff -b -i -w $OUTPUT_DIRECTORY/diff_1 $OUTPUT_DIRECTORY/diff_2 > $OUTPUT_DIRECTORY/diff_file
        chown $oracred $OUTPUT_DIRECTORY/diff_file
        chmod 755 $OUTPUT_DIRECTORY/diff_file
        fi
        if [ ! -s $OUTPUT_DIRECTORY/diff_file ]
        then
                echo "yes" > $OUTPUT_DIRECTORY/started_$what.log
                echo "OK : pre-post status matches for $what"
        else
                if [[ $what == "fgl" ]]; then
                   if [[ -f $OUTPUT_DIRECTORY/post_start_$what.log ]]; then
                      if [[ $(cat $OUTPUT_DIRECTORY/post_start_$what.log) -gt 0 ]]; then
                         echo "yes" > $OUTPUT_DIRECTORY/started_$what.log
                         echo "OK : pre-post status matches for $what"
                      else
                         echo "no" > $OUTPUT_DIRECTORY/started_$what.log
                         echo "ATTENTION : $what didn't start. Please check"
                      fi
                   fi
                else
                   echo "no" > $OUTPUT_DIRECTORY/started_$what.log
                   echo "ATTENTION : $what didn't start. Please check"
                fi
        fi
}

service_alignment()
{
        rm -f $OUTPUT_DIRECTORY/diff_file;         rm -f $OUTPUT_DIRECTORY/diff_1;         rm -f $OUTPUT_DIRECTORY/diff_2
        cat  $OUTPUT_DIRECTORY/pre_stop_dbservices.log   | sort > $OUTPUT_DIRECTORY/diff_1;
        cat  $OUTPUT_DIRECTORY/post_start_dbservices.log | sort > $OUTPUT_DIRECTORY/diff_2;
        diff -b -i -w $OUTPUT_DIRECTORY/diff_1 $OUTPUT_DIRECTORY/diff_2 > $OUTPUT_DIRECTORY/diff_file
        chown $oracred $OUTPUT_DIRECTORY/diff_file
        chmod 755 $OUTPUT_DIRECTORY/diff_file

        if [ ! -s $OUTPUT_DIRECTORY/diff_file ]
        then
                echo ""  >> ${LOG}
                echo "***************************** DB SERVICES ************************"     >> ${LOG}
                echo "" >> ${LOG}
                echo "OK : pre-post status matches for DB service alignment" >> ${LOG}
                echo "" >> ${LOG}
                echo ""
                echo "OK : pre-post status matches for DB service alignment"
                echo ""
        else
                echo ""
                #echo "ATTENTION : Please verify DB service alignment"
                #echo "pre-stop status (db:instance:service) is available at $OUTPUT_DIRECTORY/pre_stop_dbservices.log"
                #echo ""
                echo "Attempting to Fix DB service alignment mismatch on this node"
                echo ""
                echo "***************************** DB SERVICES ************************"     >> ${LOG}
                echo "" >> ${LOG}
                echo "Attempting to Fix DB service alignment mismatch on this node" >> ${LOG}
                echo "" >> ${LOG}
                # Try to fix the mis-alignment of services on this node per database (ORACLE_DB)
                cat $OUTPUT_DIRECTORY/diff_file | grep "^<" |awk '{ print $2 }' | cut -d":" -f1 | sort -u | while read ORACLE_DB
                do
                  export ORACLE_DB              >/dev/null
                  export ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep "^.*${oraowner} " | grep -v grep | grep $ORACLE_DB | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s | tail -1`             >/dev/null
                  export ORAENV_ASK=NO                                                    >/dev/null
                  if [[ ! -z $ORACLE_SID ]]; then
                     . oraenv                                                                >/dev/null
                  fi
                  export LOCAL_NODE=`hostname -s`

                  if [[ "$server_type" == rac* ]]; then

                     srvctl status service -d $ORACLE_DB | grep $ORACLE_SID | grep -i "is running" | awk '{ print $2 }' | while read SERV_NAME
                     do
                        echo "srvctl stop service -d $ORACLE_DB -s $SERV_NAME -n $LOCAL_NODE" >> ${LOG}
                        srvctl stop service -d $ORACLE_DB -s $SERV_NAME -n $LOCAL_NODE >> ${LOG}
                     done
                     # Read pre_stop_dbservices.log and start only those services
                     cat $OUTPUT_DIRECTORY/pre_stop_dbservices.log  | grep $ORACLE_DB | while read SERV_LINE
                     do
                         DB_SERV_LIST=`echo $SERV_LINE | cut -d":" -f3 -s`
                         for DB_SERV_NAME in $DB_SERV_LIST
                         do
                           echo "srvctl enable service -d $ORACLE_DB -s $DB_SERV_NAME" >> ${LOG}
                           srvctl enable service -d $ORACLE_DB -s $DB_SERV_NAME  >> ${LOG}

                           #Check if the database config is SINGLE_NODE even though this is a RAC cluster!
                           RAC_SINGLE=`${ORACLE_HOME}/bin/srvctl config database -d $ORACLE_DB | grep "^Type:" | awk '{print $2}' | grep -i "single"`
                           if [[ -z $RAC_SINGLE ]]; then
                              echo "srvctl start service -d $ORACLE_DB -s $DB_SERV_NAME -n $LOCAL_NODE" >> ${LOG}
                              srvctl start service -d $ORACLE_DB -s $DB_SERV_NAME -n $LOCAL_NODE >> ${LOG}
                           else
                              echo "srvctl start service -d $ORACLE_DB -s $DB_SERV_NAME" >> ${LOG}
                              srvctl start service -d $ORACLE_DB -s $DB_SERV_NAME >> ${LOG}
                           fi
                         done
                     done
                  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
                     # Read pre_stop_dbservices.log and start only those services
                     cat $OUTPUT_DIRECTORY/pre_stop_dbservices.log  | grep $ORACLE_DB | while read SERV_LINE
                     do
                         DB_SERV_LIST=`echo $SERV_LINE | cut -d":" -f3 -s`
                         for DB_SERV_NAME in $DB_SERV_LIST
                         do
                           echo "srvctl start service -d $ORACLE_DB -s $DB_SERV_NAME" >> ${LOG}
                           srvctl start service -d $ORACLE_DB -s $DB_SERV_NAME >> ${LOG}
                         done
                     done

                  fi

                  echo "" >> ${LOG}
                done

                # Do another round of service checks and report accordingly
                collect_dbservices
                service_alignment_basic
        fi
}

service_alignment_basic()
{
        rm -f $OUTPUT_DIRECTORY/diff_file;         rm -f $OUTPUT_DIRECTORY/diff_1;         rm -f $OUTPUT_DIRECTORY/diff_2
        cat  $OUTPUT_DIRECTORY/pre_stop_dbservices.log   | sort > $OUTPUT_DIRECTORY/diff_1;
        cat  $OUTPUT_DIRECTORY/post_start_dbservices.log | sort > $OUTPUT_DIRECTORY/diff_2;
        diff -b -i -w $OUTPUT_DIRECTORY/diff_1 $OUTPUT_DIRECTORY/diff_2 > $OUTPUT_DIRECTORY/diff_file
        chown $oracred $OUTPUT_DIRECTORY/diff_file
        chmod 755 $OUTPUT_DIRECTORY/diff_file

        if [ ! -s $OUTPUT_DIRECTORY/diff_file ]
        then
                echo ""
                echo "OK : pre-post status matches for DB service alignment"
                echo ""
                echo "" >> ${LOG}
                echo "OK : pre-post status matches for DB service alignment" >> ${LOG}
                echo "" >> ${LOG}
        else
                echo ""
                echo "ATTENTION : Please verify DB service alignment"
                echo "pre-stop status (db:instance:service) is available at $OUTPUT_DIRECTORY/pre_stop_dbservices.log"
                echo ""
                echo "" >> ${LOG}
                echo "DB_SERVICE_MISMATCH: Please verify DB service alignment" >> ${LOG}
                echo "pre-stop status (db:instance:service) is available at $OUTPUT_DIRECTORY/pre_stop_dbservices.log" >> ${LOG}
                echo "" >> ${LOG}
        fi
}

display_pre_post()
{
     if [ $why = stop ]
     then
          if [ $when = pre ]
          then
               if [ $what = fgl ] || [ $what = agent ] || [ $what = mrp ] || [ $what = dbfs ] || [ $what = ogg ]
               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
          fi
     elif [ $why = start ]
     then
          if [ $what = fgl ] || [ $what = agent ] || [ $what = mrp ] || [ $what = dbfs ] || [ $what = ogg ]
          then
               if [[ -f $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]]; then
               if [ "$(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log)" = 0 ] || [ ! -s $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log ]
               then
                    echo "POST start" $what "running " ":" "NO" | tee -a ${LOG}
                    echo ""                    | tee -a ${LOG}
               else
                    echo "POST start" $what "running " ":" "YES"        | tee -a ${LOG}
                    echo ""                    | tee -a ${LOG}
               fi
               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 start" $what "running " ":" "NO" | tee -a ${LOG}
                    echo ""                    | tee -a ${LOG}
               else
                    echo "POST start" $what "running " ":" $(cat $OUTPUT_DIRECTORY/"$when"_"$why"_"$what".log) | tee -a ${LOG}
                    echo ""                    | tee -a ${LOG}
               fi
          fi
     fi
}

# Code for starting the services

start_asm()
{
echo "***************************** ASM ************************"     >> ${LOG}; echo "" >> ${LOG}

if [[ -f $OUTPUT_DIRECTORY/pre_stop_asm.log ]]; then
while read data
do
        export ORACLE_SID=$data                                               >/dev/null
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
                echo "Checking for instance ${ORACLE_SID} "
                if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep $ORACLE_SID | wc -l) -eq 0 ]
                then
                        echo "Starting instance ${ORACLE_SID} "
                        op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysasm' << EOF
                        startup;
                        set echo off;
                        set linesize 200 pages 100;
                        select instance_name,host_name,startup_time from gv\\$instance;
EOF`
        echo "$op"                                      >> ${LOG}; echo "" >> ${LOG}
                else
                        echo "ASM Instance ${ORACLE_SID} is already running "
                        echo "ASM Instance: $ORACLE_SID is already running .. moving on to databases ... " >> ${LOG}; echo "" >> ${LOG}
                fi
        echo "_____________________________________________________________________"   >> ${LOG};echo "" >> ${LOG}
done < $OUTPUT_DIRECTORY/pre_stop_asm.log
fi
}

start_asm_10g()
{
echo "***************************** ASM ************************"     >> ${LOG}; echo "" >> ${LOG}

while read data
do
        export ORACLE_SID=$data                                               >/dev/null
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
                echo "Starting instance ${ORACLE_SID} "
                if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep $ORACLE_SID | wc -l) -eq 0 ]
                then
                        op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' << EOF
                        startup;
                        set echo off;
                        set linesize 200 pages 100;
                        select instance_name,host_name,startup_time from gv\\$instance;
EOF`
        echo "$op"                                      >> ${LOG}; echo "" >> ${LOG}
                fi
        echo "_____________________________________________________________________"   >> ${LOG};echo "" >> ${LOG}
done < $OUTPUT_DIRECTORY/pre_stop_asm.log
}

start_db_rac()
{
echo "************************** DATABASE & SERVICES *********************"     >> ${LOG}; echo "" >> ${LOG}

# Start instance in OPEN mode

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
                echo "Starting instance ${ORACLE_SID} "
                if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep "pmon_${ORACLE_SID}\$" | wc -l) -eq 0 ]
                then
                        if [ $ORACLE_DB = $ORACLE_SID ]
                        then
                                srvctl start database -d $ORACLE_DB                         >> ${LOG};echo "" >> ${LOG}
                        elif [[ ! -z $RACONE_DB ]]
                        then
                                srvctl start database -d $ORACLE_DB  -n $RACONE_DB          >> ${LOG};echo "" >> ${LOG}
                        else
                                srvctl start instance -d $ORACLE_DB -i $ORACLE_SID          >> ${LOG};echo "" >> ${LOG}
                        fi

                        op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' << EOF
                        set linesize 200 pages 100;
                        set echo off;
                        select instance_name,host_name,startup_time from gv\\$instance;
                        select name,open_mode,database_role from gv\\$database;
EOF`
        echo "$op"                                              >> ${LOG}; echo "" >> ${LOG}
                fi
        srvctl status service -d $ORACLE_DB                       >> ${LOG}
        echo "___________________________________________________________">> ${LOG};
done < $OUTPUT_DIRECTORY/pre_stop_dbopen.log

# Start instance in READ ONLY mode (should be defined as such in srvctl config)

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 "Starting instance ${ORACLE_SID} "
                if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep "pmon_${ORACLE_SID}\$" | wc -l) -eq 0 ]
                then
                        if [ $ORACLE_DB = $ORACLE_SID ]
                        then
                                srvctl start database -d $ORACLE_DB                          >> ${LOG};echo "" >> ${LOG}
                        elif [[ ! -z $RACONE_DB ]]
                        then
                                srvctl start database -d $ORACLE_DB  -n $RACONE_DB          >> ${LOG};echo "" >> ${LOG}
                        else
                                srvctl start instance -d $ORACLE_DB -i $ORACLE_SID          >> ${LOG};echo "" >> ${LOG}
                        fi

                        op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' << EOF
                        set linesize 200 pages 100;
                        set echo off;
                        select instance_name,host_name,startup_time from gv\\$instance;
                        select name,open_mode,database_role from gv\\$database;
EOF`
        echo "$op"                                              >> ${LOG}; echo "" >> ${LOG}
                fi
        srvctl status service -d $ORACLE_DB                       >> ${LOG}
        echo "___________________________________________________________">> ${LOG};
done < $OUTPUT_DIRECTORY/pre_stop_dbopenronly.log


# Start instance in MOUNT mode

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 "Mounting instance ${ORACLE_SID} "
                if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep "pmon_${ORACLE_SID}\$" | wc -l) -eq 0 ]
                then
                        if [ $ORACLE_DB = $ORACLE_SID ]
                        then
                                srvctl start database -d $ORACLE_DB -o mount                     >> ${LOG};echo "" >> ${LOG}
                        elif [[ ! -z $RACONE_DB ]]
                        then
                                srvctl start database -d $ORACLE_DB  -n $RACONE_DB  -o mount        >> ${LOG};echo "" >> ${LOG}
                        else
                                srvctl start instance -d $ORACLE_DB -i $ORACLE_SID -o mount        >> ${LOG};echo "" >> ${LOG}
                        fi
                        op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' << EOF
                        set linesize 200 pages 100;
                        set echo off;
                        select instance_name,host_name,startup_time from gv\\$instance;
                        select name,open_mode,database_role from gv\\$database;
EOF`
        echo "$op"                                              >> ${LOG}; echo "" >> ${LOG}
                fi
        echo "___________________________________________________________">> ${LOG};
done < $OUTPUT_DIRECTORY/pre_stop_dbmount.log
}

start_db_standalone()
{
echo "************************** DATABASE & SERVICES *********************"     >> ${LOG}; echo "" >> ${LOG}

# Start instance in OPEN mode

while read data
do
        export ORACLE_SID=$(echo $data | awk 'BEGIN{FS=":"}{print $2}')       >/dev/null
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
                echo "Starting instance ${ORACLE_SID} "
                if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep "pmon_${ORACLE_SID}\$" | wc -l) -eq 0 ]
                then
                        op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' << EOF
                        startup;
                        set linesize 200 pages 100;
                        set echo off;
                        select instance_name,host_name,startup_time from v\\$instance;
                        select name,open_mode,database_role from v\\$database;
EOF`
        echo "$op"                                              >> ${LOG}; echo "" >> ${LOG}
                fi
        echo "___________________________________________________________">> ${LOG};
done < $OUTPUT_DIRECTORY/pre_stop_dbopen.log


# Start instance in READ ONLY mode

while read data
do
        export ORACLE_SID=$(echo $data | awk 'BEGIN{FS=":"}{print $2}')       >/dev/null
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
                echo "Starting instance ${ORACLE_SID} "
                if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep "pmon_${ORACLE_SID}\$" | wc -l) -eq 0 ]
                then
                        op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' << EOF
                        startup mount;
                        alter database open read only;
                        set linesize 200 pages 100;
                        set echo off;
                        select instance_name,host_name,startup_time from v\\$instance;
                        select name,open_mode,database_role from v\\$database;
EOF`
        echo "$op"                                              >> ${LOG}; echo "" >> ${LOG}
                fi
        echo "___________________________________________________________">> ${LOG};
done < $OUTPUT_DIRECTORY/pre_stop_dbopenronly.log

# Start instance in MOUNT mode

while read data
do
        export ORACLE_SID=$(echo $data | awk 'BEGIN{FS=":"}{print $2}')       >/dev/null
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
                echo "Mounting instance ${ORACLE_SID} "
                if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep "pmon_${ORACLE_SID}\$" | wc -l) -eq 0 ]
                then
                        op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' << EOF
                        startup mount;
                        set linesize 200 pages 100;
                        set echo off;
                        select instance_name,host_name,startup_time from v\\$instance;
                        select name,open_mode,database_role from v\\$database;
EOF`
        echo "$op"                                              >> ${LOG}; echo "" >> ${LOG}
                fi
        echo "___________________________________________________________">> ${LOG};
done < $OUTPUT_DIRECTORY/pre_stop_dbmount.log
}

start_mrp()
{
echo "******************************* MRP **************************"     >> ${LOG}; echo "" >> ${LOG}

        echo "Starting $what"

while read data
do
        export ORACLE_SID=$data                                               >/dev/null
        export ORAENV_ASK=NO                                                    >/dev/null
        . oraenv                                                                >/dev/null
                echo "Starting MRP on ${ORACLE_SID}"
                if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep "pmon_${ORACLE_SID}\$" | wc -l) -ne 0 ]
                then
                        op=`$ORACLE_HOME/bin/sqlplus -s '/ as sysdba' << EOF
                        set echo off;
                        set linesize 200 pages 100;
                        select name,open_mode,database_role from v\\$database;
                        ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
EOF`
        echo "$op"                                              >> ${LOG}; echo "" >> ${LOG}
                fi
        echo "_____________________________________________________________________"   >> ${LOG};echo "" >> ${LOG}
done < $OUTPUT_DIRECTORY/pre_stop_mrp.log
}

start_lsnr()
{
echo "***************************** LISTENER ************************"     >> ${LOG}; echo "" >> ${LOG}

        if [[ $server_type = "rac_12c" ]] || [[ $server_type = "norac_asm12c" ]] || [[ $server_type = "rac_11g" ]] || [[ $server_type = "norac_asm11g" ]] || [[ $server_type = "rac_10g" ]] || [[ $server_type = "norac_asm10g" ]]
        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
                while read lsnr
                do
                        echo "Starting listener ${lsnr}"
                        #if [ $(ps -ef | grep -v grep | grep pmon | grep "^.*${oraowner} " | grep $lsnr | wc -l) -eq 0 ]
                        if [ $(ps -ef | grep lsnr |grep -v grep | grep "^.*${oraowner} " | grep "tnslsnr $lsnr " | wc -l) -eq 0 ]
                        then
                            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 "Starting listener ${lsnr}" >> ${LOG}
                                echo "" >> ${LOG}
                                lsnrctl start $lsnr                                             >> ${LOG};echo "" >> ${LOG}
                        else
                           echo "" >> ${LOG}
                           echo "Listener: ${lsnr} already in Running state " >> ${LOG}
                           echo "" >> ${LOG}
                        fi
                done < $OUTPUT_DIRECTORY/pre_stop_lsnr.log

        sleep 5
}

start_agent()
{
echo "***************************** EMAGENT ************************"     >> ${LOG}; echo "" >> ${LOG}

if [[ -f $OUTPUT_DIRECTORY/pre_stop_agent.log ]]; then
        echo "Starting $what"
        if [ "$(cat $OUTPUT_DIRECTORY/pre_stop_agent.log)" -eq 0 ]
        then
                echo "EMAgent was down even before the activity, so didn't bring it up"        >> ${LOG}; echo "" >> ${LOG}
        else
                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 start agent                                                        >/dev/null;
                        sleep 15
                   fi
              fi
        fi
else
   echo "Note: pre_stop file Found: $OUTPUT_DIRECTORY/pre_stop_agent.log -> Skipping EMAGENT startup" >> ${LOG}
fi
}

start_fgl()
{
echo "**************************** FOGLIGHT ****************************"       >> ${LOG}; echo "" >> ${LOG}

        echo "Starting $what"
        if [ "$(cat $OUTPUT_DIRECTORY/pre_stop_fgl.log)" -eq 0 ]
        then
                echo "Foglight was down even before the activity, so didn't bring it up"        >> ${LOG}; echo "" >> ${LOG}
        elif [ "$(ps -ef | grep -i foglight | grep "^.*${oraowner} " | grep -v grep | wc -l)" -ne 0 ]
        then
                echo "Foglight is already running"                                               >> ${LOG}; echo "" >> ${LOG}
        else
                if [[ -f /oracle/dba/foglight/fgl_5/bin/fglam ]]; then
                   /oracle/dba/foglight/fgl_5/bin/fglam -d                                  >/dev/null;
                   sleep 30
                else
                   if [[ -f $OUTPUT_DIRECTORY/non_standard_fgl_start.cmd ]]; then
                      chmod 755 $OUTPUT_DIRECTORY/non_standard_fgl_start.cmd
                      $OUTPUT_DIRECTORY/non_standard_fgl_start.cmd  >/dev/null;
                      sleep 30
                   fi
                fi
        fi
}

# OGG Support
##############
start_ogg()
{
echo "***************************** GOLDENGATE ************************"     >> ${LOG}; echo "" >> ${LOG}

        echo "Starting $what"
        if [ "$(cat $OUTPUT_DIRECTORY/pre_stop_ogg.log)" = 0 ]
        then
                echo "OGG was down even before the activity, so didn't bring it up"        >> ${LOG}; echo "" >> ${LOG}
        else
               # Set Ora env
                ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep -v grep | grep "+ASM" | grep "^.*${oraowner} " | 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 -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep "^.*${oraowner} " | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s| tail -1`
                   if [[ ! -z $ORACLE_SID ]]; then
                        . oraenv                                                                >/dev/null
                   fi
                fi

                if [[ -f ${OUTPUT_DIRECTORY}/gg_config.txt ]]; then

                # Read gg start config on this host and act accordingly ..
                cat ${OUTPUT_DIRECTORY}/gg_config.txt | sort -u | while read gg_config
                do

                CRS_RES_GG=`echo $gg_config | grep "GGCRS@" | cut -d ":" -f1 -s | cut -d"@" -f2 -s`
                GG_HOME=`echo $gg_config | cut -d ":" -f2 -s`
                LOCAL_NODE=`echo $gg_config | cut -d ":" -f3 -s`
                if [[ ! -z $CRS_RES_GG ]]; then

                   if [[ -f ${ORACLE_HOME}/bin/crsctl ]]; 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 "Starting Golden Gate via CRS Resource: $CRS_RES_GG" >> ${LOG}
                      if [[ "$server_type" == rac* ]]; then
                         echo "${ORACLE_HOME}/bin/crsctl start res $CRS_RES_GG -n $LOCAL_NODE -f" >> ${LOG}
                         ${ORACLE_HOME}/bin/crsctl start res $CRS_RES_GG -n $LOCAL_NODE -f >> ${LOG}
                         sleep 30
                      else
                         echo "${ORACLE_HOME}/bin/crsctl start res $CRS_RES_GG" >> ${LOG}
                         ${ORACLE_HOME}/bin/crsctl start res $CRS_RES_GG >> ${LOG}
                         sleep 30
                      fi
                   fi

                   fi
                   # display GG mgr output
#${GG_HOME}/ggsci << EOF >> ${LOG}
#info all
#exit
#EOF
                else

                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`
                   echo "Found a value for ORACLE_SID where GG was last running .. So will use: $ORACLE_GG_SID " >> ${LOG}
                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
                     echo "Did Not Find ORACLE_SID where GG was last running .. So will use a Random value: $ORACLE_SID" >> ${LOG}
                     . oraenv
                  fi
                fi

echo "Starting Golden Gate (Non CRS Control) from GG_HOME=${GG_HOME}" >> ${LOG}
${GG_HOME}/ggsci << EOF >> ${LOG}
start mgr
shell sleep 10
exit
EOF
sleep 60
                fi
                done
                fi

        fi

##################################
# Post start OGG status in $LOG
##################################
echo "Collecting Post START GOLDENGATE information ..... "; echo ""
        if [ "$(ps -ef | grep -v grep | grep "mgr PARAMFILE" | grep "^.*${oraowner} " | wc -l)" -eq 0 ]
        then
                echo "GoldenGate is not running on this host under osuser: ${oraowner}"                                >> ${LOG};echo "" >> ${LOG}
        else
                echo "" >> ${LOG}
                echo "******************** POST START GOLDENGATE STATUS **************************"       >> ${LOG}; echo "" >> ${LOG}
                echo "GoldenGate is running on this host under osuser: ${oraowner}"                                >> ${LOG}
                ps -ef | grep -v grep | grep "^.*${oraowner} " | grep "mgr PARAMFILE"                 >> ${LOG};echo "" >> ${LOG}

                # 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 "mgr PARAMFILE" | grep "^.*${oraowner} " | sed -n 's/.*\(....................................................................mgr.prm \).*/\1/p' | awk '{ print $NF }' | sort | while read GG_MGRPROC
ps -ef | grep -v grep | grep "mgr PARAMFILE" | grep "^.*${oraowner} " | sed -n 's/.*\(...................................................................\/....prm \).*/\1/p' | awk '{ print $NF }' | sort | 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'`
           GG_HOME=`echo $GG_MGRPROC | sed 's/\/dirprm\/mgr.prm//g' | sed 's/\/dirprm\/MGR.prm//g'`

sleep 60
if [[ -d $GG_HOME ]]; then
${GG_HOME}/ggsci << EOF >> ${LOG}
info all
exit
EOF
           #echo "$op"                                                      >> ${LOG};echo "" >> ${LOG}
fi
        fi
        done
        fi
}

# DBFS Support
##############
start_dbfs()
{
echo "***************************** DBFS ************************"     >> ${LOG}; echo "" >> ${LOG}

        echo "Starting $what"
        if [ "$(cat $OUTPUT_DIRECTORY/pre_stop_dbfs.log)" = 0 ]
        then
                echo "DBFS was down even before the activity, so didn't bring it up"        >> ${LOG}; echo "" >> ${LOG}
        else
               # Set Ora env
                ORACLE_SID=`ps -ef | grep pmon | sort -k9 | grep -v grep | grep "+ASM" | grep "^.*${oraowner} " | 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 -v grep | grep -v "+ASM" | grep -v "\-MGMTDB" | grep "^.*${oraowner} " | grep -v onitor | awk '{ print $NF }' | cut -d"_" -f3,4 -s | tail -1`
                   if [[ ! -z $ORACLE_SID ]]; then
                        . oraenv                                                                >/dev/null
                   fi
                fi

                # Read gg start config on this host and act accordingly ..
                if [[ -f ${OUTPUT_DIRECTORY}/dbfs_config.txt ]]; then

                cat ${OUTPUT_DIRECTORY}/dbfs_config.txt | sort -u | while read dbfs_config
                do

                CRS_RES_DBFS=`echo $dbfs_config | grep "DBFSCRS@" | cut -d ":" -f1 -s | cut -d"@" -f2 -s`
                DBFS_HOME=`echo $dbfs_config | cut -d ":" -f2 -s`
                LOCAL_NODE=`echo $dbfs_config | cut -d ":" -f3 -s`
                if [[ ! -z $CRS_RES_DBFS ]]; then

                   if [[ -f ${ORACLE_HOME}/bin/crsctl ]]; 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
                      if [[ "$server_type" == rac* ]]; then
                         echo "${ORACLE_HOME}/bin/crsctl start res $CRS_RES_DBFS -n $LOCAL_NODE" >> ${LOG}
                         ${ORACLE_HOME}/bin/crsctl start res $CRS_RES_DBFS -n $LOCAL_NODE >> ${LOG}
                         sleep 60
                      else
                         echo "${ORACLE_HOME}/bin/crsctl start res $CRS_RES_DBFS" >> ${LOG}
                         ${ORACLE_HOME}/bin/crsctl start res $CRS_RES_DBFS  >> ${LOG}
                         sleep 60
                      fi
                   else
                      echo "DBFS CRS Resource: $CRS_RES_DBFS is already ONLINE state ..."  >> ${LOG}; echo "" >> ${LOG}
                   fi

                   fi

                else
                    echo ""                              >> ${LOG}; echo "" >> ${LOG}
                    echo " INFO: For DBA .. DBFS process was detected on this host - As per config file: ${OUTPUT_DIRECTORY}/dbfs_config.txt "   >> ${LOG}; echo "" >> ${LOG}
                    echo " But does not appear to be setup as a CRS Cluster Resource  OR  has failed to be stopped via CRS Cluster Resource " >> ${LOG}
                    echo " ... Please start manually if required ..."   >> ${LOG}; echo "" >> ${LOG}
                    echo ""                      >> ${LOG}; echo "" >> ${LOG}
                fi
                #sleep 60
                done
                fi
        fi
}


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



# Start the appropriate services based on server type


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}

                        when=pre; why=stop; what=dbinst; display_pre_post
                        start_db_rac;
                        when=post; why=start; what=dbinst; sleep 10; collect_$what;  display_pre_post
                        collect_dbservices
                        service_alignment

                for what in mrp agent fgl dbfs ogg
                do
                        when=pre; why=stop; display_pre_post
                        start_$what
                        when=post; why=start; sleep 15; collect_$what; display_pre_post
                done

                echo "      "

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

                for what in dbinst mrp agent fgl dbfs ogg
                do
                        started_status
                done
                        assign_log
                        insert_repdb
                echo "**************************************************"
                ;;
        "rac_11g")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "---------------------------> THIS IS A 11g CLUSTERWARE SYSTEM <------------------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}

                        when=pre; why=stop; what=dbinst; display_pre_post
                        start_db_rac;
                        when=post; why=start; what=dbinst; sleep 10; collect_$what;  display_pre_post
                        collect_dbservices
                        service_alignment

                for what in mrp agent fgl dbfs ogg
                do
                        when=pre; why=stop; display_pre_post
                        start_$what
                        when=post; why=start; sleep 15; collect_$what; display_pre_post
                done

                echo "      "

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

                for what in dbinst mrp agent fgl dbfs ogg
                do
                        started_status
                done
                        assign_log
                        insert_repdb

                echo "**************************************************"
                ;;
        "rac_10g")
        echo "" ;
        echo "---------------------------------------------------------------------------------------"  >> ${LOG};
        echo "---------------------------> THIS IS A 10g CLUSTERWARE SYSTEM <------------------------- "  >> ${LOG};
        echo "---------------------------------------------------------------------------------------"  >> ${LOG}; echo "" >> ${LOG}
                        when=pre; why=stop; what=dbinst; display_pre_post
                        start_asm_10g;
                        when=post; why=start; what=dbinst; sleep 10; collect_$what;  display_pre_post
                        collect_dbservices
                        service_alignment

                        when=pre; why=stop; what=dbinst; display_pre_post
                        start_db_rac;
                        when=post; why=start; what=dbinst; sleep 10; collect_$what;  display_pre_post

                for what in mrp lsnr agent fgl ogg
                do
                        when=pre; why=stop; display_pre_post
                        start_$what
                        when=post; why=start; sleep 10; collect_$what; display_pre_post
                done

                echo "      "

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

                for what in asm dbinst lsnr mrp agent fgl ogg
                do
                        started_status
                done
                        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}

                        #Check for ASM and start if needed
                        when=pre; why=stop; what=asm; display_pre_post
                        start_asm;
                        when=post; why=start; what=asm; sleep 10; collect_$what;  display_pre_post

                        when=pre; why=stop; what=dbinst; display_pre_post
                        start_db_standalone
                        when=post; why=start; what=dbinst; sleep 10; collect_$what;  display_pre_post
                        collect_dbservices
                        service_alignment

                for what in lsnr mrp agent fgl dbfs ogg
                do
                        when=pre; why=stop; display_pre_post
                        start_$what;
                        when=post; why=start; sleep 15; collect_$what; display_pre_post
                done

                echo "      "

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

                for what in asm dbinst mrp agent lsnr fgl dbfs ogg
                do
                        started_status
                done
                        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}

                        #Check for ASM and start if needed
                        when=pre; why=stop; what=asm; display_pre_post
                        start_asm;
                        when=post; why=start; what=asm; sleep 10; collect_$what;  display_pre_post

                        when=pre; why=stop; what=dbinst; display_pre_post
                        start_db_standalone
                        when=post; why=start; what=dbinst; sleep 10; collect_$what;  display_pre_post
                        collect_dbservices
                        service_alignment

                for what in lsnr mrp agent fgl dbfs ogg
                do
                        when=pre; why=stop; display_pre_post
                        start_$what;
                        when=post; why=start; sleep 15; collect_$what; display_pre_post
                done

                echo "      "

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

                for what in asm dbinst mrp agent fgl lsnr dbfs ogg
                do
                        started_status
                done
                        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}

               when=post; why=start;

                        start_asm_10g; what=dbinst; collect_$what
                        start_db_standalone; what=dbinst; collect_$what

                for what in mrp lsnr agent fgl ogg
                        do
                        start_$what; collect_$what
                        done

                echo "      "

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

                for what in fgl agent lsnr dbinst mrp asm ogg
                        do
                        started_status
                        done
                assign_log
                insert_repdb
                echo "**************************************************"

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

                when=pre; why=stop; what=dbinst; display_pre_post
                        start_db_standalone;
                when=post; why=start; what=dbinst; sleep 10; collect_$what;  display_pre_post

                for what in mrp lsnr agent fgl ogg
                        do
                                when=pre; why=stop; display_pre_post
                                start_$what;
                                when=post; why=start; sleep 10; collect_$what; display_pre_post
                        done

                echo "      "

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

                for what in dbinst mrp lsnr agent fgl ogg
                        do
                        started_status
                        done
                assign_log
                insert_repdb
                echo "**************************************************"

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


chmod 755 $OUTPUT_DIRECTORY/pre_stop*.log 1> /dev/null 2>&1
chmod 755 $OUTPUT_DIRECTORY/post_start*.log 1> /dev/null 2>&1
chmod 755 $OUTPUT_DIRECTORY/started*.log 1> /dev/null 2>&1
chmod 755 $OUTPUT_DIRECTORY/bl_start*.log 1> /dev/null 2>&1

chown $oracred $OUTPUT_DIRECTORY/pre_stop*.log 1> /dev/null 2>&1
chown $oracred $OUTPUT_DIRECTORY/post_start*.log 1> /dev/null 2>&1
chown $oracred $OUTPUT_DIRECTORY/started*.log 1> /dev/null 2>&1
chown $oracred $OUTPUT_DIRECTORY/bl_start*.log 1> /dev/null 2>&1


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


echo ""
echo ""
echo "************************************************************************************************************";
echo "Please check the detailed START report at $OUTPUT_DIRECTORY/start_$filename.log"
echo "************************************************************************************************************";
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