下面是自己写的对数据库检查的存储过程:
create or replace procedure sp_dbstate_check(send_email varchar2,
receive_email varchar2)
v_count number;
n_rows number;
v_sqlcode number(10);
v_sqlerr varchar2(255);
v_tabname varchar2(50);
BEGIN
----会话数检查
select count(*) into v_count from gv$session;
if v_count>1500 then
sp_send_email('alert','数据库会话数超过'||v_count,send_email,receive_email,'smtp.163.com');
end if;
---数据文件状态检查
select ((select count(1) from dba_data_files) -
(select count(1) from dba_data_files where status = 'AVAILABLE'))
into v_count
from dual;
if v_count>0 then
sp_send_email('alert','数据库数据文件状态异常'||v_count,send_email,receive_email,'smtp.163.com');
end if;
----表空间检查
select tablespace_name, free_percent into v_tabname,v_count
from (select f.tablespace_name,
round((f.free / a.total) * 100) free_percent
from (select tablespace_name, sum(bytes / (1024 * 1024)) total
from dba_data_files
group by tablespace_name) a,
(select tablespace_name,
round(sum(bytes / (1024 * 1024))) free
from dba_free_space
group by tablespace_name) f
WHERE a.tablespace_name = f.tablespace_name(+)
and a.tablespace_name not in
('SYSTEM', 'SYSAUX', 'UNDOTBS1','UNDOTBS2', 'USERS')
order by free_percent)
where rownum < 2;
if v_count<10 then
sp_send_email('alert','数据库表空间'||v_tabname||'剩余百分比为'||v_count||'%',send_email,receive_email,'smtp.163.com');
end if;
----asm存储检查
select name, n_count into v_tabname,v_count
from (select name, round(free_mb / total_mb, 3) * 100 n_count
from v$asm_diskgroup
order by 2)
where rownum < 2;
if v_count<15 then
sp_send_email('alert','数据库asm磁盘组'||v_tabname||'剩余百分比为'||v_count||'%',send_email,receive_email,'smtp.163.com');
end if;
----警告日志检查
---Oracle 10g
select count(1) into v_count
from (select msg_line
from (select rownum lineno,
substr(msg_line, 1, 132) msg_line,
case
when msg_line like '___ ___ __ __:__:__ ____' then
to_date(msg_line,
'Dy Mon DD hh24:mi:ss yyyy',
'nls_date_language=American')
else
null
end thedate
from ALERT_LOG_VIEW2
order by 1 desc)
where rownum < 20)
where regexp_like(msg_line,
'Error|Fail|WARNING|Invalid|ORA-|Global Enqueue Services|dead|ORA-',
'i');
if v_count>0 then
sp_send_email('alert','数据库alert日志有错误或警告',send_email,receive_email,'smtp.163.com');
end if;
----表锁阻塞检查
SELECT COUNT(1) into n_rows FROM GV$LOCK WHERE BLOCK=1;
IF n_rows>0 then
SELECT OBJECT_NAME, CTIME into v_tabname,v_count
FROM (SELECT O.OBJECT_NAME, L.CTIME
FROM GV$SESSION S, GV$LOCK L, DBA_OBJECTS O
WHERE L.SID = S.SID
AND L.ID1 = O.OBJECT_ID(+)
AND S.USERNAME IS NOT NULL
AND O.OBJECT_TYPE = 'TABLE'
ORDER BY 2 DESC)
WHERE ROWNUM < 2;
if v_count>300 then
sp_send_email('alert','数据库表'||v_tabname||'产生阻塞,被锁超过'||round(v_count/60)||'分钟',send_email,receive_email,'smtp.163.com');
end if;
END IF;
exception
when others then
v_sqlcode := sqlcode;
v_sqlerr := sqlerrm;
insert into monitor_log
(errcode,errmsg,action)
values
(v_sqlcode,
v_sqlerr,
'dbstat_check_exception');
commit;
END;
