表空间自动报警存储过程

--sys用户创建
create or replace view  dba_tablespace_free as  
select a.tablespace_name,
       trunc(a.total) allocated_space_mb,
       trunc(a.total-b.free) Used_mb,
       trunc(b.free) free_space_mb,
       round((1-b.free/a.total),4)*100 usage,
       c.AUTOSIZE AUTOSIZE_mb,
       round(((a.total-b.free)/c.AUTOSIZE),4)*100 autousage
from 
         (select tablespace_name, sum(nvl(bytes,2))/1024/1024 total from dba_data_files group by tablespace_name) a,
         (select tablespace_name, sum(nvl(bytes,2))/1024/1024 free from dba_free_space group by tablespace_name) b,
         (select x.TABLESPACE_NAME,sum(x.AUTOSIZE) AUTOSIZE from 
(select TABLESPACE_NAME ,
   CASE
            WHEN MAXBYTES/1024/1024 = 0
            THEN
                  BYTES/1024/1024
            ELSE
               MAXBYTES/1024/1024
         END
         AUTOSIZE
from  DBA_DATA_FILES) x
group by x.tablespace_name)  c
where 
a.tablespace_name=b.tablespace_name
and
a.tablespace_name=c.tablespace_name
and
b.TABLESPACE_NAME=c.TABLESPACE_NAME
and
a.tablespace_name not in('SYSTEM','SYSAUX','UNDOTBS1')
order by 3 desc;
 create or replace procedure p_monit_tbs  as
  --created by houzhiqing,2016-09-13
  --监控表空间
  vMessage         varchar(250);
  vDb_name         varchar2(10);  --数据库名
  vUsage     number;        --剩余表空间阀值
BEGIN
  --获取数据库名
  vUsage :=92;
  select name into vDb_name from v$database;
   for x in (SELECT tablespace_name,usage,autousage,free_space_mb,(autosize_mb-USED_mb) AutoFreeSize_mb FROM dba_tablespace_free) loop
if(x.usage>= vUsage) then
        vMessage := '[warning]' || vDb_name ||
                    ' Databases '|| x.tablespace_name||' Tablespace Used:'|| x.usage 
                    ||'%,Fixed allocation tablespace Free:'||x.free_space_mb|| 'MB, Autoextended tablespace Used:'||x.autousage||'%,Autoextended tablespace Free:'||x.AutoFreeSize_mb||'M';
    dbms_output.put_line(vMessage);
end if;
    end loop;
end;
/
set serveroutput on;
execute p_monit_tbs

 

posted on 2016-05-16 13:49  侯志清  阅读(213)  评论(0编辑  收藏  举报

导航