项目中要对表空间做监控,定时的执行相关python脚本调用设计好的oracle 存储工程,同时扫描表每一次监控的记录是否达标,如果不达标则发出邮件通知及时处理。
第一、首先设计表空间配置 表 ALERT_CFG_DB_TABLESPACE
<span style="font-size:12px;">-- Create table
create table ALERT_CFG_DB_TABLESPACE
(
DB_NAME VARCHAR2(30),
TS_NAME VARCHAR2(30),
ALERT_RATIO NUMBER(5,4),
ALERT_FREE_GB NUMBER(6,2)
)
tablespace NISMP_DATA
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table ALERT_CFG_DB_TABLESPACE
is '告警配置表:数据库表空间';
-- Add comments to the columns
comment on column ALERT_CFG_DB_TABLESPACE.DB_NAME
is '数据库';
comment on column ALERT_CFG_DB_TABLESPACE.TS_NAME
is '表空间';
comment on column ALERT_CFG_DB_TABLESPACE.ALERT_RATIO
is '已使用比例告警(非空则必须满足)';
comment on column ALERT_CFG_DB_TABLESPACE.ALERT_FREE_GB
is '剩余空间告警绝对值(非空则必须满足)';</span>
create table ALERT_LOG_DB_TABLESPACE
(
INFO_DATE VARCHAR2(12),
DB_NAME VARCHAR2(30),
TS_NAME VARCHAR2(30),
TS_RATIO NUMBER(5,4),
TS_FREE_GB NUMBER(6,2),
INFO_TIME DATE
)
tablespace NISMP_DATA
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table ALERT_LOG_DB_TABLESPACE
is '表空间使用情况表';
-- Add comments to the columns
comment on column ALERT_LOG_DB_TABLESPACE.INFO_DATE
is '信息日期';
comment on column ALERT_LOG_DB_TABLESPACE.DB_NAME
is '数据库';
comment on column ALERT_LOG_DB_TABLESPACE.TS_NAME
is '表空间';
comment on column ALERT_LOG_DB_TABLESPACE.TS_RATIO
is '使用率';
comment on column ALERT_LOG_DB_TABLESPACE.TS_FREE_GB
is '剩余空间GB';
comment on column ALERT_LOG_DB_TABLESPACE.INFO_TIME
is '生成时间';
第二、设计存储过程对表空间进行统计和计算 插入log表空间使用情况表
create or replace procedure p_alert_log_db_tablespace(
/*
** 功能:检查表空间剩余情况
*/i_info_date in varchar2, --信息日期
i_db_name in varchar2, --数据库
o_errnum out number,
o_errmsg out varchar2) is
vtime date := sysdate;
begin
o_errnum := 0;
o_errmsg := null;
insert into alert_log_db_tablespace
(info_date, db_name, ts_name, ts_ratio, ts_free_gb, info_time)
select i_info_date,
a.db_name,
a.ts_name,
round(nvl(b.total_gb - nvl(c.free_gb, 0), 0) / b.total_gb, 2) ts_ratio,
nvl(c.free_gb, 0) ts_free_gb,
vtime
from alert_cfg_db_tablespace a
left join (select x.tablespace_name ts_name,
round(sum(x.bytes) / 1024 / 1024 / 1024, 2) total_gb
from dba_data_files x
group by x.tablespace_name) b
on a.ts_name = b.ts_name
left join (select x.tablespace_name ts_name,
round(sum(x.bytes) / 1024 / 1024 / 1024, 2) free_gb
from dba_free_space x
group by x.tablespace_name) c
on a.ts_name = c.ts_name
where a.db_name = i_db_name;
commit;
end p_alert_log_db_tablespace;
如图 自己可以直接测试输入相关的参数,日期,数据库 在表空间就会插入一条记录,如图
上图为 直接测试存储工程的步骤,下面查看表使用情况的记录如图
根据表使用情况看出当前数据库表空间的使用,后续可以根据这个开始开发调用存储过程的python 代码