Script:利用外部表实现SQL查询Oracle告警日志Alert.log

有同学问是否可以用SQL语句直接查询告警日志的内容,即创建一张包含Alert.log内容的表或视图。 实际上之前已经有人这样做了(http://t.cn/SwGvq9),只需要运行一个存储过程即可达到目的, 这里我对原有的语句做了一些改良, 直接执行PL/SQL块即可无需创建存储过程了,而且现在支持RAC了。

 

--drop table alert_log_view;
--drop directory bdump;

declare
  path_bdump varchar2(4000);
  name_alert varchar2(4000);
  ins_name   varchar2(200);
begin

  select value
    into path_bdump
    from sys.v_$parameter
   where name = 'background_dump_dest';
  select 'alert_' || value || '.log'
    into name_alert
    from sys.v_$parameter
   where name = 'instance_name';

  select value
    into ins_name
    from sys.v_$parameter
   where name = 'instance_number';

  if ins_name = '0' then
    ins_name := '';
  end if;

  execute immediate 'create or replace directory bdump'||ins_name||' as ''' || path_bdump || '''';
  execute immediate 'create table ALERT_LOG_VIEW' || ins_name ||
                    '  (MSG_line varchar2(4000)   ) ' ||
                    ' organization external ' || ' (type oracle_loader ' ||
                    ' default directory bdump' || ins_name ||
                    ' access parameters ( ' ||
                    ' records delimited by newline ' || ' nobadfile ' ||
                    ' nologfile ' || ' nodiscardfile ' || ' skip 0 ' ||
                    ' READSIZE 10485760 ' || ' FIELDS LDRTRIM ' ||
                    ' REJECT ROWS WITH ALL NULL FIELDS ' ||
                    ' (MSG_LINE (1:1000) CHAR(1000)) ' || ' ) ' ||
                    ' location (''' || name_alert || ''') )' ||
                    ' reject limit unlimited ' ||
                    ' noparallel nomonitoring ';

end;
/

 

执行以上PL/SQL代码,会创建名为bdump$SID的目录 和ALERT_LOG_VIEW$SID的外部表(如RAC中的1号实例PROD1,则为ALERT_LOG_VIEW1,单实例single instance则为 ALERT_LOG_VIEW), 需要时直接查询ALERT_LOG_VIEW即可,譬如要从告警信息中找出最近三天ORA-错误的记录:

 

Comments

  1. Charlie 木匠 (@mujiang) says

    昨天看Tom的新书(一年前的旧书了), 93~96页, 有类似解决方案.

  2. 这个太牛了,居然可以不用shell就能做。

  3. casper0511 says

    第二段sql报错:
    ORA-01846: not a valid day of the week

    修改字符集后正常。alter session set nls_date_language = ‘american’;

  4. chengwill says

    Hello

    That’s a perfect implementation for analysis ORACLE DB& RAC alert logs mothod.

    I have used it on our RAC environment ,it’s working fine!

    Thank you provide it!

  5. Youhai says

    SQL> select “LINENO”, “THEDATE”, “ORA_ERROR”, “MSG_LINE”
    2 from (select *
    3 from (select lineno,
    4 msg_line,
    5 thedate,
    6 max(case
    7 when (ora_error like ‘ORA-%’ or
    8 ora_error like ‘PLS-%’) then
    9 rtrim(substr(ora_error, 1, instr(ora_error, ‘ ‘) – 1),
    10 ‘:’)
    11 else
    12 null
    13 end) over(partition by thedate) ora_error
    14 from (select lineno,
    15 msg_line,
    16 max(thedate) over(order by lineno) thedate,
    17 lead(msg_line) over(order by lineno) ora_error
    18 from (select rownum lineno,
    19 substr(msg_line, 1, 132) msg_line,
    20 case
    21 when msg_line like
    22 ‘___ ___ __ __:__:__ ____’ then
    23 to_date(msg_line,
    24 ‘Dy Mon DD hh24:mi:ss yyyy’)
    25 else
    26 null
    27 end thedate
    28 from ALERT_LOG_VIEW))))
    29 where ora_error is not null
    30 and thedate >= (trunc(sysdate) – 3)
    31 order by thedate
    32 /
    to_date(msg_line,
    *
    第 23 行出现错误:
    ORA-29913: 执行 ODCIEXTTABLEFETCH 调出时出错
    ORA-01846: 周中的日无效

Comment

*

沪ICP备14014813号

沪公网安备 31010802001379号