在工作过程中,运维人员需要每天去看出表空间是不是已经不足,一般是如果发现表空间不足85%,再创建一个表空间。但是这样无形中增加了工作量,如果同时监控多个主机,则更加麻烦。
下面介绍如何使用脚本自动检查扩展:
-------------------创建表空间使用---------------------------------
create view TABLESPACE_USAGE as
select
a.tablespace_name,
a.file_name,
a.total "Total(MB)",
round(a.total-b.Free_Space) "Used(MB)",
round(((a.total-b.Free_Space)/a.total)*100,2) "Used(%)",
a.auto_extend
from
(select
FILE_ID,
tablespace_name,
file_name,
bytes/(1024*1024) Total,
AUTOEXTENSIBLE auto_extend
from
dba_data_files ddf) a,
(select
file_id,
sum(bytes)/(1024*1024) Free_Space
from
dba_free_space
group by file_id) b
where
a.file_id=b.file_id;
select * from TABLESPACE_USAGE;
上面视图可以看到目前整个数据库的表空间使用情况
TABLESPACE_NAME FILE_NAME Total(MB) Used(MB) Used(%) AUTO_EXTEND
1 SYSTEM /collect/oracle/oradata/ora11g/system01.dbf 700 676 96.5 YES
2 SYSAUX /collect/oracle/oradata/ora11g/sysaux01.dbf 600 495 82.52 YES
3 USERS /collect/oracle/oradata/ora11g/users01.dbf 13.75 10 75.45 YES
4 TEST_SPACE /collect/oracle/oradata/ora11g/test_space.dbf 100 1 1 NO
5 UNDOTBS1 /collect/oracle/oradata/ora11g/undotbs01.dbf 670 35 5.28 YES
脚本如下:
----------------------shell脚本---------------------------
#####################################################################
## checkTabsp.sh ##
## This Script will add the new datafile if Tablespace's data
## file, which is greater than the 80% of one datafiles size
#####################################################################
#!/bin/bash
usedPercentNO=(`sqlplus -s '/as sysdba' <
SET heading OFF;
SET verify OFF;
SELECT * FROM tablespace_used_percent;
EOF`
)
#get the length of array
len=${#usedPercentNO[*]}
echo "The array has $len members."
i=0
while [ $i -lt $len ]; do
echo "$i: ${usedPercentNO[$i]}"
arrNo=`echo "${usedPercentNO[$i]}" | awk -F. '{print $1}'`
if [ -z $arrNo ]
then
arrNo=1
fi
# if usedPercentNo >= 80 then we add new data file,which will have 8G size
if [ $arrNo -gt 80 ]
then
let sigNo=$i+1
sqlplus -s "/ as sysdba" <
ALTER TABLESPACE DB_TABLESPACE ADD DATAFILE '/opt/oracle/oradata/DB/DB_DATA$sigNo.dbf' SIZE 2G AUTOEXTEND ON MAXSIZE 8G;
EOF
# we need send email to report the tablespace stats info
sqlplus -s "/as sysdba" <
col tablespace_name for a30
col file_name for a60
col auto_extend for a12
col tablespace_name justify center
col file_name justify center
col autoextend justify right
set linesize 200
set pagesize 500
SPOOL tablespace.alert
SELECT * FROM TABLESPACE_USAGE;
SPOOL OFF;
EXIT
EOF
fi
let i++
done
#we needn't send email from there the crontab will do
if [ `cat tablespace.alert|wc -l` -gt 0 ]
then
cat tablespace.alert >tablespace.tmp
mailx -s "TABLESPACE ALERT for DB" EMAIL-ADDRESS < tablespace.alert
fi
上面这个脚本会导致如果有一个文件超过80%的话,脚本会不停添加数据文件....