|
|

楼主 |
发表于 2006/7/3 16:56:14
|
显示全部楼层
- Auditing LOGOFF and LOGON
- This tip comes from Milind Deobhankar, Oracle Software Engineer in
- Ahmedabad, Gujarat, India.
- Auditing the database to find out who is logged on, who has logged off,
- from which machines users are logged in, and from which program users are
- connected can be a very cumbersome task. Here is a procedure that will
- help the DBA to trace who has logged on, logged off, and at what times.
- First log on as the sys user (as this task is of the database
- administrator).
- Create the table in which all information will be stored.
- create table
- SYSTEM.login_info
- ( logindatetime date,
- LogoffDateTime Date,
- LoggedUser Varchar2(30),
- SESSIONID Number,
- TERMINAL varchar2(20),
- PROGRAM varchar2(20)
- );
- /
- Now create the trigger on the logon event so that whenever anybody logs
- on, the trigger will fire.
- CREATE OR REPLACE TRIGGER logon_audit AFTER LOGON ON DATABASE
- BEGIN
- insert into system.login_info
- values(sysdate,null,user,sys_context('USERENV','SESSIONID'),sys_context(
- 'USERENV','TERMINAL'),null);
- END;
- /
- Now create the trigger on the logoff event so that whenever anybody logs
- off, the trigger will fire.
- CREATE OR REPLACE TRIGGER logoff_audit Before LOGOFF ON DATABASE
- BEGIN
- insert into system.login_info
- select
- '',sysdate,user,sys_context('USERENV','SESSIONID'),sys_context('USERENV'
- ,'TERMINAL'), program from v$session where logon_time = (select
- logindatetime from SYSTEM.login_info where SESSIONID=
- sys_context('USERENV','SESSIONID'));
- END;
- /
- Shutdown the database, restart it, and log on as SYS. There will be values
- inserted in the table. Now retrieve the value from the table.
- select to_char(logindatetime,'DD-MON-YYYY
- hh:mi:ss')LOGIN,to_char(LogoffDateTime,'DD-MON-YYYY hh:mi:ss')LOGOFF,
- SESSIONID,
- TERMINAL,
- PROGRAM
- from SYSTEM.login_info
- group by SESSIONID,logindatetime,LogoffDateTime,TERMINAL,PROGRAM;
- /
- Alternatively, you can also develop a PL/SQL procedure to do the same
- thing.
- create or replace procedure traceusers
- as
- CURSOR BROWSER IS
- select to_char(logindatetime,'DD-MON-YYYY
- hh:mi:ss')LOGIN,to_char(LogoffDateTime,'DD-MON-YYYY hh:mi:ss')LOGOFF,
- SESSIONID,
- TERMINAL,
- PROGRAM
- from SYSTEM.login_info
- group by SESSIONID,logindatetime,LogoffDateTime,TERMINAL,PROGRAM;
- BROWSEIT BROWSER%ROWTYPE;
- begin
- OPEN BROWSER;
- LOOP
- FETCH BROWSER INTO BROWSEIT;
- EXIT WHEN BROWSER%NOTFOUND;
- dbms_output.put_line('Logged In: '||BROWSEIT.LOGIN ||' Logged Off: '||
- BROWSEIT.LOGOFF || ' SessionID: '||BROWSEIT.SESSIONID||'
- Terminal:'||BROWSEIT.TERMINAL||' Program: '||BROWSEIT.PROGRAM);
- END LOOP;
- CLOSE BROWSER;
- end;
- /
复制代码 |
|