Monday, November 17, 2008

Shell script to clean up log files Oracle Application Server

This is an example of a shell script to clean up log files in an Oracle Application Server. Use at your own risk :)

#!/bin/sh
#################################################################
# Author : Arjan Goos #
# Date : 03-06-2008 #
# #
# Purpose : Clean log files Oracle Application Server #
# #
# History : #
# --------------------------------------------------------------#
# Date |Author |Description #
# --------------------------------------------------------------#
# 03-06-2008 |Arjan Goos |Creation #
# #
#################################################################
SCRIPT=`basename $0`
SCRIPT_PATH="/oracle/scripts/as/clean_log"
HOST_NAME=`hostname`

DATE=`date "+%d-%m-%y"`
STATUS_FILE=$SCRIPT_PATH/log/status-${SCRIPT}_${HOST_NAME}_${DATE}.txt

MAILADRES="test@test.nl"
DU_TOTAL=0

mail_logfile()
{
mail -s "Clean log files $HOST_NAME - Oracle home: $ORACLE_HOME - ( $DATE )" $MAILADRES < $STATUS_FILE
}

disk_usages_dir()
{
DUH=`du -hc | grep -i total | awk -F"total" '{ print $1 }'`
DIR=`pwd`
}

disk_usages_home()
{
DU_HOME=`du -c | grep -i total | awk -F"total" '{ print $1 }'`
DU_HOME=`expr $DU_HOME / 1024`
}

clean_apache()
{
echo " Start Clean Up Apache logging" >> $STATUS_FILE
echo "" >> $STATUS_FILE
cd $ORACLE_HOME/Apache/Apache/logs
disk_usages_dir
echo " Disk Usages in $DIR before $SCRIPT: $DUH." >> $STATUS_FILE
FILE="*_log.*"
echo " These log files are removed:" >> $STATUS_FILE
for LOG_FILE in `find ./ -name "$FILE" -mtime +"$TIME" -print 2>&1`
do
echo " $LOG_FILE" >> $STATUS_FILE
rm $LOG_FILE
done
disk_usages_dir
echo " Disk Usages in $DIR after $SCRIPT: $DUH." >> $STATUS_FILE
echo "" >> $STATUS_FILE
echo " Ready Clean Up Apache logging" >> $STATUS_FILE
echo "" >> $STATUS_FILE
}

clean_application_server_control()
{
echo " Start Clean Up Application Server Control" >> $STATUS_FILE
echo "" >> $STATUS_FILE
cd $ORACLE_HOME/opmn/logs >> $STATUS_FILE
disk_usages_dir
echo " Disk Usages in $DIR before $SCRIPT: $DUH." >> $STATUS_FILE
# Rotate log files
FILE="*.log*"
rotate_files
# Delete old rotated log files
delete_files
disk_usages_dir
echo " Disk Usages in $DIR after $SCRIPT: $DUH." >> $STATUS_FILE
echo "" >> $STATUS_FILE
echo " Ready Clean Up Application Server Control" >> $STATUS_FILE
echo "" >> $STATUS_FILE
}

clean_oc4j_instances()
{
echo " Start Clean Up OC4J Instances" >> $STATUS_FILE
echo "" >> $STATUS_FILE
cd $ORACLE_HOME/j2ee >> $STATUS_FILE
# Get all oc4j_instances from the opmn.xml
for OC4J_INSTANCE in `grep -i module-id=\"OC4J\" $ORACLE_HOME/opmn/conf/opmn.xml | awk -F'<process-type id=' '{ print $2 }' | awk -F" " '{ print $1 }' | sed 's/\"//g'`
do
echo " Start OC4J_CONTAINER: $OC4J_INSTANCE." >> $STATUS_FILE
cd $OC4J_INSTANCE/log
disk_usages_dir
echo " Disk Usages in $DIR before $SCRIPT: $DUH." >> $STATUS_FILE
# Rotate log files
FILE="*.log"
rotate_files
# Delete old rotated log files
delete_files
disk_usages_dir
echo " Disk Usages in $DIR after $SCRIPT: $DUH." >> $STATUS_FILE
cd - > /dev/null
echo " Ready OC4J_CONTAINER: $OC4J_INSTANCE." >> $STATUS_FILE
echo "" >> $STATUS_FILE
done
echo "" >> $STATUS_FILE
echo " Ready Clean Up OC4J Instances" >> $STATUS_FILE
echo "" >> $STATUS_FILE
}

