Rotate alert logs (alertlog) with shell script

From dbawiki
Revision as of 11:42, 10 October 2013 by Stuart (talk | contribs) (Created page with "<pre> #!/bin/ksh # ============================================================================== # Name : alertlog_rotate.ksh # Description : Rotates the Oracle aler...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#!/bin/ksh
# ==============================================================================
# Name         : alertlog_rotate.ksh
# Description  : Rotates the Oracle alert log
#
# Parameters   : -s sid
#
# 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
# ==============================================================================

PROGNAME=`basename $0`
OS=`uname`
ORATAB="/etc/oratab"
VERSIONS=7


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'

[[ ! -r /etc/oratab ]] && echo "oratab is not where we want it. Please run 'ln -s /var/opt/oracle/oratab $ORATAB' as root and try again" && exit 1


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


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


# -------------------------------
# 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 oldest alert log is older than a month
# -------------------------------------------------
if [[ -e "alert_${ORACLE_SID}.log.${VERSIONS}" ]]; then
    CNT=`find . -name "alert_${ORACLE_SID}.log.${VERSIONS}" -mtime +30 | wc -l`
    [[ $CNT -eq 0 ]] &&  echo "Oldest alert log is not a month old yet, stopping." && 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