Purging and archiving Oracle alert.log and listener.log
As i said in "Purging Oracle traces with ADRCI" ADRCI does not work properly for alert.log and listener.log, so in this article i´m going to study who to do this tasks externally, with unix tools.
1-With logrotate Linux-command. I think this is the best option if you have an Oracle linux because it can be setup easily...
Put this lines in a config file but modifing your alert.log and listener.log paths
# alert log /oracle/db/diag/rdbms/orcl/orcl/trace/alert_orcl.log { monthly rotate 13 notifempty missingok copytruncate nocreate } # listener log /oracle/db/diag/tnslsnr/wcp12cr2/listener/trace/listener.log { weekly rotate 53 notifempty missingok copytruncate nocreate }
i put them into alert_logrotate.conf as you can see:
[oracle@wcp12cr2 Documents]$ ls -ltr alert_logrotate.conf -rw-r--r--. 1 oracle oinstall 593 Oct 5 03:08 alert_logrotate.conf [oracle@wcp12cr2 Documents]$ cat alert_logrotate.conf # see "man logrotate" for details # rotate log files weekly or monthly # keep 4 weeks worth of backlogs # create new (empty) log files after rotating old ones # use date as a suffix of the rotated file # uncomment this if you want your log files compressed # alert log /oracle/db/diag/rdbms/orcl/orcl/trace/alert_orcl.log { monthly rotate 13 notifempty missingok copytruncate nocreate } # # listener log /oracle/db/diag/tnslsnr/wcp12cr2/listener/trace/listener.log { weekly rotate 53 notifempty missingok copytruncate nocreate }
rotate count Log files are rotated count times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather than rotated.
daily-weekly-monthly Log files are rotated every ---
notifempty Do not rotate the log if it is empty (this overrides the ifempty option).
nomissingok If a log file does not exist, issue an error. This is the default.
copytruncate Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old logfile stays in place.
nocreate New log files are not created (this overrides the create option).
Then, i executed:
[oracle@wcp12cr2 Documents]$ logrotate -s /home/oracle/Documents/logrotate.status -f /home/oracle/Documents/alert_logrotate.conf [oracle@wcp12cr2 Documents]$Parameter explanation:
-s /home/oracle/Documents/logrotate.status location where this process will write his execution log.
-f /home/oracle/Documents/alert_logrotate.conf Setup file we configured previously
So after 2 executions of this command
[oracle@wcp12cr2 Documents]$ ls -ltr /oracle/db/diag/rdbms/orcl/orcl/trace/alert* -rw-r-----. 1 oracle oinstall 199808 Oct 4 03:08 /oracle/db/diag/rdbms/orcl/orcl/trace/alert_orcl.log-20171004 -rw-r-----. 1 oracle oinstall 201808 Oct 5 03:08 /oracle/db/diag/rdbms/orcl/orcl/trace/alert_orcl.log-20171005 -rw-r-----. 1 oracle oinstall 211 Oct 6 12:39 /oracle/db/diag/rdbms/orcl/orcl/trace/alert_orcl.log [oracle@wcp12cr2 Documents]$ ls -ltr /oracle/db/diag/tnslsnr/wcp12cr2/listener/trace/lis* -rw-r-----. 1 oracle oinstall 433815 Oct 4 03:08 /oracle/db/diag/tnslsnr/wcp12cr2/listener/trace/listener.log-20171004 -rw-r-----. 1 oracle oinstall 433815 Oct 5 03:08 /oracle/db/diag/tnslsnr/wcp12cr2/listener/trace/listener.log-20171005 -rw-r-----. 1 oracle oinstall 123 Oct 6 12:49 /oracle/db/diag/tnslsnr/wcp12cr2/listener/trace/listener.log
2-With Unix script.
If you do not have logrotate command in your system, you can configure alert.log rotation with this script (also listener.ora if you modify it accordingly)
This script split alert.log in two files, files older than 1 day (configurable) and another one newer than 1 day, for example in my machine after 3 executions:
[oracle@wcp12cr2 Documents]$ ls -ltr aler* -rw-r--r--. 1 oracle oinstall 198876 Oct 3 04:27 alert_orcl_03102017_042713.log -rw-r--r--. 1 oracle oinstall 1955 Oct 4 04:28 alert_orcl_04102017_042813.log -rw-r--r--. 1 oracle oinstall 1055 Oct 5 04:22 alert_orcl_05102017_042213.log -rw-r-----. 1 oracle oinstall 2932 Oct 6 04:28 alert_orcl.logso in alert_orcl.log file there are lines from today 00:00 until now..
#!/bin/bash export ORA_DUMP=/oracle/db/diag/rdbms/orcl/orcl/trace/ ## your path to alert.log if [ -f ${ORA_DUMP}/alert_${ORACLE_SID}.log ] then datelog=`date '+%d%m%Y_%H%M%S'` export dayn=3 ##days to preserve in your alert.log startDate=`date -d 'now - '$dayn' days' +"%a %b %d"` startLine=`grep -n "$startDate" ${ORA_DUMP}/alert_${ORACLE_SID}.log | head -1 | cut -d':' -f1` totalRows=`wc -l ${ORA_DUMP}/alert_$ORACLE_SID.log | awk '{ print $1 }'` let lineDif=$totalRows-$startLine let lineDif=$lineDif tail -${lineDif} ${ORA_DUMP}/alert_${ORACLE_SID}.log > ${ORA_DUMP}/alert_${ORACLE_SID}_tmp.log head -${startLine} ${ORA_DUMP}/alert_${ORACLE_SID}.log > ${ORA_DUMP}/alert_${ORACLE_SID}_${datelog}.log cat ${ORA_DUMP}/alert_${ORACLE_SID}_tmp.log > ${ORA_DUMP}/alert_${ORACLE_SID}.log rm ${ORA_DUMP}/alert_${ORACLE_SID}_tmp.log else echo "error: not alert log found" fi
Comments
Post a Comment