clean_webcache()
{
if [ -d $ORACLE_HOME/webcache/log ];
then
echo " Start Clean Up WebCache" >> $STATUS_FILE
echo "" >> $STATUS_FILE
cd $ORACLE_HOME/webcache/log >> $STATUS_FILE
disk_usages_dir
echo " Disk Usages in $DIR before $SCRIPT: $DUH." >> $STATUS_FILE
# Rotate log files
FILE="*_log"
rotate_files
# Delete old rotated log files
delete_files
disk_usages_dir
echo " Disk Usages in $DIR after $SCRIPT: $DUH." >> $STATUS_FILE
echo "" >> $STATUS_FILE
echo " Ready Clean Up WebCache" >> $STATUS_FILE
echo "" >> $STATUS_FILE
fi
}

rotate_files()
{
#This function rotates the log files when the file is bigger than $ROTATE_SIZE
echo " These files are rotated: " >> $STATUS_FILE
for LOG_FILE in `find ./ -name "$FILE" -size +"$ROTATE_SIZE" -print | grep -v "archive" 2>&1`
do
#echo "cp $LOG_FILE $LOG_FILE.archive.$DATE"
echo " $LOG_FILE to $LOG_FILE.archive.$DATE" >> $STATUS_FILE
cp $LOG_FILE $LOG_FILE.archive.$DATE
cp /dev/null $LOG_FILE
done
echo "" >> $STATUS_FILE
}

delete_files()
{
#This function removes the rotated log files when the file is older than $TIME
FILE="*.archive.*"
echo " These archive files are removed:" >> $STATUS_FILE
for LOG_FILE in `find ./ -name "$FILE" -mtime +"$TIME" -print 2>&1`
do
echo " $LOG_FILE" >> $STATUS_FILE
rm $LOG_FILE
done
echo "" >> $STATUS_FILE
}

find_large_log_files()
{
cd $ORACLE_HOME
echo "" >> $STATUS_FILE
echo " Overview big remaining log files (manual action required):" >> $STATUS_FILE
echo ""
echo " These *.log files are in the $ORACLE_HOME with a size larger than $ROTATE_SIZE and older than $TIME day(s)." >> $STATUS_FILE
find ./ -name "*log*" -size +"2048k" -mtime +"$TIME" -exec ls -lh {} \; | awk '{ print " " $9 " size: " $5 }' >> $STATUS_FILE
echo "" >> $STATUS_FILE
}
cd $SCRIPT_PATH
#MAIN
if [ $# != 1 ];
then
echo "Start op: $0 <files older than DATE>"
exit 1
fi

#ORACLE_HOME=$1
TIME=$1
#Files bigger than 2 Mb are rotated
ROTATE_SIZE="2048k"
#ROTATE_SIZE="10k"

clear
echo "Start of the $SCRIPT on `date`: $HOST_NAME." >> $STATUS_FILE
echo "" >> $STATUS_FILE

for ORACLE_HOME in `cat /etc/oratab | grep -v "^#" | grep -v "^agent" | awk -F":" '{ print $2 }'`
do
if [ -n ${ORACLE_HOME} ];
then
echo " Start Oracle Home: $ORACLE_HOME." >> $STATUS_FILE
cd $ORACLE_HOME
disk_usages_home
echo "" >> $STATUS_FILE
### MAIN PROG
### Apache log
clean_apache
### Application Server Control logs
clean_application_server_control
### OC4J instances
clean_oc4j_instances
### Webcache
clean_webcache
#TO_DO
#application specific logging
find_large_log_files
echo "" >> $STATUS_FILE
echo " Total Disk Usage $ORACLE_HOME before $SCRIPT : $DU_HOME MB." >> $STATUS_FILE
DU_HOME_START=$DU_HOME
disk_usages_home
DU_HOME_STOP=$DU_HOME
echo " Total Disk Usage $ORACLE_HOME after $SCRIPT : $DU_HOME MB." >> $STATUS_FILE
echo "" >> $STATUS_FILE
echo " Total Cleared Disk Space $ORACLE_HOME by $SCRIPT: `expr $DU_HOME_START - $DU_HOME_STOP` MB." >> $STATUS_FILE
echo "" >> $STATUS_FILE
echo " Ready Oracle Home: $ORACLE_HOME." >> $STATUS_FILE
echo "" >> $STATUS_FILE
echo "" >> $STATUS_FILE
fi
done

echo "" >> $STATUS_FILE
echo "Ready of the $SCRIPT on `date`" >> $STATUS_FILE
echo "" >> $STATUS_FILE
echo "" >> $STATUS_FILE
mail_logfile
exit 0

1 comment:

  1. Excellent helpful script for fast and simple solution. Thank you very much.

    ReplyDelete

comment