该程序脚本最主要的功能实现为通过oracle自带的过程包发送邮件来监控ETL的执行情况:
ORACLE_SID=orcl
ORACLE_BASE=/opt/oracle
ORACLE_HOME=/opt/oracle/product/10.2.0
export ORACLE_SID ORACLE_BASE ORACLE_HOME
PWD_DIR=/home/oracle/shell
SQLPLUS=${ORACLE_HOME}/bin/sqlplus
CONFIG_INI=${PWD_DIR}/ini/config.ini
while read gameuser
do
echo ${gameuser}
echo ${SQLPLUS}
cd ${PWD_DIR}
${SQLPLUS} ${gameuser} << !
@etl_monitor.sql;
/
exit;
!
done<${CONFIG_INI}
etl_monitor.sql脚本为:
DECLARE
p_txt VARCHAR2 (4000);
p_txt_all VARCHAR2 (4000);
BEGIN
FOR r IN (SELECT job_name,
run_cnt,
table_name,
column_name
FROM etl_monitor_config_tab)
LOOP
-- Call the Etl Monitor function
p_txt :=
etl_monitor (r.job_name,
r.run_cnt,
r.table_name,
r.column_name);
p_txt_all := p_txt_all || CHR (13) || p_txt;
END LOOP;
-- Call the Send Mail function
procsendemail (p_txt_all,
'Etl Moniotr',
'xxx@163.com ',
'xxxx@kingsoft.com ',
'mail.kingsoft.com',
25,
1,
'xxxxxx',
'xxxxxx',
'',
'bit 7');
p_txt_all := '';
END;
create or replace function etl_monitor(job_name varchar2,
run_cnt int,
table_name varchar2,
column_name varchar2)
RETURN varchar2 IS
v_monitor_date date; --The monitor of the proc's date
v_job_name varchar2(130);
v_log_id number;
v_result1 char(1); --The status of the proc's result1
v_result2 char(1); --The status of the proc's result2
v_status_cnt int;
v_record_num int; --The number of the job run
v_result varchar2(4000);
v_sql varchar2(1000);
begin
v_monitor_date := trunc(sysdate);
v_job_name := job_name;
v_result1 := '0';
v_result2 := '0';
v_sql := 'select count(1) from ';
if run_cnt = 1 then
select log_id
into v_log_id
from user_scheduler_job_run_details
where job_name = v_job_name
and trunc(actual_start_date) = v_monitor_date;
else
select max(log_id)
into v_log_id
from user_scheduler_job_run_details
where job_name = v_job_name
and trunc(actual_start_date) = v_monitor_date;
end if;
select count(*)
into v_status_cnt
from user_scheduler_job_run_details
where log_id = v_log_id
and status = 'SUCCEEDED';
if v_status_cnt = 0 then
goto error1;
end if;
v_result1 := '1';
v_sql := v_sql || table_name || ' ' || 'where trunc(' || column_name ||
') =' || 'trunc(sysdate-1) and rownum=1';
execute immediate v_sql
into v_record_num;
if v_record_num > 0 then
v_result2 := '1';
else
v_status_cnt := 0;
goto error1;
end if;
if v_result1 = '1' and v_result2 = '1' then
v_result := SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') || '.' ||
v_job_name || ' At ' || v_monitor_date || ' IS SUCCEEDED';
end if;
<<error1>>
if v_status_cnt = 0 then
select OWNER || '.' || JOB_NAME || ' At ' || TRUNC(ACTUAL_START_DATE) ||
'IS ' ADDITIONAL_INFO
into v_result
from user_scheduler_job_run_details
where log_id = v_log_id;
end if;
return v_result;
exception
when others then
return SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') || '.' || v_job_name || ' At ' || v_monitor_date || ' IS NOT EXECUTE';
end;
最主要的procsendmail的过程为:
CREATE OR REPLACE PROCEDURE PROCSENDEMAIL (
P_TXT VARCHAR2,
P_SUB VARCHAR2,
P_SENDOR VARCHAR2,
P_RECEIVER VARCHAR2,
P_SERVER VARCHAR2,
P_PORT NUMBER DEFAULT 25 ,
P_NEED_SMTP INT DEFAULT 0 ,
P_USER VARCHAR2 DEFAULT NULL ,
P_PASS VARCHAR2 DEFAULT NULL ,
P_FILENAME VARCHAR2 DEFAULT NULL ,
P_ENCODE VARCHAR2 DEFAULT 'bit 7'
)
AUTHID CURRENT_USER
IS
/*
作用:用oracle发送邮件
主要功能:1、支持多收件人。
2、支持中文
3、支持抄送人
4、支持大于32K的附件
5、支持多行正文
6、支持多附件
7、支持文本附件和二进制附件
8、支持HTML格式
8、支持
作者:suk
参数说明:
p_txt :邮件正文
p_sub: 邮件标题
p_SendorAddress : 发送人邮件地址
p_ReceiverAddress : 接收地址,可以同时发送到多个地址上,地址之间用","或者";"隔开
p_EmailServer : 邮件服务器地址,可以是域名或者IP
p_Port :邮件服务器端口
p_need_smtp:是否需要smtp认证,0表示不需要,1表示需要
p_user:smtp验证需要的用户名
p_pass:smtp验证需要的密码
p_filename:附件名称,必须包含完整的路径,如"d:\temp\a.txt"。
可以有多个附件,附件名称只见用逗号或者分号分隔
p_encode:附件编码转换格式,其中 p_encode='bit 7' 表示文本类型附件
p_encode='base64' 表示二进制类型附件
注意:
1、对于文本类型的附件,不能用base64的方式发送,否则出错
2、对于多个附件只能用同一种格式发送
*/
L_CRLF VARCHAR2 (2) := UTL_TCP.CRLF;
L_SENDORADDRESS VARCHAR2 (4000);
L_SPLITE VARCHAR2 (10) := '++';
BOUNDARY CONSTANT VARCHAR2 (256) := '-----BYSUK';
FIRST_BOUNDARY CONSTANT VARCHAR2 (256) := '--' || BOUNDARY || L_CRLF;
LAST_BOUNDARY CONSTANT VARCHAR2 (256)
:= '--' || BOUNDARY || '--' || L_CRLF ;
MULTIPART_MIME_TYPE CONSTANT VARCHAR2 (256)
:= 'multipart/mixed; boundary="' || BOUNDARY || '"' ;
/* 以下部分是发送大二进制附件时用到的变量 */
L_FIL BFILE;
L_FILE_LEN NUMBER;
L_MODULO NUMBER;
L_PIECES NUMBER;
L_FILE_HANDLE UTL_FILE.FILE_TYPE;
L_AMT BINARY_INTEGER := 672 * 3; /* ensures proper format; 2016 */
L_FILEPOS PLS_INTEGER := 1; /* pointer for the file */
L_CHUNKS NUMBER;
L_BUF RAW (2100);
L_DATA RAW (2100);
L_MAX_LINE_WIDTH NUMBER := 54;
L_DIRECTORY_BASE_NAME VARCHAR2 (100) := 'DIR_FOR_SEND_MAIL';
L_LINE VARCHAR2 (1000);
L_MESG VARCHAR2 (32767);
/* 以上部分是发送大二进制附件时用到的变量 */
TYPE ADDRESS_LIST
IS
TABLE OF VARCHAR2 (100)
INDEX BY BINARY_INTEGER;
MY_ADDRESS_LIST ADDRESS_LIST;
TYPE ACCT_LIST
IS
TABLE OF VARCHAR2 (100)
INDEX BY BINARY_INTEGER;
MY_ACCT_LIST ACCT_LIST;
-------------------------------------返回附件源文件所在目录或者名称-----------------------
FUNCTION GET_FILE (P_FILE VARCHAR2, P_GET INT)
RETURN VARCHAR2 IS
--p_get=1 表示返回目录
--p_get=2 表示返回文件名
L_FILE VARCHAR2 (1000);
BEGIN
IF INSTR (P_FILE, '\') > 0
THEN
--windows
IF P_GET = 1
THEN
L_FILE := SUBSTR (P_FILE, 1, INSTR (P_FILE, '\', -1) - 1);
ELSIF P_GET = 2
THEN
L_FILE :=
SUBSTR (P_FILE, - (LENGTH (P_FILE) - INSTR (P_FILE, '\', -1)));
END IF;
ELSIF INSTR (P_FILE, '/') > 0
THEN
--linux/unix
IF P_GET = 1
THEN
L_FILE := SUBSTR (P_FILE, 1, INSTR (P_FILE, '/', -1) - 1);
ELSIF P_GET = 2
THEN
L_FILE :=
SUBSTR (P_FILE, - (LENGTH (P_FILE) - INSTR (P_FILE, '/', -1)));
END IF;
END IF;
RETURN L_FILE;
END;
---------------------------------------------删除directory------------------------------------
PROCEDURE DROP_DIRECTORY (P_DIRECTORY_NAME VARCHAR2)
IS
BEGIN
EXECUTE IMMEDIATE 'drop directory ' || P_DIRECTORY_NAME;
EXCEPTION
WHEN OTHERS
THEN
NULL;
END;
--------------------------------------------------创建directory-----------------------------------------
PROCEDURE CREATE_DIRECTORY (P_DIRECTORY_NAME VARCHAR2, P_DIR VARCHAR2)
IS
BEGIN
EXECUTE IMMEDIATE 'create directory '
|| P_DIRECTORY_NAME
|| ' as '''
|| P_DIR
|| '''';
EXECUTE IMMEDIATE 'grant read,write on directory '
|| P_DIRECTORY_NAME
|| ' to public';
EXCEPTION
WHEN OTHERS
THEN
RAISE;
END;
--------------------------------------------分割邮件地址或者附件地址--------------------
PROCEDURE P_SPLITE_STR (P_STR VARCHAR2, P_SPLITE_FLAG INT DEFAULT 1 )
IS
L_ADDR VARCHAR2 (254) := '';
L_LEN INT;
L_STR VARCHAR2 (4000);
J INT := 0; --表示邮件地址或者附件的个数
BEGIN
/*处理接收邮件地址列表,包括去空格、将;转换为,等*/
L_STR :=
TRIM (RTRIM (REPLACE (REPLACE (P_STR, ';', ','), ' ', ''), ','));
L_LEN := LENGTH (L_STR);
FOR I IN 1 .. L_LEN
LOOP
IF SUBSTR (L_STR, I, 1) <> ','
THEN
L_ADDR := L_ADDR || SUBSTR (L_STR, I, 1);
ELSE
J := J + 1;
IF P_SPLITE_FLAG = 1
THEN --表示处理邮件地址
--前后需要加上'<>',否则很多邮箱将不能发送邮件
L_ADDR := '<' || L_ADDR || '>';
--调用邮件发送过程
MY_ADDRESS_LIST (J) := L_ADDR;
ELSIF P_SPLITE_FLAG = 2
THEN --表示处理附件名称
MY_ACCT_LIST (J) := L_ADDR;
END IF;
L_ADDR := '';
END IF;
IF I = L_LEN
THEN
J := J + 1;
IF P_SPLITE_FLAG = 1
THEN
--调用邮件发送过程
L_ADDR := '<' || L_ADDR || '>';
MY_ADDRESS_LIST (J) := L_ADDR;
ELSIF P_SPLITE_FLAG = 2
THEN
MY_ACCT_LIST (J) := L_ADDR;
END IF;
END IF;
END LOOP;
END;
------------------------------------------------写邮件头和邮件内容------------------------------------------
PROCEDURE WRITE_DATA (P_CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
P_NAME IN VARCHAR2,
P_VALUE IN VARCHAR2,
P_SPLITE VARCHAR2 DEFAULT ':' ,
P_CRLF VARCHAR2 DEFAULT L_CRLF )
IS
BEGIN
/* utl_raw.cast_to_raw 对解决中文乱码问题很重要*/
UTL_SMTP.WRITE_RAW_DATA (
P_CONN,
UTL_RAW.CAST_TO_RAW (
CONVERT (P_NAME || P_SPLITE || P_VALUE || P_CRLF, 'ZHS16GBK')
)
);
END;
----------------------------------------写MIME邮件尾部-----------------------------------------------------
PROCEDURE END_BOUNDARY (CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
LAST IN BOOLEAN DEFAULT FALSE )
IS
BEGIN
UTL_SMTP.WRITE_DATA (CONN, UTL_TCP.CRLF);
IF (LAST)
THEN
UTL_SMTP.WRITE_DATA (CONN, LAST_BOUNDARY);
END IF;
END;
----------------------------------------------发送附件----------------------------------------------------
PROCEDURE ATTACHMENT (
CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
MIME_TYPE IN VARCHAR2 DEFAULT 'text/plain' ,
INLINE IN BOOLEAN DEFAULT TRUE ,
FILENAME IN VARCHAR2 DEFAULT 't.txt' ,
TRANSFER_ENC IN VARCHAR2 DEFAULT '7 bit' ,
DT_NAME IN VARCHAR2 DEFAULT '0'
)
IS
L_FILENAME VARCHAR2 (1000);
BEGIN
--写附件头
UTL_SMTP.WRITE_DATA (CONN, FIRST_BOUNDARY);
--设置附件格式
WRITE_DATA (CONN, 'Content-Type', MIME_TYPE);
--如果文件名称非空,表示有附件
DROP_DIRECTORY (DT_NAME);
--创建directory
CREATE_DIRECTORY (DT_NAME, GET_FILE (FILENAME, 1));
--得到附件文件名称
L_FILENAME := GET_FILE (FILENAME, 2);
IF (INLINE)
THEN
WRITE_DATA (CONN,
'Content-Disposition',
'inline; filename="' || L_FILENAME || '"');
ELSE
WRITE_DATA (CONN,
'Content-Disposition',
'attachment; filename="' || L_FILENAME || '"');
END IF;
(责任编辑:IT) |