Tidy the Oracle Logs
#!/bin/ksh -x
############################################################################
#
#    Script :        tidy_oracle_logs.ksh
#
#    Date :         28th July 1998
#
#    Author :        Mark Ramsay
#
#    Description :    For each SID found in /var/opt/oracle/oratab 
#                Copies the Oracle
#                       alertSID.log to alertSID.DAY and empties the original.
#                       listener.log to listener.DAY and empties the original.
#                       Clears down udump.  (Keeps 30 days)
#                       Clears down bdump.  (Keeps 30 days)
#                       Clears down adump.  (Keeps 2 days)
#                       Works with 8i, 9i and 10g.
#
#    Parameters :    None.
#
#    History:          Date      Name      Reason                                      
#                       ----      ----      ------                                      
#                       dd/mm/yy  Initials  Description of change
#
#    Copyright:         www.markramsay.com
#
############################################################################
#
#Set-up variables and parameter validation.
#

SDS_param1=${1:-false}

if [[ $SDS_param1 != "false" ]] 
then echo "Parameter invalid."
     echo
     echo "Parameters:- "                                                       
     echo ""                                                                    
     echo "None."
     echo ""
     echo "Description."
     echo ""
     echo "Copies the Oracle alert logs for all instances on this"
     echo "machine to monthly files and empties the original files."
     echo "If the monthly files exist then it appends to the monthly"
     echo "files before emptying the alert logs."
     echo ""
     exit
fi

#
#Preserve the current ORACLE_SID, PATH and ORACLE_HOME.
#

SDS_old_oracle_sid=$ORACLE_SID
SDS_old_oracle_home=$ORACLE_HOME
SDS_old_path=$PATH

#
#Establish the number of instances
#

SDS_num_of_instances=$(cat /var/opt/oracle/oratab |egrep -v '(^#|^\*|^\+|^LISTENER)' |grep \: |wc -l)
SDS_instances=$(cat /var/opt/oracle/oratab |egrep -v '(^#|^\*|^\+|^LISTENER)'|grep \: |awk -F\: '{print $1}')
for f in $SDS_instances
do

SDS_instance_name=$f
SDS_oracle_home=$(cat /var/opt/oracle/oratab |egrep -v '(^#|^\*|^\+|^LISTENER)' |grep \: | grep $SDS_instance_name | awk -F\: '{print $2}')

ORACLE_SID=$SDS_instance_name
export ORACLE_SID
ORACLE_HOME=$SDS_oracle_home
export ORACLE_HOME
PATH=$SDS_old_path
PATH=$PATH:$ORACLE_HOME/bin
export PATH

#
#Validate the instance name.
#

SDS_instance_check=$(cat /var/opt/oracle/oratab |grep $SDS_instance_name |wc -l)           

if [[ $SDS_instance_check -eq 0 ]]                                              
   then echo "The instance id specified does not exist in /var/opt/oracle/oratab."         
        echo "This could indicate a problem with the script."
    ORACLE_SID=$SDS_old_oracle_sid
    export ORACLE_SID
    ORACLE_HOME=$SDS_old_oracle_home
    export ORACLE_HOME
    PATH=$SDS_old_path
    export PATH
        exit                                                                    
fi                                                                              

#
#Get the Alert Log Location.
#

SDS_parameter=v\$parameter

SDS_bdump=`sqlplus -SILENT / <<EOF 
set HEA off
set feedback off
set pagesize 0
set verify off
select value from $SDS_parameter where name='background_dump_dest';
quit
EOF`

#
#Get the udump Log Location.
#

SDS_parameter=v\$parameter

SDS_udump=`sqlplus -SILENT / <<EOF 
set HEA off
set feedback off
set pagesize 0
set verify off
select value from $SDS_parameter where name='user_dump_dest';
quit
EOF`

#
#Get the Audit Dest Location.
#

SDS_parameter=v\$parameter

SDS_temp=`sqlplus -SILENT / <<EOF 
set HEA off
set feedback off
set pagesize 0
set verify off
select value from $SDS_parameter where name='audit_file_dest';
quit
EOF`

echo $SDS_temp |grep \? 
if [ $? -eq 0 ] 
then
SDS_audit=$(echo $SDS_temp |sed -e s!\?!$ORACLE_HOME!)
else
SDS_audit=$SDS_temp
fi

#
#Get the Listener Log Location.
#

SDS_listener=`lsnrctl status|grep 'Listener Log File'|awk '{print $4}'`
#SDS_listener=$ORACLE_HOME/network/log

