Difference between revisions of "Rotate alert logs (alertlog) with shell script"
From dbawiki
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<pre> | <pre> | ||
| − | #!/bin/ksh | + | #!/usr/bin/ksh |
# ============================================================================== | # ============================================================================== | ||
| − | # Name : | + | # Name : maint_rotate_alertlog.ksh |
# Description : Rotates the Oracle alert log | # Description : Rotates the Oracle alert log | ||
# | # | ||
| − | # Parameters : -s sid | + | # 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 | # Notes : Ensures at least a month is kept even if someone | ||
| Line 18: | Line 21: | ||
ORATAB="/etc/oratab" | 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 | |
| − | |||
# ======================================= | # ======================================= | ||
| Line 36: | Line 38: | ||
[[ "${OS}" == "SunOS" ]] && GREP='/usr/xpg4/bin/grep' | [[ "${OS}" == "SunOS" ]] && GREP='/usr/xpg4/bin/grep' | ||
| − | [[ ! -r | + | |
| + | # ------------------------------------------- | ||
| + | # 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 | ||
| Line 42: | Line 49: | ||
# get the arguments, if any | # get the arguments, if any | ||
# ------------------------- | # ------------------------- | ||
| − | while getopts "s:" OPT | + | while getopts "s:v:p:a:" OPT |
do | do | ||
case "$OPT" in | case "$OPT" in | ||
s) export ORACLE_SID=$OPTARG;; | s) export ORACLE_SID=$OPTARG;; | ||
| + | v) VERSIONS=$OPTARG;; | ||
| + | p) ROTATE_PERIOD=$OPTARG;; | ||
| + | a) ALERTLOG_AGE=$OPTARG;; | ||
esac | esac | ||
done | done | ||
| Line 55: | Line 65: | ||
# -------------------------------- | # -------------------------------- | ||
[[ -z $ORACLE_SID ]] && echo "Not all mandatory parameters supplied. Usage: $0 -s <SID>" && exit 1 | [[ -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} | ||
| Line 99: | Line 117: | ||
if [[ -e "alert_${ORACLE_SID}.log.${VERSIONS}" ]]; then | if [[ -e "alert_${ORACLE_SID}.log.${VERSIONS}" ]]; then | ||
CNT=`find . -name "alert_${ORACLE_SID}.log.${VERSIONS}" -mtime +${ALERTLOG_AGE} | wc -l` | CNT=`find . -name "alert_${ORACLE_SID}.log.${VERSIONS}" -mtime +${ALERTLOG_AGE} | wc -l` | ||
| − | [[ $CNT -eq 0 ]] | + | if [[ $CNT -eq 0 ]]; then |
| + | echo "Oldest alert log is not ${ALERTLOG_AGE} days old yet, nothing to do here." && exit 1 | ||
| + | else | ||
| + | for i in `find . -name "alert_${ORACLE_SID}.log.${VERSIONS}" -mtime +${ALERTLOG_AGE}`; do | ||
| + | echo ". deleting "$i; rm $i | ||
| + | done | ||
| + | fi | ||
fi | fi | ||
| + | |||
# -------------------------------------------------- | # -------------------------------------------------- | ||
| Line 107: | Line 132: | ||
if [[ -e "alert_${ORACLE_SID}.log.1" ]]; then | if [[ -e "alert_${ORACLE_SID}.log.1" ]]; then | ||
CNT=`find . -name "alert_${ORACLE_SID}.log.1" -mtime +${ROTATE_PERIOD} | wc -l` | 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, | + | [[ $CNT -eq 0 ]] && echo "Last alert log rotation was less than ${ROTATE_PERIOD} days ago, nothing to do here." && exit 1 |
fi | fi | ||
Latest revision as of 11:19, 18 October 2013
#!/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 oldest alert log is older than permitted
# ---------------------------------------------------
if [[ -e "alert_${ORACLE_SID}.log.${VERSIONS}" ]]; then
CNT=`find . -name "alert_${ORACLE_SID}.log.${VERSIONS}" -mtime +${ALERTLOG_AGE} | wc -l`
if [[ $CNT -eq 0 ]]; then
echo "Oldest alert log is not ${ALERTLOG_AGE} days old yet, nothing to do here." && exit 1
else
for i in `find . -name "alert_${ORACLE_SID}.log.${VERSIONS}" -mtime +${ALERTLOG_AGE}`; do
echo ". deleting "$i; rm $i
done
fi
fi
# --------------------------------------------------
# Ensure the last 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