收集ADDM报告并发送邮件PROCEDURE

2018-07-27  本文已影响0人  答春竹

收集ADDM报告,邮件发送给指定邮箱;
实际就是抽取了ADDM报告的sql,可以了解下对应的addm相关sql包。
也可以改AWR报表。缺点是text的格式而不是html的格式。排版不好看

create or replace procedure send_addmrpt1(fromuser varchar2,
                                          fpasswd  varchar2,
                                          touser   varchar2,
                                          subj     varchar2)
  --usage example : exec send_addmrpt1('xxxx@qq.com','xxpassword','xxxxxx@qq.com','addm rpt');
 is
  dbid      number;
  inst_num  number;
  bid       number;
  eid       number;
  texttt    clob;
  id        number;
  name      varchar2(100);
  descr     varchar2(500);
  task_name varchar2(40);
  --create text report
  cursor awrtext_cur is(
    select dbms_advisor.get_task_report(task_name, 'TEXT', 'TYPICAL') report
      from sys.dual);
  c      utl_smtp.connection;
  send   varchar2(40);
  pass   varchar2(40);
  header varchar2(200);

begin
  send   := utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(fromuser)));
  pass   := utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(fpasswd)));
  header := 'From:' || fromuser || utl_tcp.CRLF || 'to:' || touser ||
            utl_tcp.CRLF || 'Subject:  ' || subj ||
            TO_CHAR(SYSDATE, 'yyyy mm dd hh24:mi:ss') || utl_tcp.CRLF;
  select max(snap_id) - 12 into bid from dba_hist_snapshot;
  select max(snap_id) into eid from dba_hist_snapshot;
  select dbid into dbid from v$database;
  select instance_number into inst_num from v$instance;

  BEGIN
    -- initialize addm
    name  := '';
    descr := 'ADDM run: snapshots [' || bid || ', ' || eid ||
             '], instance ' || inst_num || ', database id ' || dbid;

    dbms_advisor.create_task('ADDM', id, name, descr, null);

    task_name := name;

    -- set time window
    dbms_advisor.set_task_parameter(name, 'START_SNAPSHOT', bid);
    dbms_advisor.set_task_parameter(name, 'END_SNAPSHOT', eid);

    -- set instance number
    dbms_advisor.set_task_parameter(name, 'INSTANCE', inst_num);

    -- set dbid
    dbms_advisor.set_task_parameter(name, 'DB_ID', dbid);

    -- execute task
    dbms_advisor.execute_task(name);

  end;
  -- initialize smtp,you can change the host
  c := utl_smtp.open_connection('smtp.qq.com', 25);
  utl_smtp.ehlo(c, 'smtp.qq.com');
  utl_smtp.command(c, 'AUTH LOGIN');
  utl_smtp.command(c, send);
  utl_smtp.command(c, pass);
  utl_smtp.mail(c, fromuser);
  utl_smtp.rcpt(c, touser);
  utl_smtp.open_data(c);
  utl_smtp.write_data(c, header);
  -- open cursor and create mail main body
  begin
    for rec in awrtext_cur loop
      texttt := rec.report;
      utl_smtp.write_raw_data(c,
                              utl_raw.cast_to_raw(utl_tcp.CRLF || texttt));
    end loop;
  end;

  utl_smtp.close_data(c);
  utl_smtp.quit(c);
  utl_smtp.close_connection(c);

exception
  WHEN OTHERS THEN
    DBMS_OUTPUT.put_line(DBMS_UTILITY.format_error_stack);
    DBMS_OUTPUT.put_line(DBMS_UTILITY.format_call_stack);

end;
上一篇下一篇

猜你喜欢

热点阅读