User Tools

Site Tools


rotate_alert_logs_alertlog_with_shell_script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
rotate_alert_logs_alertlog_with_shell_script [2018/12/08 12:49] – created 0.0.0.0rotate_alert_logs_alertlog_with_shell_script [2019/01/30 11:32] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Rotate_alert_logs_(alertlog)_with_shell_script ====== 
- 
 <code> <code>
-#!/usr/bin/ksh +  - !/usr/bin/ksh 
-============================================================================== +  -  ============================================================================== 
-Name         : maint_rotate_alertlog.ksh +  -  Name         : maint_rotate_alertlog.ksh 
-Description  : Rotates the Oracle alert log +  -  Description  : Rotates the Oracle alert log 
-# +   
-Parameters   : -s (mandatory) sid +  -  Parameters   : -s (mandatory) sid 
-#                -v (optional)  number of alertlogs to keep +                  -v (optional)  number of alertlogs to keep 
-#                -p (optional)  days between log rotations +                  -p (optional)  days between log rotations 
-#                -a (optional)  age, in days, after which logs can be deleted +                  -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 
-#                runs the script every 10 minutes +                  runs the script every 10 minutes 
-# +   
-Modification History +  -  Modification History 
-==================== +  -  ==================== 
-When      Who               What +  -  When      Who               What 
-========= ================= ================================================== +  -  ========= ================= ================================================== 
-12-SEP-13 Stuart Barkley    Created +  -  12-SEP-13 Stuart Barkley    Created 
-==============================================================================+  -  ==============================================================================
  
 ORATAB="/etc/oratab" ORATAB="/etc/oratab"
Line 27: Line 25:
 DEF_ALERTLOG_AGE=30    # default number of days before oldest log can be deleted DEF_ALERTLOG_AGE=30    # default number of days before oldest log can be deleted
  
-======================================= +  -  ======================================= 
-thats it, nothing to change below here! +  -  thats it, nothing to change below here! 
-=======================================+  -  =======================================
  
 PROGNAME=`basename $0` PROGNAME=`basename $0`
Line 41: Line 39:
  
  
-------------------------------------------- +  -  ------------------------------------------- 
-dont run around trying to find oratab, +  -  dont run around trying to find oratab, 
-just get them to put it in a standard place +  -  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 [[ ! -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 +  -  get the arguments, if any 
--------------------------+  -  -------------------------
 while getopts "s:v:p:a:" OPT while getopts "s:v:p:a:" OPT
 do do
Line 63: Line 61:
  
  
--------------------------------- +  -  -------------------------------- 
-check we have required arguments +  -  check we have required arguments 
---------------------------------+  -  --------------------------------
 [[ -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 +  -  set the optional parameters to defaults if not supplied 
--------------------------------------------------------+  -  -------------------------------------------------------
 VERSIONS=${VERSIONS:-$DEF_VERSIONS} VERSIONS=${VERSIONS:-$DEF_VERSIONS}
 ROTATE_PERIOD=${ROTATE_PERIOD:-$DEF_ROTATE_PERIOD} ROTATE_PERIOD=${ROTATE_PERIOD:-$DEF_ROTATE_PERIOD}
Line 77: Line 75:
  
  
-------------------------------- +  -  ------------------------------- 
-check the supplied SID is valid +  -  check the supplied SID is valid 
--------------------------------+  -  -------------------------------
 $GREP -q -E "^${ORACLE_SID}:" $ORATAB $GREP -q -E "^${ORACLE_SID}:" $ORATAB
 [[ $? -ne 0 ]] && echo "$ORACLE_SID not found in $ORATAB, please verify SID and run again" && exit 1 [[ $? -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 +  -  check we are running as the database owner 
------------------------------------------- +  -  ------------------------------------------ 
-DB_STARTED_AS=`ps -ef | $GREP [p]mon | $GREP $ORACLE_SID | $AWK '{print $1}'`+DB_STARTED_AS=`ps -ef | $GREP [[p]]mon | $GREP $ORACLE_SID | $AWK '{print $1}'`
 WE_ARE=`$ID -un` 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 [[ "$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 +  -  setup the Oracle environment so we can find the alert log directory 
--------------------------------------------------------------------+  -  -------------------------------------------------------------------
 ORAENV_ASK=NO ORAENV_ASK=NO
 . oraenv . oraenv
  
  
------------------------------------------------ +  -  ----------------------------------------------- 
-Now we can get the ALERT_PATH from the database +  -  Now we can get the ALERT_PATH from the database 
------------------------------------------------+  -  -----------------------------------------------
 ALERT_PATH=`sqlplus -s "/ as sysdba"<<EOSQL ALERT_PATH=`sqlplus -s "/ as sysdba"<<EOSQL
 set pages 0 set pages 0
 set feedb off set feedb off
-select value from v\\$diag_info where lower(name) = 'diag trace';+select value from v\\\\$diag_info where lower(name) = 'diag trace';
 EOSQL EOSQL
 ` `
Line 114: Line 112:
  
  
---------------------------------------------------- +   --------------------------------------------------------- 
-# Ensure the oldest alert log is older than permitted +   Ensure the most recent alert log was not rotated too soon 
---------------------------------------------------- +   ---------------------------------------------------------
-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 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`
Line 138: Line 121:
  
  
------------------------------------------------------- +  -  ------------------------------------------------------ 
-rotate the old logs. 6->7, 5->6 etc... down until 1->2 +  -  rotate the old logs. 6->7, 5->6 etc... down until 1->2 
-------------------------------------------------------+  -  ------------------------------------------------------
 i=$VERSIONS i=$VERSIONS
 while [[ $i -ge 2 ]]; do while [[ $i -ge 2 ]]; do
Line 150: Line 133:
 done done
  
-and finally, rotate the current log+  -  and finally, rotate the current log
 cp alert_$ORACLE_SID.log alert_$ORACLE_SID.log.1 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 [[ $? -ne 0 ]] && echo "Cannot rename alert_$ORACLE_SID.log to alert_$ORACLE_SID.log.1, permissions?" && exit 1
 echo > alert_$ORACLE_SID.log echo > alert_$ORACLE_SID.log
 </code> </code>
 +
rotate_alert_logs_alertlog_with_shell_script.1544273361.txt.gz · Last modified: 2018/12/08 12:49 by 0.0.0.0

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki