Record user logon information in Oracle
We can use the Oracle Audit function to record user login information, but if the Audit function is opened it will degrade Oracle performance and even cause Oracle to crash. So how can we record user login information? We can actually do this by creating a trigger. The method is as follows:
1. Log in to Oracle with SYS user
2. Create a table that records user logon information
CREATE TABLE log$information
(
ID Number (10),
USERNAME VARCHAR2 (30),
Logintime DATE,
TERMINAL VARCHAR2 (50),
Ipadress VARCHAR2 (20),
Osuser VARCHAR2 (30),
MACHINE VARCHAR2 (64),
Program VARCHAR2 (64),
SID number,
serial# number,
Ausid number
)
/
3. Create a Sequence as the primary key for the login information
CREATE SEQUENCE Login_seq
MinValue 1
MaxValue 9999999999
Start with 1
Increment by 1
Cache 20
/
4. Create triggers to record user logon information
CREATE OR REPLACE TRIGGER login_record_tr
After logon on DATABASE
DECLARE
Mtsession V$session%rowtype;
CURSOR CSession (Iiquerysid in number) is
SELECT * from V$session
WHERE audsid = Iiquerysid;
BEGIN
OPEN CSession (Userenv (' SESSIONID '));
FETCH csession into mtsession;
IF Csession%found and Sys_context (' USERENV ', ' ip_address ') is not NULL THEN
INSERT into Log$information (
Id
Username
Logintime,
Terminal
Ipadress,
Osuser,
Machine
Program
Sid
serial#,
Ausid
) VALUES (
Login_seq.nextval,
USER,
Sysdate,
Mtsession.terminal,
Sys_context (' USERENV ', ' ip_address '),
Mtsession.osuser,
Mtsession.machine,
Mtsession.program,
Mtsession.sid,
mtsession.serial#,
Userenv (' SESSIONID ')
);
End IF;
Close CSession;
EXCEPTION
When others THEN
RAISE;
End;
/