0% found this document useful (0 votes)
64 views2 pages

Document 70679.1

This document provides instructions on how to audit logon and logoff events in Oracle databases using PL/SQL triggers. It details the creation of a table to capture login/logout timings and the implementation of logon and logoff triggers to insert relevant data into this table. The document is applicable to various Oracle Database versions and includes sample SQL code for implementation.

Uploaded by

kushika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views2 pages

Document 70679.1

This document provides instructions on how to audit logon and logoff events in Oracle databases using PL/SQL triggers. It details the creation of a table to capture login/logout timings and the implementation of logon and logoff triggers to insert relevant data into this table. The document is applicable to various Oracle Database versions and includes sample SQL code for implementation.

Uploaded by

kushika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

16/4/25, 11:20 Document 70679.

1
Copy right (c) 2025, Oracle. A ll rights reserv ed. Oracle Confidential.

How to Audit Logon/Logoff Events with Triggers (Doc ID 70679.1)

APPLIES TO:

Oracle Database Cloud Service - Version N/A and later


Oracle Database - Enterprise Edition - Version 8.1.7.4 to 11.1.0.8 [Release 8.1.7 to 11.1]
Oracle Database Cloud Schema Service - Version N/A and later
Gen 1 Exadata Cloud at Customer (Oracle Exadata Database Cloud Machine) - Version N/A and later
Oracle Cloud Infrastructure - Database Service - Version N/A and later
Information in this document applies to any platform.

PURPOSE

This article contains PL/SQL code that demonstrates how to audit logon/logoff information using the Oracle 8i new logon
trigger and logoff trigger.

SCOPE

To get users login and logout time use following steps.

DETAILS

Steps:

1. Create the table to capture the LOGIN and LOGOUT timings.

CREATE TABLE
(
event VARCHAR2(10),
sid NUMBER,
serial# NUMBER,
timestamp DATE,
username VARCHAR2(30),
osuserid VARCHAR2(30),
machinename VARCHAR2(64)
)
/

2. Create LOGON trigger at DATABASE level.

CREATE OR REPLACE TRIGGER AFTER LOGON ON database


DECLARE
machinename VARCHAR2(64);
osuserid VARCHAR2(30);
v_sid NUMBER(10);
v_serial NUMBER(10);

CURSOR c1 IS
SELECT sid, serial#, osuser, machine
FROM v$session WHERE audsid = userenv('sessionid');
BEGIN
OPEN c1;
FETCH c1 INTO v_sid, v_serial, osuserid, machinename;

INSERT INTO VALUES ( 'LOGON', v_sid, v_serial, sysdate,


user, osuserid, machinename );

CLOSE c1;
END;
/
https://support.oracle.com/epmos/faces/DocumentDisplay?_adf.ctrl-state=19dm1dtidg_260&id=70679.1 1/2
16/4/25, 11:20 Document 70679.1
3. Create LOGOFF trigger

CREATE OR REPLACE TRIGGER


BEFORE LOGOFF ON database
DECLARE
machinename VARCHAR2(64);
osuserid VARCHAR2(30);
v_sid NUMBER(10);
v_serial NUMBER(10);

CURSOR c1 IS
SELECT sid, serial#, osuser, machine
FROM v$session WHERE audsid = userenv('sessionid');
BEGIN
OPEN c1;
FETCH c1 INTO v_sid, v_serial, osuserid, machinename;

INSERT INTO logonaudittable VALUES ( 'LOGOFF', v_sid, v_serial, sysdate,


user, osuserid, machinename );

CLOSE c1;
END;
/

Check the result:

select event, sid, serial#, username, osuserid,


to_char(timestamp,'dd-mon-yyyy hh24:mi:ss') as TIMESTAMP, machinename
from logonaudittable;

Sample result:

EVENT SID SERIAL# USERNAME OSUSERID TIMESTAMP MACHINENAME


------ --- ------- -------- -------- -------------------- ---------------
LOGON 11 7

Additional Search Words:

PLSQL AUDITING
LOGON TRIGGER

Related Documents:

Oracle8i Concepts
Chapter "Triggers"
Oracle9i Application Developer's Guide - Fundamentals
Chapter "Working With System Events"

Didn't find what you are looking for?

https://support.oracle.com/epmos/faces/DocumentDisplay?_adf.ctrl-state=19dm1dtidg_260&id=70679.1 2/2

You might also like