#
#Check for Errors in the Alert Log befor Cleardown
#

#date >/tmp/tidy_oracle_logs.rpt
#echo "" >>/tmp/tidy_oracle_logs.rpt
#echo "The following errors were found in the alert.log for $ORACLE_SID:-" >>/tmp/tidy_oracle_logs.rpt
#echo "" >>/tmp/tidy_oracle_logs.rpt
#echo "                            ++++++++++++++" >>/tmp/tidy_oracle_logs.rpt
#echo "" >>/tmp/tidy_oracle_logs.rpt
#grep -i ORA- $SDS_bdump/alert_$ORACLE_SID.log >>/tmp/tidy_oracle_logs.rpt
#echo "" >>/tmp/tidy_oracle_logs.rpt
#echo "                            ++++++++++++++" >>/tmp/tidy_oracle_logs.rpt

#
#Begin Tidy Routines
#

echo "" >>/tmp/tidy_oracle_logs.rpt
echo "" >>/tmp/tidy_oracle_logs.rpt
echo "Alert Log Cleardown Beginning...." >>/tmp/tidy_oracle_logs.rpt
echo "" >>/tmp/tidy_oracle_logs.rpt
echo "Location of the alert log for instance $SDS_instance_name is...." >>/tmp/tidy_oracle_logs.rpt
echo "" >>/tmp/tidy_oracle_logs.rpt
echo "$SDS_bdump" >>/tmp/tidy_oracle_logs.rpt

############################################################################
#        Clear Down alert_ORACLE_SID.log file
############################################################################
   echo "\nCopying file alert_$ORACLE_SID.log to alert_$ORACLE_SID.$(date +%a)\n">>/tmp/tidy_oracle_logs.rpt
   cp $SDS_bdump/alert_$ORACLE_SID.log $SDS_bdump/alert_$ORACLE_SID.$(date +%a)
   errlog_copy_rc=$?
if [ $errlog_copy_rc -eq 0 ] ; then
   cp /dev/null $SDS_bdump/alert_$ORACLE_SID.log
   echo "\nFile $SDS_bdump/alert_$ORACLE_SID.log ">>/tmp/tidy_oracle_logs.rpt
   echo "\ncopied to " >>/tmp/tidy_oracle_logs.rpt
   echo "\n$SDS_bdump/alert_$ORACLE_SID.$(date +%a) and emptied\n">>/tmp/tidy_oracle_logs.rpt
else
   echo "\nFailed to copy/empty $SDS_bdump/alert_$ORACLE_SID.log\n">>/tmp/tidy_oracle_logs.rpt
   exit
fi

echo "Oracle Alert Log Cleardown for instance $SDS_instance_name Completed." >>/tmp/tidy_oracle_logs.rpt

############################################################################
#               Clear Down listener.log file
############################################################################
   echo "\nCopying file listener.log to listener.$(date +%a)\n">>/tmp/tidy_oracle_logs.rpt
   cp $SDS_listener $SDS_listener.$(date +%a)
   errlog_copy_rc=$?
if [ $errlog_copy_rc -eq 0 ] ; then
   cp /dev/null $SDS_listener
   echo "\nFile $SDS_listener ">>/tmp/tidy_oracle_logs.rpt
   echo "\ncopied to " >>/tmp/tidy_oracle_logs.rpt
   echo "\n$SDS_listener.$(date +%a) and emptied\n">>/tmp/tidy_oracle_logs.rpt
else
   echo "\nFailed to copy/empty $SDS_listener\n">>/tmp/tidy_oracle_logs.rpt
   exit
fi

echo "Oracle Listener Log Cleardown for instance $SDS_instance_name Completed."
>>/tmp/tidy_oracle_logs.rpt
############################################################################
#        Clear Down trc files older than 30 days
############################################################################

find $SDS_bdump -name "*.trc" -mtime +30 -follow -exec rm {} \;
find $SDS_udump -name "*.trc" -mtime +30 -follow -exec rm {} \;
find $SDS_audit -name "*.aud" -mtime +2 -follow -exec rm {} \;

done

#
#Restore Previous Variable Values.
#

ORACLE_SID=$SDS_old_oracle_sid
export ORACLE_SID
ORACLE_HOME=$SDS_old_oracle_home
export ORACLE_HOME
PATH=$SDS_old_path
export PATH

#
#End script
#

echo "JOB COMPLETE"
exit
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License