- !/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 $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 " && 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"<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