1

V$SESSION视图包含当前登录的用户列表,但它们是否存储在日志表中的某个位置或其他什么地方?

我需要获取使用 pl/sql 登录的昨天用户的列表(例如)

4

1 回答 1

4

默认情况下,Oracle 不存储此类信息。你有几个选择:

  • 启用系统事件的审核(在这种情况下为 longon\logoff)。
  • 编写系统事件(在 longon 之后,注销之前)触发器以收集该信息。

以下是示例:

SQL> show parameter audit_trail

NAME                                 TYPE        VALUE                          
------------------------------------ ----------- ------------------------------ 
audit_trail                          string      DB, EXTENDED       

SQL> select sessionid
  2       , userid
  3       , decode(action#, 100, 'logon', 101, 'logoff') 
  4    from sys.aud$ 
  5    where action# in (100, 101);

no rows selected

SQL> audit connect;

Audit succeeded.

SQL> conn hr/hr
Connected.
SQL> select sessionid
  2       , userid
  3       , decode(action#, 100, 'logon', 101, 'logoff')
  4    from sys.aud$
  5   where action# in (100, 101);

 SESSIONID USERID                         DECODE                                
---------- ------------------------------ ------                                
   1000712 HR                             logon                                 

SQL> conn hr/hr
Connected.
SQL> select sessionid
  2       , userid
  3       , decode(action#, 100, 'logon', 101, 'logoff')
  4   from sys.aud$
  5  where action# in (100, 101);

 SESSIONID USERID                         DECODE                                
---------- ------------------------------ ------                                
   1000712 HR                             logon                                 
   1000712 HR                             logoff                                
   1000713 HR                             logon   

和触发器的例子:

SQL> create table Logon_history(
  2    sessionid  number,
  3    userid     varchar2(31),
  4    logon_date timestamp,
  5    logoff_date timestamp
  6  )
  7  ;

Table created


 SQL> create or replace trigger TR_LOGON_STAT after logon
  2  on database
  3  begin
  4    insert into Logon_history(Sessionid, Userid, Logon_Date, Logoff_Date)
  5      values(sys_context('userenv', 'sessionid'), user, systimestamp, null);
  6  end;
  7  /

Trigger created

SQL> create or replace trigger TR_LOGOFF_STAT before logoff
  2  on database
  3  begin
  4    insert into Logon_history(Sessionid, Userid, Logon_Date, Logoff_Date)
  5      values(sys_context('userenv', 'sessionid'), user, null,systimestamp);
  6  end;
  7  /

Trigger created

 SQL> select sessionid
  2       , userid
  3       , logon_date
  4       , logoff_date
  5  from Logon_history
  6  ;

  no rows selected

 SQL> conn hr/hr
 Connected.

 SQL> select sessionid
  2       , userid
  3       , logon_date
  4       , logoff_date
  5  from Logon_history
  6  ;

Sessionid   Userid   Logon_Date                       Logoff_Date 
-------------------------------------------------------------------------------- 
 2490674    HR       01-NOV-12 11.46.23.421000 PM   
 2490672    HR                                     01-NOV-12 11.46.23.343000 PM 
于 2012-11-01T14:16:10.190 回答