--查看alert日志所在目录
select value from v$diag_info where name=‘Diag Trace‘;
VALUE
---------------------------------------------------
/home/oracle/app/oracle/diag/rdbms/ecom/ecom/trace
--查到alert文件的名称(alert文件命名原则:alert_dbname.log)
select ‘alert_‘||lower(name)||‘.log‘ from v$database;
--创建目录
create or replace directory data_dir as ‘/home/oracle/app/oracle/diag/rdbms/ecom/ecom/trace‘;
--创建警告日志表【directory、location要替换成你自己的】
create table alert_log
(
text_line varchar(255)
)
organization external
(
type oracle_loader
default directory data_dir
access parameters
(
records delimited by newline
fields
reject rows with all null fields
)
location
(
‘alert_ecom.log‘
)
)
reject limit unlimited; ----注意这点,否则在查询时会报错 ora-30653
查询一、
select r,
to_date(last_time, ‘Dy Mon DD HH24:MI:SS YYYY‘,‘NLS_DATE_LANGUAGE = American‘) last_time,
to_date(start_time,‘Dy Mon DD HH24:MI:SS YYYY‘,‘NLS_DATE_LANGUAGE = American‘) start_time
from (
select r,
text_line,
lag(text_line,1) over (order by r) start_time,
lag(text_line,2) over (order by r) last_time
from (
select rownum r, text_line
from alert_log
where text_line like ‘___ ___ __ __:__:__ 20__‘
or text_line like ‘Starting ORACLE instance %‘
)
)
where text_line like ‘Starting ORACLE instance %‘;
查询二、
select to_char(last_time,‘dd-mon-yyyy hh24:mi‘) shutdown,
to_char(start_time,‘dd-mon-yyyy hh24:mi‘) startup,
round((start_time-last_time)*24*60,2) mins_down,
round((last_time-lag(start_time) over (order by r)),2) days_up,
case when (lead(r) over (order by r) is null )
then round((sysdate-start_time),2)
end days_still_up
from (
select r,
to_date(last_time, ‘Dy Mon DD HH24:MI:SS YYYY‘,‘NLS_DATE_LANGUAGE = American‘) last_time,
to_date(start_time,‘Dy Mon DD HH24:MI:SS YYYY‘,‘NLS_DATE_LANGUAGE = American‘) start_time
from (
select r,
text_line,
lag(text_line,1) over (order by r) start_time,
lag(text_line,2) over (order by r) last_time
from (
select rownum r, text_line
from alert_log
where text_line like ‘___ ___ __ __:__:__ 20__‘
or text_line like ‘Starting ORACLE instance %‘
)
)
where text_line like ‘Starting ORACLE instance %‘
);
查询三、
select * from alert_log t where t.TEXT_LINE like ‘%ORA-%‘;
---------------------额外说明-----------------
to_date(last_time, ‘Dy Mon DD HH24:MI:SS YYYY‘,‘NLS_DATE_LANGUAGE = American‘) last_time
上面的例子里面是没有‘NLS_DATE_LANGUAGE = American‘这部分的,执行时老提示“周中的日无效”,
后来查询才知道要加这部分,用了这么久to_date才知道原来还有第三个参数。