Jaggie,
(Preliminary Note: Since I did not want sample output, below, from my login-audit trigger to wrap around the screen, this post is wider than normal and you will occasionally need to scroll horizontally to read the post text.)
Terry (THoey) is actually correct. Oracle's auditing functionality carries with it a certain amount of overhead and at minimum, audits more than just login and logout. As I recall, Oracle did not invision its auditing to be "On" all the time (due to the overhead). But if someone wants to have it "on" continuously, then that is certainly their prerogative.
An advantage of a login trigger is that gathering data from your user-defined login-audit table seems much simpler than manipulating the results of turning Oracle auditing on. So the key here is that Oracle does more than "...capturing the exact same information...". It would, however, be fascinating to do a true "apples-to-apples" test of the CPU cycles for each alternative. (My money would be on the trigger, however.)
Now for Ron's request: a copy of my login trigger. First, I must mention that I originally audited/recorded each distinct login in my table (i.e., each login produced an "INSERT" into the logon-audit table). For our more "active" databases, our login table soon became one of our largest tables. For our purposes, we decided that we we could live with the earliest and the most recent logins. Therefore, as an accommodation to saving space, we now just record the most recent login (i.e., "UPDATE" the row in the log in table for a user combination that has logged in before, and "INSERT" a row for brand-new users.) It ended up saving us many dozens of megabytes over time.
There are a few components that I have in my login auditing. By no means is my method the only way. I'm certain that other Tek-Tipsters can propose alternatives.
The following sections present my "login-audit" components:
Section 1 -- Create the audit table, "LOGONS" in the SYS schema:
Code:
set echo off
set feedback on
CREATE TABLE SYS.LOGONS
(MIN_LOGIN_TIME DATE,
MAX_LOGIN_TIME DATE,
LOGIN_CNT NUMBER,
OS_USER_NAME VARCHAR2(100),
MACHINE_TERMINAL VARCHAR2(100),
ORACLE_USER_NAME VARCHAR2(30),
LATEST_SESSION_ID NUMBER,
CONSTRAINT LOGONS_OSUSER_MACHINE_OCUSER UNIQUE (OS_USER_NAME,
MACHINE_TERMINAL, ORACLE_USER_NAME));
Section 2 -- My user-defined function to gather session information. (There are certainly other built-in functions available; I just like to use my own in this case.):
Code:
create or replace function sys.get_session_info
(sess_id in number
, info_type in varchar2) return varchar2
is
w_osuser varchar2(100);
w_machine varchar2(100);
w_user varchar2(100);
begin
for i in (Select osuser, machine, user into
w_osuser, w_machine, w_user
from v$session
where audsid = sess_id
order by logon_time desc) loop
if upper(info_type) = 'OSUSER' then
return w_osuser;
elsif upper(info_type) = 'MACHINE' then
return w_machine;
elsif upper(info_type) = 'USER' then
return w_user;
end if;
end loop;
end;
/* Above function accesses session information */
/
Section 3 -- Actual Logon trigger code:
[code]
CREATE OR REPLACE TRIGGER sys.logontrigger
AFTER LOGON ON DATABASE
DECLARE
cnt_hold number;
BEGIN
select count(*) into cnt_hold
from sys.logons
where OS_USER_NAME = get_session_info(userenv('sessionid'),'OSUSER')
and MACHINE_TERMINAL = get_session_info(userenv('sessionid'),'MACHINE')
and ORACLE_USER_NAME = get_session_info(userenv('sessionid'),'USER');
if cnt_hold = 0 and get_session_info(userenv('sessionid'),'USER') is not null then
INSERT INTO sys.logons
(MIN_LOGIN_TIME
,MAX_LOGIN_TIME
,Login_cnt
,OS_USER_NAME
,MACHINE_TERMINAL
,ORACLE_USER_NAME
,LATEST_SESSION_ID
)
VALUES
(sysdate
,sysdate
,1
,get_session_info(userenv('sessionid'),'OSUSER')
,get_session_info(userenv('sessionid'),'MACHINE')
,get_session_info(userenv('sessionid'),'USER')
,userenv('sessionid'));
else
update sys.logons set
MAX_LOGIN_TIME = sysdate
,login_cnt = login_cnt+1
,LATEST_SESSION_ID = userenv('sessionid')
where OS_USER_NAME = get_session_info(userenv('sessionid'),'OSUSER')
and MACHINE_TERMINAL = get_session_info(userenv('sessionid'),'MACHINE')
and ORACLE_USER_NAME = get_session_info(userenv('sessionid'),'USER');
end if;
END;
/* Above trigger audits most recent database login for an O/S-Oracle user combination */
/
Section 4 -- SQL*Plus script to display the contents of the SYS.LOGONS table:
Code:
REM ***************************************************************
REM Script produces formatted report of logins, sorted by date/time.
REM ***************************************************************
col a heading "Earliest Login Date/Time" format a26
col a2 heading "Latest Login Date/Time" format a26
col b heading "Latest|Oracle|Session|ID" format 99999999
col c heading "OS User Login Name" format a20
col d heading "Login Machine/|Terminal Name" format a25
col e heading "Oracle User Name" format a20
col f heading "Number|of|Logins" format 9,999,999
set linesize 200
select to_char(min_login_time,'YYYY-Mon-DD (Dy) hh24:mi:ss') a
,to_char(max_login_time,'YYYY-Mon-DD (Dy) hh24:mi:ss') a2
,login_cnt f
,Latest_Session_ID b
,os_user_name c
,machine_terminal d
,Oracle_user_name e
from sys.logons
order by max_login_time
/
Section 5 -- Sample Output from "LogonReport.sql":
Code:
Latest
Number Oracle
of Session Login Machine/
Earliest Login Date/Time Latest Login Date/Time Logins ID OS User Login Name Terminal Name Oracle User Name
-------------------------- -------------------------- ---------- --------- -------------------- ------------------------- ----------------
2003-Nov-04 (Tue) 01:50:10 2003-Nov-04 (Tue) 01:59:23 4 454 Dhunt MOBILEPC2709 SYSTEM
2003-Oct-30 (Thu) 16:04:55 2003-Nov-11 (Tue) 02:39:15 34 646 Dhunt MOBILEPC2709 TEST2
2004-Feb-27 (Fri) 09:43:15 2004-Feb-27 (Fri) 09:43:15 1 1265 Dhunt MOBILEPC2709 YADA3
2004-Mar-04 (Thu) 13:26:06 2004-Mar-04 (Thu) 13:26:06 1 1333 dhunt MOBILEPC2709 DHUNT
2004-Apr-27 (Tue) 09:06:43 2004-Apr-27 (Tue) 09:06:43 1 1838 Dhunt MOBILEPC2709 DONTDROP
2004-Jun-02 (Wed) 12:38:05 2004-Jun-02 (Wed) 12:48:11 6 2124 Dhunt MOBILEPC2709 RAY
2003-Oct-15 (Wed) 22:58:01 2004-Jul-08 (Thu) 12:55:44 114 2429 Dhunt MOBILEPC2709 TEST
2003-Oct-14 (Tue) 18:11:10 2004-Jul-10 (Sat) 03:39:44 854 2436 Dhunt MOBILEPC2709 DHUNT
8 rows selected.
******************************************************************************************************************************************
Let me know, Ron, if this is what you wanted.
![[santa] [santa] [santa]](/data/assets/smilies/santa.gif)
Mufasa
(aka Dave of Sandy, Utah, USA @ 17:39 (13Jul04) UTC (aka "GMT" and "Zulu"), 10:39 (13Jul04) Mountain Time)