Oracle - Audit Trail Generator?
NickName:AAA Ask DateTime:2017-10-10T01:29:22

Oracle - Audit Trail Generator?

I am looking for a generic procedure that will generate audit trails for Oracle databases. We are currently using a similar procedure on SQL Server and wondering if an Oracle equivalent exists. We are hoping the audit table will be a separate table than the original table and include user/date time info.

Here is the SQL Server equivalent we are using: https://www.codeproject.com/Articles/21068/Audit-Trail-Generator-for-Microsoft-SQL

Any advice is greatly appreciated.

Copyright Notice:Content Author:「AAA」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46651942/oracle-audit-trail-generator

Answers
J. Chomel 2017-10-15T19:15:49

If you don't want to use Oracle native mechanism, you could have your very own framework that generates and reads your own auditing table (I know you can, we had similar thing where I once worked).\n\nHere are the main components:\n\n\na_sqnc is the sequence you will use in TrackTable to keep track of the order of actions in column NO_ORD (even though there is also a D_UPD column with the modification time).\n\n\n\n\ncreate sequence a_sqnc\nminvalue 1\nmaxvalue 99999999\nstart with 1\nincrement by 1\nnocache;\n\n\n\nTrackTable will have a TABLE_NAME column in order to track changes from different tables. It also have a PK_VALUE and ROW_VALUE where we store the data that changed. Here is the table creation with useful indexes:\n\n\n\n\ncreate table TrackTable (\n table_name VARCHAR2(50) not null,\n action VARCHAR2(240) not null,\n no_ord NUMBER(12) not null,\n nature VARCHAR2(3) not null,\n pk_value VARCHAR2(4000),\n row_value VARCHAR2(4000),\n ori VARCHAR2(250),\n c_user VARCHAR2(20),\n d_upd DATE\n);\n\ncreate index AP_D_UPD on TrackTable (D_UPD);\ncreate index AP_NO_ORD on TrackTable (NO_ORD);\ncreate index AP_TABLE_NAME on TrackTable (TABLE_NAME);\n\n\n\nSay you have a simple table BANK with two columns PK_val (the primary key) and val:\n\n\n\n\ncreate table BANK (\n pk_val VARCHAR2(50) not null,\n val VARCHAR2(240) not null\n);\n\nalter table BANK\n add constraint BK_PK primary key (pk_val)\n using index ;\n\n\n\nUse DBMS_APPLICATION_INFO.READ_MODULE(w_sess_mod, w_sess_act) to know what module and what action operates: I concatenate both in column ORI in TrackTable; \nuser Oracle session variable will allow you tracking who did the change in column c_user;\nHere is how to create trigger TRCK_BNK to track changes in table BANK; it will categorize in 3 actions: DELETE, UPDATE, INSERT (you can remove the INSERT case if needed).\n\n\n\n\nCREATE OR REPLACE TRIGGER \"TRCK_BNK\" \nAFTER DELETE OR INSERT OR UPDATE \n ON BANK\nREFERENCING NEW AS NEW OLD AS OLD\nFOR EACH ROW\n\nDECLARE\n w_a VARCHAR2(10);\n W_ERRM VARCHAR2(1000);\n W_CODE VARCHAR2(1000);\n w_n VARCHAR2(200) := 'BANK';\n w_id NUMBER := a_sqnc.nextval;\n w_act v$session.action%type;\n w_mod v$session.module%type;\n w_ori TrackTable.ORI%TYPE; \nBEGIN\n DBMS_APPLICATION_INFO.READ_MODULE(w_mod, w_act);\n w_ori := 'Module : '||w_mod ||' ; Action : '||w_act;\n ----------------------------------\n -- test which action is for change\n ----------------------------------\n IF UPDATING\n THEN\n w_a := 'UPDATE';\n ELSIF DELETING\n THEN\n w_a := 'DELETE';\n ELSIF INSERTING\n THEN\n w_a := 'INSERT';\n END IF;\n ----------------------------------\n -- Insert into TrackTable \n ----------------------------------\nIf w_a in ('UPDATE', 'DELETE') then\n Insert into TrackTable \n Select w_n, w_a, w_id, 'OLD', :OLD.pk_val, :OLD.val\n , w_ori, user, sysdate\n From Dual;\nEnd if;\n\n-- if you update, there is a new value and an old value\nIf w_a in ('UPDATE', 'INSERT') then\n Insert into TrackTable \n Select w_n, w_a, w_id, 'NEW', :NEW.pk_val, :NEW.val\n , w_ori, user, sysdate\n From Dual;\nEnd if;\n\nException\nWhen others then\n Begin\n W_ERRM := SQLERRM;\n W_CODE := SQLCODE;\n -- try inserting in case of error anyway\n Insert into TrackTable \n Select w_n, w_a, -1, 'ERR', 'Grrr: '||W_CODE, W_ERRM\n , w_ori, user, sysdate\n From Dual;\n End;\nEnd;\n/\n\n\nThen add functions to your framework that generates the triggers given a table, retrieves changes, reverts table to a given date...\n\nNB:\nThis way of tracking every change on the table impairs performances if table changes a lot. But it is great for parameter tables that scarcely change.",


More about “Oracle - Audit Trail Generator?” related questions

Oracle - Audit Trail Generator?

I am looking for a generic procedure that will generate audit trails for Oracle databases. We are currently using a similar procedure on SQL Server and wondering if an Oracle equivalent exists. We ...

Show Detail

Oracle - Audit Trail

Does oracle have Audit Trail as an inbuilt functionality? Do i need to create separate table for Audit Log purpose to capture INSERT, UPDATE and DELETE changes?

Show Detail

Elasticsearch for Audit trail

I wanted to know whether elasticsearch is a good option for audit logs. I have a web application which uses Oracle Database. For each action on the tables, we are maintaining audit trail. We need t...

Show Detail

Oracle XE audit_trail not saving for all users

I enabled auditing on my Oracle XE server via the following run by the sys user: SQL> ALTER SYSTEM SET audit_sys_operations=true SCOPE=spfile; SQL> ALTER SYSTEM SET audit_trail=XML,EXTENDED ...

Show Detail

ORACLE AUDIT_TRAIL

What should be done to enable the system to observe users? I try alter system set audit_trail=db,extended scope=spfile; but when I check select * from sys.dba_audit_trail; there is no rows se...

Show Detail

Oracle - Audit Trail for a specific user

As stated on the topic , I am looking for a way for us to track on the activities of the specific user. May or may not have the SYSDBA or SYSOPER privilege. For example , HR. I would like to know...

Show Detail

Audit trail is not purged

I try to clean audit trail (my database version is 11.2.0.3.0). Everything run without errors, but audit trail is not purged. Here is what I do: 1) cleanup inicialization BEGIN IF NOT

Show Detail

Oracle 11g audit trail for specific tables

I would like to submit a question about creating an audit trail for specific tables on Oracle 11g database. We would like to track user's changes, on some specific tables, that records has been cha...

Show Detail

Audit log (trail) tampering in databases

Lets say we have a application which has a database - MySQL, SQL Server, or Oracle, for example. And let's assume that we have multiple people, like db and audit administrators, who have admin acc...

Show Detail

Oracle clean audit trails

I am trying to clean the unified audit trails using the plsql below DBMS_AUDIT_MGMT.clean_audit_trail( audit_trail_type => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED, use_last_arch_timestamp => TRUE...

Show Detail