User Tools

Site Tools


rotate_alert_logs_alertlog_with_shell_script
  - !/usr/bin/ksh
  -  ==============================================================================
  -  Name         : maint_rotate_alertlog.ksh
  -  Description  : Rotates the Oracle alert log
  - 
  -  Parameters   : -s (mandatory) sid
  -                 -v (optional)  number of alertlogs to keep
  -                 -p (optional)  days between log rotations
  -                 -a (optional)  age, in days, after which logs can be deleted
  - 
  -  Notes        : Ensures at least a month is kept even if someone
  -                 runs the script every 10 minutes
  - 
  -  Modification History
  -  ====================
  -  When      Who               What
  -  ========= ================= ==================================================
  -  12-SEP-13 Stuart Barkley    Created
  -  ==============================================================================

ORATAB="/etc/oratab"
DEF_VERSIONS=7         # default number of alertlog history files
DEF_ROTATE_PERIOD=7    # default number of days between rotations
DEF_ALERTLOG_AGE=30    # default number of days before oldest log can be deleted

  -  =======================================
  -  thats it, nothing to change below here!
  -  =======================================

PROGNAME=`basename $0`
OS=`uname`
ID=`which id`
AWK=`which awk`
GREP=`which grep`
[[ "${OS}" == "SunOS" ]] && ID='/usr/xpg4/bin/id'
[[ "${OS}" == "SunOS" ]] && AWK='/usr/xpg4/bin/awk'
[[ "${OS}" == "SunOS" ]] && GREP='/usr/xpg4/bin/grep'


  -  -------------------------------------------
  -  dont run around trying to find oratab,
  -  just get them to put it in a standard place
  -  -------------------------------------------
[[ ! -r $ORATAB ]] && echo "oratab is not where we want it. Please run 'ln -s <wherever your oratab is> $ORATAB' as root and try again" && exit 1


  -  -------------------------
  -  get the arguments, if any
  -  -------------------------
while getopts "s:v:p:a:" OPT
do
    case "$OPT" in
    s) export ORACLE_SID=$OPTARG;;
    v) VERSIONS=$OPTARG;;
    p) ROTATE_PERIOD=$OPTARG;;
    a) ALERTLOG_AGE=$OPTARG;;
    esac
done
shift $((OPTIND-1))


  -  --------------------------------
  -  check we have required arguments
  -  --------------------------------
[[ -z $ORACLE_SID ]] && echo "Not all mandatory parameters supplied. Usage: $0 -s <SID>" && exit 1


  -  -------------------------------------------------------
  -  set the optional parameters to defaults if not supplied
  -  -------------------------------------------------------
VERSIONS=${VERSIONS:-$DEF_VERSIONS}
ROTATE_PERIOD=${ROTATE_PERIOD:-$DEF_ROTATE_PERIOD}
ALERTLOG_AGE=${ALERTLOG_AGE:-$DEF_ALERTLOG_AGE}


  -  -------------------------------
  -  check the supplied SID is valid
  -  -------------------------------
$GREP -q -E "^${ORACLE_SID}:" $ORATAB
[[ $? -ne 0 ]] && echo "$ORACLE_SID not found in $ORATAB, please verify SID and run again" && exit 1


  -  ------------------------------------------
  -  check we are running as the database owner
  -  ------------------------------------------
DB_STARTED_AS=`ps -ef | $GREP [[p]]mon | $GREP $ORACLE_SID | $AWK '{print $1}'`
WE_ARE=`$ID -un`
[[ "$DB_STARTED_AS" != "$WE_ARE" ]] && echo "You are ${WE_ARE}. Please run as the database owner (${DB_STARTED_AS})." && exit 1


  -  -------------------------------------------------------------------
  -  setup the Oracle environment so we can find the alert log directory
  -  -------------------------------------------------------------------
ORAENV_ASK=NO
. oraenv


  -  -----------------------------------------------
  -  Now we can get the ALERT_PATH from the database
  -  -----------------------------------------------
ALERT_PATH=`sqlplus -s "/ as sysdba"<<EOSQL
set pages 0
set feedb off
select value from v\\\\$diag_info where lower(name) = 'diag trace';
EOSQL
`
[[ $? -ne 0 ]] &&  echo "Problem connecting to SQL*Plus. Has to be run from a privileged user account (oracle, oraibm etc.)" && exit 1

cd $ALERT_PATH
[[ $? -ne 0 ]] &&  echo "ALERT_PATH not set or set to a non-existing directory (${ALERT_PATH}), please verify and run again" && exit 1


  -  ---------------------------------------------------------
  -  Ensure the most recent alert log was not rotated too soon
  -  ---------------------------------------------------------
if [[ -e "alert_${ORACLE_SID}.log.1" ]]; then
    CNT=`find . -name "alert_${ORACLE_SID}.log.1" -mtime +${ROTATE_PERIOD} | wc -l`
    [[ $CNT -eq 0 ]] &&  echo "Last alert log rotation was less than ${ROTATE_PERIOD} days ago, nothing to do here." && exit 1
fi


  -  ------------------------------------------------------
  -  rotate the old logs. 6->7, 5->6 etc... down until 1->2
  -  ------------------------------------------------------
i=$VERSIONS
while [[ $i -ge 2 ]]; do
    if  [[ -e alert_$ORACLE_SID.log.$((i-1)) ]]; then
        mv alert_$ORACLE_SID.log.$((i-1))  alert_$ORACLE_SID.log.$i
        [[ $? -ne 0 ]] && echo "Cannot rename alert_$ORACLE_SID.log.$((i-1)) to alert_$ORACLE_SID.log.$i, permissions?" && exit 1
    fi
    i=$((i-1))
done

  -  and finally, rotate the current log
cp alert_$ORACLE_SID.log alert_$ORACLE_SID.log.1
[[ $? -ne 0 ]] && echo "Cannot rename alert_$ORACLE_SID.log to alert_$ORACLE_SID.log.1, permissions?" && exit 1
echo > alert_$ORACLE_SID.log
rotate_alert_logs_alertlog_with_shell_script.txt · Last modified: 2019/01/30 11:32 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki