以下两个过程呢,其实是辅助过程,一个是完成写日志,一个是完成发邮件,在以后的程序中,可能会经常用到。
1、写日志过程
name:sp_Write_log
parameter:textContext in varchar2日志内容
create date:2003-06-01
creater:chen jiping
desc: •写日志,把内容记到服务器指定目录下
•必须配置Utl_file_dir初始化参数,并保证日志路径与Utl_file_dir路径一致或者是其中一个
create or replace PROCEDURE sp_Write_log(textContext VARCHAR2)
IS
file_handleUTL_FILE.file_type;
Write_contentVARCHAR2(1024);
Write_file_nameVARCHAR2(50);
BEGIN
--打开文件
--Write_file_name := rtrim(to_char(SYSDATE,'YYYY-MM-DD'))'.log';
Write_file_name := 'db108_alert.log';
file_handle := UTL_FILE.FOPEN('/u01/prodUCt/admin/ora81/logs',Write_file_name,'a');
Write_content := to_char(SYSDATE,'yyyy-mm-dd hh24:mi:ss')''textContext;
--写文件
IF UTL_FILE.IS_OPEN(file_handle) THEN
UTL_FILE.PUT_LINE(file_handle,Write_content);
END IF;
--关闭文件
UTL_FILE.Fclose(file_handle);
EXCEPTION
WHEN OTHERS THEN
IF UTL_FILE.IS_OPEN(file_handle) THEN
UTL_FILE.Fclose(file_handle);
END IF;
END sp_Write_log;
2、发送Email的过程
name:sp_Send_mail
parameter: Rcpter in varchar2 接收者邮箱
Mail_Content in Varchar2 邮件内容
create date:2003-06-01
creater:chen jiping
desc: •发送邮件到指定邮箱
•只能指定一个邮箱,假如需要发送到多个邮箱,需要另外的辅助程序
create or replace procedure sp_send_mail(
Rcpter IN VARCHAR2,
Mail_Content IN VARCHAR2)
IS
conn utl_smtp.connection;
PROCEDURE send_header(NAME IN VARCHAR2, header IN VARCHAR2) AS
BEGIN
utl_smtp.write_data(conn, NAME ': ' header utl_tcp.CRLF);
END;
BEGIN
conn := utl_smtp.open_connection('smtp.ur.net.cn');
utl_smtp.helo(conn, 'Oracle');
utl_smtp.mail(conn, 'oracle info');
utl_smtp.rcpt(conn, Rcpter);
utl_smtp.open_data(conn);
send_header('From','Oracle Database');
send_header('To','"Recipient" <'Rcpter'>');
send_header('Subject', 'Hello');
utl_smtp.write_data(conn, utl_tcp.CRLF Mail_Content);
utl_smtp.close_data(conn);
utl_smtp.quit(conn);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
BEGIN
utl_smtp.quit(conn);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
NULL; -- When the SMTP server is down or unavailable, we don't have
-- a connection to the server. The quit call will raise an
-- exception that we can ignore.
END;
raise_application_error(-20000,
'Failed to send mail due to the following error: ' SQLERRM);
END sp_send_mail;
3、监控数据库关闭/启动的触发器
create or replace trigger TR_DB_SHUTDOWN
before shutdown ON DATABASE
DECLARE
msMsg VARCHAR2(500);
BEGIN
msMsg :='user 'ora_login_user' in 'ora_client_ip_address' ready shutdown database 'ora_database_name ' now';
sp_send_mail('urmail@mail.com',msMsg);
EXCEPTION
WHEN OTHERS THEN
sp_send_mail(' urmail@mail.com ',ora_database_name' shutdown error');
END;
说明:当数据库关闭之前,发送Mail到指定邮箱
不要在关闭/启动数据库的触发器中调用utl_file包写文件,可能会导致BUG,引起数据库不能启动,假如实在有必要,则关闭数据库与启动数据库不要使用同一个会话。
4、监控登录用户的触发器
先需要建立一张表,用于存放登陆信息
create table LOG$INFORMATION
(
IDNUMBER(10),
USERNAMEVARCHAR2(30),
LOGINTIME DATE,
TERMINALVARCHAR2(50),
IPADRESSVARCHAR2(20),
OSUSERVARCHAR2(30),
MACHINE VARCHAR2(64),
PROGRAM VARCHAR2(64),
SID NUMBER,
SERIAL# NUMBER,
AUSID NUMBER
)
然后需要创建一个序列,才产生连续的序列号,根据序列的信息,可以更好的得到登录的信息
create sequence SQ_LOGIN
minvalue 1
maxvalue 999999999
start with 1
increment by 1
cache 20;
最后创建触发器,记载登录信息
CREATE OR REPLACE TRIGGER TR_LOGIN_RECORD
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 THEN
INSERT INTO log$information(id,username,logintime,terminal,ipadress,osuser,machine,
program,sid,serial#,ausid)
VALUES(sq_login.nextval,USER,SYSDATE,mtSession.Terminal,
SYS_CONTEXT ('USERENV','IP_ADDRESS'),mtSession.Osuser,
mtSession.Machine,mtSession.Program,mtSession.Sid,mtSession.Serial#,userenv('SESSIONID'));
ELSE
sp_write_log('session信息错误:'SQLERRM);
raise_application_error(-20099,'登录异常错误',FALSE);
END IF;
CLOSE cSession;
EXCEPTION
WHEN OTHERS THEN
sp_write_log('登记登录信息错误:'SQLERRM);
RAISE;
END;
说明:这个触发器监控所有登录用户,并把其信息存入到以上表中。
根据表中记载的信息,可以获得所有登录信息,用于审计用户的登陆是否许可。
5、监控所有DDL的触发器
当然,在此之前我们需要建立一张表,用来记录所有的DDL操作的信息。
create table DDL$TRACE
(
LOGIN_USERVARCHAR2(30),
AUDSIDNUMBER,
IPADDRESS VARCHAR2(20),
SCHEMA_USER VARCHAR2(30),
SCHEMA_OBJECT VARCHAR2(30),
DDL_TIMEDATE,
DDL_SQL VARCHAR2(4000)
)
下面就是触发器的主体,用来记录审计所有的DDL操作。
CREATE OR REPLACE TRIGGER tr_trace_ddl
AFTER ddl
ON database
DECLARE
sql_text ora_name_list_t;
state_sql ddl$trace.ddl_sql%TYPE;
BEGIN
FOR i IN 1..ora_sql_txt(sql_text) LOOP
state_sql := state_sqlsql_text(i);
END LOOP;
INSERT INTO ddl$trace(login_user,audsid,ipaddress,
schema_user,schema_object,ddl_time,ddl_sql)
VALUES(ora_login_user,userenv('SESSIONID'),sys_context('userenv','ip_address'),
ora_dict_obj_owner,ora_dict_obj_name,SYSDATE,state_sql);
EXCEPTION
WHEN OTHERS THEN
sp_write_log('捕捉DDL语句异常错误:'SQLERRM);
END tr_trace_ddl;
说明:以上语句是监控整个数据库的DDL语句,假如只想监控一个用户的话,需要修改
ON database
为
ON uruser.schema
6、捕捉有需要的DML语句
对于某些非凡的表,可能需要记载DML语句,我们也需要创建一张表来记载这个信息:
create table CAPT$SQL
(
CAPT_TIMEDATE,
USERNAME VARCHAR2(30),
AUDSIDNUMBER,
CLIENT_IPVARCHAR2(20),
SQL_TEXT VARCHAR2(4000),
TABLE_NAME VARCHAR2(30),
OWNERVARCHAR2(30)
)
以下就是捕捉特定表的DML语句的触发器
CREATE OR REPLACE TRIGGER tr_capt_sql
BEFORE DELETE OR INSERT OR UPDATE
ON mtamanager.emailbox
DECLARE
stmt VARCHAR2(4000);
sql_text ora_name_list_t;
BEGIN
FOR i IN 1..ora_sql_txt(sql_text) LOOP
stmt := stmt sql_text(i);
END LOOP;
INSERT INTO
capt$sql(CAPT_TIME,USERNAME,AUDSID,CLIENT_IP,SQL_TEXT,
TABLE_NAME,OWNER)
VALUES(sysdate,ora_login_user,userenv('SESSIONID'),
sys_context('userenv','ip_address'),stmt,'emailbox','mtamanager');
EXCEPTION
WHEN OTHERS THEN
pkgsys_manage.sp_write_log('捕捉DML语句异常错误:'SQLERRM);
END;