PurgeLog: Oracle Logs and Traces Housekeeping
Tags: oracle, housekeeping, log, tutorial
Published: 2021-01-18 12:14:12


This recent post by [Michael Dinh](https://blog.pythian.com/author/dinh/) for [pythian.com](https://twitter.com/Pythian) - [HOW TO EASILY PURGE LOGS FOR GI AND DB](https://blog.pythian.com/how-to-easily-purge-logs-for-gi-and-db/) reminded me that I have never posted anything about this great tool for housekeeping.

I actually discovered it after a customer requested more information about it, and couldn't be more grateful about it since is a very easy and powerful tool.

If you check [MOS Doc 2081655.1](https://support.oracle.com/epmos/faces/DocumentDisplay?&id=2081655.1) you will see all explained, including how to set it up and more importantly, the Script Download link [purgeLogs 20201221 - Revision: 1.65](https://support.oracle.com/epmos/main/downloadattachmentprocessor?attachid=2081655.1%3APURGELOGS&docType=WHITE%20PAPER&action=download)


![purgeLogs Doc ID 2081655.1](https://dev-to-uploads.s3.amazonaws.com/i/vc3lkiqv8gv0q5eazk24.png)


Something I dint see mentioned there, is that it also runs in Solaris, just in case you were wondering.

The basic configuration is quite simple, but you can add options to cleanup Audit files (both from ASM and DB) and also TFA/AHF and OSWatcher logs, which is great.

This is what I normally run in one of my Home Lab Servers (I don't normally have the boxes running for more than few hours, don't have any cronjob created for them)
```bash
[root@rac1-node1 ~]# /opt/utils/purgeLogs -orcl 7 -aud -lsnr -tfa 15 -osw 15
```

This will cleanup all Oracle (DB and GI) Logs and Traces including Audit and Listener older than 7 days, and will also cleanup TFA/AHF and OSWatcher files for last 15

The Audit files period goes connected to the "-orcl" attribute, so if you want to have different periods for them, you will need a separate command, but still very easy to do.
In this example, we cleanup Oracle (DB and GI) Logs and Traces including Audit and Listener older than 15 days and then we can do a second run where we will only cleanup Oracle (DB and GI) Logs and Traces older than 7 days 
```bash
/opt/utils/purgeLogs -orcl 15 -aud -lsnr 
/opt/utils/purgeLogs -orcl 7
```
Let's try it out.

Let's see some of the logs/trace location to compare after the execution. See how the listener log has information from Dec 2020 and trace/audit locations have files from same time as well:
```bash
[root@rac1-node1 ~]# head /u01/app/grid/diag/tnslsnr/rac1-node1/listener/trace/listener.log
2020-12-12T13:21:51.456851+00:00
12-DEC-2020 13:21:51 * (CONNECT_DATA=(CID=(PROGRAM=)(HOST=rac1-node1.raclab.local)(USER=oracle))(COMMAND=status)(ARGUMENTS=64)(SERVICE=LISTENER)(VERSION=318767104)) * status * 0
2020-12-12T13:22:27.888635+00:00
12-DEC-2020 13:22:27 * ping * 0
12-DEC-2020 13:22:27 * (CONNECT_DATA=(CID=(PROGRAM=)(HOST=rac1-node2.raclab.local)(USER=oracle))(COMMAND=status)(ARGUMENTS=64)(SERVICE=(ADDRESS=(PROTOCOL=TCP)(HOST=rac1-node1.raclab.local)(PORT=1521)))(VERSION=318767104)) * status * 1189
TNS-01189: The listener could not authenticate the user
2020-12-12T13:22:51.461060+00:00
12-DEC-2020 13:22:51 * (CONNECT_DATA=(CID=(PROGRAM=)(HOST=rac1-node1.raclab.local)(USER=oracle))(COMMAND=status)(ARGUMENTS=64)(SERVICE=LISTENER)(VERSION=318767104)) * status * 0
2020-12-12T13:23:51.459288+00:00

[root@rac1-node1 ~]# ls -lthr /u01/app/oracle/diag/rdbms/db112/db1121/trace/*.tr* |head -5
total 29M
-rw-r----- 1 oracle oinstall  152 Dec 11 14:04 db1121_ora_10103.trm
-rw-r----- 1 oracle oinstall 2.8K Dec 11 14:04 db1121_ora_10103.trc
-rw-r----- 1 oracle oinstall   62 Dec 11 14:04 db1121_mark_10214.trm
-rw-r----- 1 oracle oinstall  912 Dec 11 14:04 db1121_mark_10214.trc

[root@rac1-node1 ~]# ls -lrth /u01/app/19.3.0/grid/rdbms/audit/ |head -5
total 12M
-rw-r----- 1 oracle oinstall  838 Dec 11 14:03 +ASM1_ora_7437_20201211140332383484588514.aud
-rw-r----- 1 oracle oinstall  899 Dec 11 14:03 +ASM1_ora_7437_20201211140346459666144005.aud
-rw-r----- 1 oracle oinstall  905 Dec 11 14:03 +ASM1_ora_7688_20201211140346542256454593.aud
-rw-r----- 1 oracle oinstall  905 Dec 11 14:03 +ASM1_ora_7770_20201211140358694624901360.aud
[root@rac1-node1 ~]#
```
Let's now run the command, which in this case will cleanup anything older than 1 day (output truncated since it was too long)
```bash
[root@rac1-node1 ~]# /opt/utils/purgeLogs -orcl 1 -aud -lsnr

│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│
 purgeLogs version: 1.64
 Copyright (c) 2015-2020 Oracle and/or its affiliates.
 -------------------------------------------------------
 Author: Ruggero Citton <ruggero.citton@oracle.com>
 RAC Pack, Cloud Innovation and Solution Engineering Team
│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│

2021-01-16 13:47:10: I Cleaning up Listener logs
2021-01-16 13:47:10: I ... purging Listener logs, ORACLE_BASE is '/u01/app/grid'
2021-01-16 13:47:10: I ... purging SCAN1 Listener logs, ORACLE_BASE is '/u01/app/grid'
2021-01-16 13:47:10: I ... purging SCAN2 Listener logs, ORACLE_BASE is '/u01/app/grid'
2021-01-16 13:47:10: I ... purging SCAN3 Listener logs, ORACLE_BASE is '/u01/app/grid'
2021-01-16 13:47:10: I ... purging Management Listener logs, ORACLE_BASE is '/u01/app/grid'
2021-01-16 13:47:10: W Path /u01/app/grid/diag/tnslsnr/rac1-node1/mgmtlsnr/trace does not exist, Management Listener logs cleanup skipped...
2021-01-16 13:47:10: I ... purging ASM Listener logs, ORACLE_BASE is '/u01/app/grid'
2021-01-16 13:47:10: I GI Audit logs purge started
2021-01-16 13:47:10: I ... purging GI Audit logs under '/u01/app/19.3.0/grid/rdbms/audit' older then 1 days
[....]
2021-01-16 13:47:46: I adrci RDBMS purging diagnostic destination diag/rdbms/cdb19/cdb191
2021-01-16 13:47:46: I ... purging ALERT older than 1 days
2021-01-16 13:47:47: I ... purging INCIDENT older than 1 days
2021-01-16 13:47:47: I ... purging TRACE older than 1 days
2021-01-16 13:47:47: I ... purging CDUMP older than 1 days
2021-01-16 13:47:47: I ... purging HM older than 1 days
2021-01-16 13:47:47: I ... purging STAGE older than 1 days
2021-01-16 13:47:47: I ... purging SWEEP older than 1 days
2021-01-16 13:47:47: I ... purging UTSCDMP older than 1 days
2021-01-16 13:47:47: I Getting dbhome for 'db112'
2021-01-16 13:47:51: I adrci RDBMS purging diagnostic destination diag/rdbms/db112/db1121
2021-01-16 13:47:51: I ... purging ALERT older than 1 days
2021-01-16 13:47:51: I ... purging INCIDENT older than 1 days
[....]
2021-01-16 13:48:33: I Purging RDBMS Audit logs on '/u01/app/oracle/admin/db19/adump' older then 1 days
2021-01-16 13:48:33: I Purging RDBMS Audit logs on '/u01/app/oracle/admin/db112/adump' older then 1 days
2021-01-16 13:48:33: I Purging RDBMS Audit logs on '/u01/app/oracle/admin/cdb121/adump' older then 1 days
2021-01-16 13:48:33: S Purging completed succesfully!
[root@rac1-node1 ~]#
```

See how the script truncated the listener.log file of any old records, the same that did with audit files and trace files (no trace files in that location since I didn't start the DB recently)
```bash
[root@rac1-node1 ~]# head /u01/app/grid/diag/tnslsnr/rac1-node1/listener/trace/listener.log

2021-01-16T13:47:48.094559+00:00
16-JAN-2021 13:47:48 * (CONNECT_DATA=(CID=(PROGRAM=)(HOST=rac1-node1.raclab.local)(USER=oracle))(COMMAND=status)(ARGUMENTS=64)(SERVICE=LISTENER)(VERSION=318767104)) * status * 0
2021-01-16T13:48:48.091847+00:00
16-JAN-2021 13:48:48 * (CONNECT_DATA=(CID=(PROGRAM=)(HOST=rac1-node1.raclab.local)(USER=oracle))(COMMAND=status)(ARGUMENTS=64)(SERVICE=LISTENER)(VERSION=318767104)) * status * 0
[root@rac1-node1 ~]# ls -lthr /u01/app/oracle/diag/rdbms/db112/db1121/trace/*.tr* |head -5
ls: cannot access /u01/app/oracle/diag/rdbms/db112/db1121/trace/*.tr*: No such file or directory
[root@rac1-node1 ~]#
[root@rac1-node1 ~]# ls -lrth /u01/app/19.3.0/grid/rdbms/audit/ |head -5
total 276K
-rw-r----- 1 oracle oinstall 838 Jan 16 12:10 +ASM1_ora_7207_20210116121000402108307709.aud
-rw-r----- 1 oracle oinstall 899 Jan 16 12:10 +ASM1_ora_7207_20210116121014786453494743.aud
-rw-r----- 1 oracle oinstall 905 Jan 16 12:10 +ASM1_ora_7449_20210116121014874778755693.aud
-rw-r----- 1 oracle oinstall 905 Jan 16 12:10 +ASM1_ora_7579_20210116121028185252731961.aud
[root@rac1-node1 ~]#
```

To get it executed regularly, you can just add this to root cron of every host:
```bash
0 0 * * MON /opt/utils/purgeLogs -orcl 15 -aud -lsnr -tfa 15 -osw 15
```

You also have a dry-run option, just in case you want to check it properly first and avoid nasty surprises
```bash
[root@rac1-node1 ~]# /opt/utils/purgeLogs -dryrun -orcl 7 -aud -lsnr -tfa 15 -osw 15

│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│
 purgeLogs version: 1.64
 Copyright (c) 2015-2020 Oracle and/or its affiliates.
 -------------------------------------------------------
 Author: Ruggero Citton <ruggero.citton@oracle.com>
 RAC Pack, Cloud Innovation and Solution Engineering Team
│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│

2021-01-16 13:15:52: I DRYRUN - Cleaning up Listener logs, ORACLE_BASE is '/u01/app/grid'
        echo "" >| /u01/app/grid/diag/tnslsnr/rac1-node1/listener/trace/listener.log
2021-01-16 13:15:52: I DRYRUN - Cleaning up SCAN1 Listener logs, ORACLE_BASE is '/u01/app/grid'
        echo "" >| /u01/app/grid/diag/tnslsnr/rac1-node1/listener_scan1/trace/listener_scan1.log
2021-01-16 13:15:52: I DRYRUN - Cleaning up SCAN2 Listener logs, ORACLE_BASE is '/u01/app/grid'
        echo "" >| /u01/app/grid/diag/tnslsnr/rac1-node1/listener_scan2/trace/listener_scan2.log
2021-01-16 13:15:52: I DRYRUN - Cleaning up SCAN3 Listener logs, ORACLE_BASE is '/u01/app/grid'
[....]
2021-01-16 13:15:52: I GI Audit logs purge started
2021-01-16 13:15:52: I DRYRUN - Purging GI Audit logs under '/u01/app/19.3.0/grid/rdbms/audit' older then 7 days
        rm /u01/app/19.3.0/grid/rdbms/audit/+ASM1_ora_28136_20201212150856457826030998.aud
        rm /u01/app/19.3.0/grid/rdbms/audit/+ASM1_ora_2857_20201212133503423553983140.aud
        rm /u01/app/19.3.0/grid/rdbms/audit/+ASM1_ora_12878_20201217133522936627859143.aud
        rm /u01/app/19.3.0/grid/rdbms/audit/+ASM1_ora_26992_20201217112131382742482339.aud
[....]
        rm /u01/app/oracle/admin/db112/adump/db1121_ora_26323_20210105140349071050143795.aud
2021-01-16 13:17:05: I DRYRUN - Purging RDBMS Audit logs on '/u01/app/oracle/admin/cdb121/adump' older than 7 days

~~~~~~~~~~~~~~~~~TFA purge started~~~~~~~~~~~~~~~~~
2021-01-16 13:17:05: I DRYRUN - Purging TFA archives older then 15 days
        /opt/oracle.ahf/tfa/bin/tfactl purge -older 15d -force
2021-01-16 13:17:05: I DRYRUN - Purging ODA OSW archives older then 15 days
2021-01-16 13:17:05: W Path /opt/oracle/oak/oswbb/archive does not exist, ODA OSW archive cleanup skipped...
2021-01-16 13:17:05: I DRYRUN - Purging ExaWatcher archives older then 15 days
2021-01-16 13:17:05: W Path /opt/oracle.ExaWatcher/archive does not exist, ExaWatcher archive cleanup skipped...
[root@rac1-node1 ~]#
```

There are some issues you can find with this system, like the script deleting files from the default trace/log directories just because they are older than the period we chosen. 
Won't be first time someone selected the Database Diagnostic Location to drop them some important Export or something like that :)

To show you this, I moved an old trace to a directory and just changed its name to see how the script doesn't only purge files with ".log/.trc/.trm/.aud..." extensions
```bash
[root@rac1-node1 ~]# cp -pr /u01/app/oracle/diag/rdbms/db112/db1121/trace/db1121_ora_10103.trc /tmp/test/very_important_file
[root@rac1-node1 ~]# ls -lrth /tmp/test/
total 4.0K
-rw-r----- 1 oracle oinstall 2.8K Dec 11 14:04 very_important_file
[root@rac1-node1 ~]#
```

Since PurgeLog allow us to select a extra directory for cleaning up, we run the command with that option to see the results directly:
```bash
[root@rac1-node1 ~]# /opt/utils/purgeLogs -extra /tmp/test:5

│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│
 purgeLogs version: 1.64
 Copyright (c) 2015-2020 Oracle and/or its affiliates.
 -------------------------------------------------------
 Author: Ruggero Citton <ruggero.citton@oracle.com>
 RAC Pack, Cloud Innovation and Solution Engineering Team
│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│


~~~~~~~~~~~~~~~~~~~~~~Extra purge started~~~~~~~~~~~~~~~~~~~~~~
2021-01-16 13:56:05: I Purging files older then 5 days on user specified folder '/tmp/test'
2021-01-16 13:56:05: S Purging completed succesfully!
[root@rac1-node1 ~]#
[root@rac1-node1 ~]# ls -lrth /tmp/test/
total 0
[root@rac1-node1 ~]#
```

Remember, *with great power comes great responsibility* 
As mentioned in the Documentation, make sure you are not leaving anything that doesn't belong to the default Oracle Diagnostic Location, or you will see it disappear :)