简体   繁体   English

Oracle - Audit Trail Generator?

[英]Oracle - Audit Trail Generator?

I am looking for a generic procedure that will generate audit trails for Oracle databases. 我正在寻找一个通用程序,它将为Oracle数据库生成审计跟踪。 We are currently using a similar procedure on SQL Server and wondering if an Oracle equivalent exists. 我们目前在SQL Server上使用类似的过程,并想知道是否存在Oracle等效项。 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 以下是我们使用的SQL Server等效项: https//www.codeproject.com/Articles/21068/Audit-Trail-Generator-for-Microsoft-SQL

Any advice is greatly appreciated. 任何意见是极大的赞赏。

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). 如果您不想使用Oracle本机机制,您可以拥有自己的框架来生成和读取您自己的审计表(我知道您可以,我曾经有过类似的工作)。

Here are the main components: 以下是主要组件:

  • a_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). a_sqnc是您将在TrackTable使用的序列,用于跟踪列NO_ORD的操作顺序(即使还有一个带有修改时间的D_UPD列)。

create sequence a_sqnc
minvalue 1
maxvalue 99999999
start with 1
increment by 1
nocache;
  • TrackTable will have a TABLE_NAME column in order to track changes from different tables. TrackTable将具有TABLE_NAME列,以便跟踪来自不同表的更改。 It also have a PK_VALUE and ROW_VALUE where we store the data that changed. 它还有一个PK_VALUEROW_VALUE ,我们存储更改的数据。 Here is the table creation with useful indexes: 这是使用有用索引创建的表:

create table TrackTable (
  table_name VARCHAR2(50) not null,
  action     VARCHAR2(240) not null,
  no_ord     NUMBER(12) not null,
  nature     VARCHAR2(3) not null,
  pk_value   VARCHAR2(4000),
  row_value  VARCHAR2(4000),
  ori        VARCHAR2(250),
  c_user     VARCHAR2(20),
  d_upd      DATE
);

create index AP_D_UPD on TrackTable (D_UPD);
create index AP_NO_ORD on TrackTable (NO_ORD);
create index AP_TABLE_NAME on TrackTable (TABLE_NAME);
  • Say you have a simple table BANK with two columns PK_val (the primary key) and val : 假设您有一个简单的表BANK其中包含两列PK_val (主键)和val

create table BANK (
  pk_val VARCHAR2(50) not null,
  val    VARCHAR2(240) not null
);

alter table BANK
  add constraint BK_PK primary key (pk_val)
  using index ;
  • Use 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 ; 使用DBMS_APPLICATION_INFO.READ_MODULE(w_sess_mod, w_sess_act)来了解哪个模块和操作是什么操作:我在TrackTableORITrackTable ;

  • user Oracle session variable will allow you tracking who did the change in column c_user ; user Oracle会话变量将允许您跟踪谁在列c_user进行了更改;

  • Here is how to create trigger TRCK_BNK to track changes in table BANK ; 以下是如何创建触发器TRCK_BNK来跟踪表BANK变化; it will categorize in 3 actions: DELETE , UPDATE , INSERT (you can remove the INSERT case if needed). 它将分为3个动作: DELETEUPDATEINSERT (如果需要,你可以删除INSERT案例)。


CREATE OR REPLACE TRIGGER "TRCK_BNK" 
AFTER DELETE OR INSERT OR UPDATE 
   ON BANK
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW

DECLARE
    w_a        VARCHAR2(10);
    W_ERRM     VARCHAR2(1000);
    W_CODE     VARCHAR2(1000);
    w_n        VARCHAR2(200) := 'BANK';
    w_id       NUMBER :=  a_sqnc.nextval;
    w_act      v$session.action%type;
    w_mod      v$session.module%type;
    w_ori      TrackTable.ORI%TYPE;    
BEGIN
  DBMS_APPLICATION_INFO.READ_MODULE(w_mod, w_act);
  w_ori := 'Module : '||w_mod ||' ; Action : '||w_act;
  ----------------------------------
  -- test which action is for change
  ----------------------------------
  IF UPDATING
  THEN
    w_a := 'UPDATE';
  ELSIF DELETING
  THEN
    w_a := 'DELETE';
  ELSIF INSERTING
  THEN
    w_a := 'INSERT';
  END IF;
  ----------------------------------
  -- Insert into TrackTable 
  ----------------------------------
If w_a in ('UPDATE', 'DELETE') then
  Insert into TrackTable 
       Select w_n, w_a, w_id, 'OLD', :OLD.pk_val, :OLD.val
            , w_ori, user, sysdate
         From Dual;
End if;

-- if you update, there is a new value and an old value
If w_a in ('UPDATE', 'INSERT') then
  Insert into TrackTable 
       Select w_n, w_a, w_id, 'NEW', :NEW.pk_val, :NEW.val
            , w_ori, user, sysdate
         From Dual;
End if;

Exception
When others then
  Begin
    W_ERRM := SQLERRM;
    W_CODE := SQLCODE;
    -- try inserting in case of error anyway
    Insert into TrackTable 
         Select w_n, w_a, -1, 'ERR', 'Grrr: '||W_CODE, W_ERRM
              , w_ori, user, sysdate
     From Dual;
  End;
End;
/

Then add functions to your framework that generates the triggers given a table, retrieves changes, reverts table to a given date... 然后向框架添加函数,生成给定表的触发器,检索更改,将表还原到给定日期...

NB: This 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. 但是对于几乎没有变化的参数表来说非常棒。

Have a look at Oracles Flashback Data Archive which bases upon the UNDO Data. 看看基于UNDO数据的Oracles 闪回数据存档 It can be configured to track any change to your data. 它可以配置为跟踪对数据的任何更改。 It is available in any edition of oracle since 11g2 (11.2.0.4). 它自11g2(11.2.0.4)以来可用于任何版本的oracle。 In the Oracle documentation it says that optimazation is limited but basic functionality is available in any edition. Oracle文档中,它表示最佳化是有限的,但任何版本都可以使用基本功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM