在Linux系统上同时监控多个Oracle数据库表空间的方法
一,设计背景
由于所在公司oracle数据库较多,传统人工监控表空间的方式较耗时,且无法记录历史表空间数据,无法判断每日表空间增长量,在没有gridcontrol/cloudcontrol软件的情况下,笔者设计如下表空间监控方案,大家也可以根据自己的实际情况对下面的方案进行修改。
二,设计思路
通过dblink将来查询到的表空间数据集中汇总到一张表里通过crontab跑定时任务从各台服务器获取表空间使用情况信息。
三,具体实施步骤
1.所在oracle数据库ip地址信息(下面为举例说明具体情况要根据所在环境设置)
2.在tbsmonitor主机上创建tbsmonitor表空间
create tablespace tbsmonitor datafile '/opt/u01/app/oradata/tbsmonitor/tsmonitor.dbf' size 50m autoextend on;
3.在tbsmonitor和database1/database2/database3上建立tbsmonitor用户用来做表空间监控。
create user tsmonitor identified by i11m8cb default tablespace tsmonitor;
4.为了tbsmonitor用户赋权用来查找表空间使用情况。
grant resource to tbsmonitor; grant create session to tbsmonitor; grant create table to tbsmonitor; grant select on dba_data_files to tbsmonitor; grant select on dba_free_space to tbsmonitor;
5.在tbsmonitor上建立database1/ database2/ database3的tnsnames.ora连接,在tnsnames.ora文件中加入
database1 = (description= (address=(protocol=tcp)(host=10.1.21.1)(port=1521)) (connect_data=(sid= database1))) database2 = (description= (address=(protocol=tcp)(host=10.1.21.2)(port=1521)) (connect_data=(sid= database2))) database3 = (description= (address=(protocol=tcp)(host=10.1.21.3)(port=1521)) (connect_data=(sid= database3)))
6.修改/etc/hosts文件,如果有dns服务器的话可以略过
10.1.21.2 database1 10.1.21.3 database2 10.1.21.4 database3
7.在tbsmonitor主机设置dblink,这样就能通过dblink从被监控服务器远程抽取表空间信息。
create database link to_database1 connect to tsmonitor identified by i11m08cb using 'database1'; create database link to_database2 connect to tsmonitor identified by i11m08cb using 'database2'; create database link to_database3 connect to tsmonitor identified by i11m08cb using 'database3';
8.建立tbsmonitor表,表空间统计数据将插入这张表。
create table tbsmonitor.tbsmonitor ( ipaddress varchar2(200), instancename varchar2(200), tablespace_name varchar2(200), datafile_count number, size_mb number, free_mb number, used_mb number, maxfree number, pct_used number, pct_free number, time date ) tablespace tbsmonitor;
9. 在crontab中运行每日0点1分更新数据库表空间信息的脚本tbsmonitor.sh(我根据业务需要每日统计一次,大家也可以通过业务要求修改统计频率)
1 0 * * * /opt/u01/app/oracle/tbsmonitor.sh
#!/bin/bash #filename: tbsmonitor.sh #createdate:2016-01-1 #version:1.0 #discription:take the basic information to insert into the table tbs_usage # author:fuzhou hot #email:15980219172@139.com oracle_sid= tbsmonitor oracle_base=/opt/u01/app oracle_home=/opt/u01/app/oracle path=$oracle_home/bin:$path;export path export oracle_sid oracle_base oracle_home date>>/opt/u01/app/oracle/tbsmonitor.sh sqlplus sys/i11m08cb as sysdba <<eof >> /opt/u01/app/oracle/tbsmonitor.log 2>&1 @/opt/u01/app/oracle/tbsmonitor/ tbsmonitor.sql; @/opt/u01/app/oracle/tbsmonitor/database1.sql; @/opt/u01/app/oracle/tbsmonitor/database2.sql; @/opt/u01/app/oracle/tbsmonitor/database3.sql; eof echo >> /opt/u01/app/oracle/ tbsmonitor.log
11.创建插入脚本(拿database1举例,以此类推)
/opt/u01/app/oracle/tbsmonitor/database1.sql; /opt/u01/app/oracle/tbsmonitor/database2.sql; /opt/u01/app/oracle/tbsmonitor/database3.sql; /opt/u01/app/oracle/tbsmonitor/ tbsmonitor.sql;
sql脚本如下
insert into tsmonitor.tbsmonitor select utl_inaddr.get_host_address('database1') ipaddress, (select instance_name from v$instance) instancename, df.tablespace_name, count(*) datafile_count, round(sum(df.bytes) / 1048576) size_mb, round(sum(free.bytes) / 1048576, 2) free_mb, round(sum(df.bytes) / 1048576 - sum(free.bytes) / 1048576, 2) used_mb, round(max(free.maxbytes) / 1048576, 2) maxfree, 100 - round(100.0 * sum(free.bytes) / sum(df.bytes), 2) pct_used, round(100.0 * sum(free.bytes) / sum(df.bytes), 2) pct_free,sysdate time from dba_data_files@to_database1 df, (select tablespace_name, file_id, sum(bytes) bytes, max(bytes) maxbytes from dba_free_space@to_database1 group by tablespace_name, file_id) free where df.tablespace_name = free.tablespace_name(+) and df.file_id = free.file_id(+) group by df.tablespace_name order by 6;
12.查看表空间使用占比可以使用如下语句(如果要查看某台机器可以带上条件where ipaddress='xxxx' and instance='xxxxx' and to_char(time,'yyyy-mm-dd')='xxxx-xx-xx')
select ipaddress , instancename, tablespace_name, datafile_count, size_mb "表空间大小(m)", used_mb "已使用空间(m)", to_char(round((used_mb) / size_mb * 100, 2), '990.99') "使用比", free_mb "空闲空间(m)" from tbsmonitor. tbsmonitor order by "使用比" desc
13.查看每日增量可以使用如下脚本。(下面显示的是4-8日10.1.21.2表空间增长的情况)
select a.tablespace_name,(b.used_mb-a.used_mb) increase,a.ipaddress from (select * from tsmonitor.tbs_usage where to_char(time,'yyyy-mm-dd')='2016-01-04') a, (select * from tsmonitor.tbs_usage where to_char(time,'yyyy-mm-dd')='2016-01-08') b where a.tablespace_name=b.tablespace_name and a.ipaddress=b.ipaddress order by increase desc select * from tbsmonitor. tbsmonitor where ipaddress='10.1.21.2' and to_char(time,'yyyy-mm-dd')='2016-01-08'