oracle 提供了强大的审计功能,可以针对用户级,系统级范围,以及标准审计,细粒度审计等多种方式来审计各种数据库层面上的操作。
oracle 提供了强大的审计功能,可以针对用户级,系统级范围,以及标准审计,细粒度审计等多种方式来审计各种数据库层面上的操作。然很多中小型数据库需要记录用户的登陆登出信息,而又不希望牺牲太多的性能。基于这种情形,,使用基于数据库级别的触发器可以简单的实现这个需求。
1、实现代码
--创建表用于存储登陆或登出的统计信息
create table stats$user_log
(
user_id varchar2 (30),
session_id number (8),
host varchar2 (30),
last_program varchar2 (48),
last_action varchar2 (32),
last_module varchar2 (32),
logon_day date,
logon_time varchar2 (10),
logoff_day date,
logoff_time varchar2 (10),
elapsed_minutes number (8)
);
--创建登陆之后的触发器
create or replace trigger logon_audit_trigger
after logon
on database
begin
insert into stats$user_log
values (user,
sys_context ('userenv', 'sessionid'),
sys_context ('userenv', 'host'),
null,
null,
null,
sysdate,
to_char (sysdate, 'hh24:mi:ss'),
null,
null,
null);
end;
/
--创建登出之后的触发器
create or replace trigger logoff_audit_trigger
before logoff
on database
begin
-- ***************************************************
-- update the last action accessed
-- ***************************************************
update stats$user_log
set last_action =
(select action
from v$session
where sys_context ('userenv', 'sessionid') = audsid)
where sys_context ('userenv', 'sessionid') = session_id;
--***************************************************
-- update the last program accessed
-- ***************************************************
update stats$user_log
set last_program =
(select program
from v$session
where sys_context ('userenv', 'sessionid') = audsid)
where sys_context ('userenv', 'sessionid') = session_id;
-- ***************************************************
-- update the last module accessed
-- ***************************************************
update stats$user_log
set last_module =
(select module
from v$session
where sys_context ('userenv', 'sessionid') = audsid)
where sys_context ('userenv', 'sessionid') = session_id;
-- ***************************************************
-- update the logoff day
-- ***************************************************
update stats$user_log
set logoff_day = sysdate
where sys_context ('userenv', 'sessionid') = session_id;
-- ***************************************************
-- update the logoff time
-- ***************************************************
update stats$user_log
set logoff_time = to_char (sysdate, 'hh24:mi:ss')
where sys_context ('userenv', 'sessionid') = session_id;
-- ***************************************************
-- compute the elapsed minutes
-- ***************************************************
update stats$user_log
set elapsed_minutes = round ( (logoff_day - logon_day) * 1440)
where sys_context ('userenv', 'sessionid') = session_id;
end;
/
更多详情见请继续阅读下一页的精彩内容:
相关阅读:
oracle触发器的使用
oracle触发器给表自身的字段重新赋值出现ora-04091异常
oracle创建触发器调用含参数存储过程
oracle触发器查询统计本表
mysql 触发器应用案例