需求:需要监控Oracle数据库表空间使用率和是否设置自动扩展,展示在监控平台上
脚本如下:
[oracle@racdb2:/home/oracle]$ cat tablespace.sh
#!/bin/bash
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/19.3.0/db
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
sqlplus -S / as sysdba <<EOF
set heading off
set pagesize 0
set feedback off
set linesize 200
spool /tmp/ora_tablespace.txt
SELECT aa.tablespace_name,
round(aa.usd / bb.maxs * 100, 2) "USED%",
round((bb.maxs - aa.usd) / 1024 / 1024 / 1024, 2) "FREE(G)",
round(bb.maxs / 1024 / 1024 / 1024, 2) "MAX(G)",
round(aa.usd / 1024 / 1024 / 1024, 2) "USED(G)"
FROM (SELECT b.Tablespace_Name, (b.Bytes - NVL(a.Bytes, 0)) "USD"
FROM (SELECT tablespace_name, SUM(bytes) bytes
FROM Dba_Free_Space
GROUP BY tablespace_name) a,
(SELECT tablespace_name, SUM(bytes) bytes
FROM Dba_Data_Files
GROUP BY tablespace_name) b
WHERE b.tablespace_name = a.tablespace_name(+)
) aa,
(SELECT tablespace_name,
SUM(CASE
WHEN maxbytes = 0 THEN
user_bytes
ELSE
maxbytes
END) "MAXS"
FROM dba_data_files
GROUP BY tablespace_name
ORDER BY tablespace_name) bb
WHERE aa.tablespace_name = bb.tablespace_name
ORDER BY 5 DESC, 1;
spool off
set linesize 100
set pagesize 100
spool /tmp/ora_autex.txt
select tablespace_name,autoextensible from dba_data_files;
spool off
EOF
输出如下:
[oracle@racdb2:/home/oracle]$ sh tablespace.sh
I1_INDEX 76.67 21.93 94 72.07
I1_DATA 24.67 117.51 156 38.49
M1_INDEX 40.04 19.19 32 12.81
SYSAUX 27.04 23.35 32 8.65
M1_DATA 18.27 26.16 32 5.84
x3dpasstokens 3.22 30.97 32 1.03
SYSTEM 2.98 31.05 32 .95
UNDOTBS1 1.43 31.54 32 .46
UNDOTBS2 1.33 31.58 32 .42
x3dnotif .85 31.73 32 .27
M1 .82 31.74 32 .26
x3dpassadmin .64 31.79 32 .21
V1_INDEX .05 31.98 32 .02
x3ddashadmin .06 31.98 32 .02
x3dswym .06 31.98 32 .02
V1_DATA .02 31.99 32 .01
x3dcomment .03 31.99 32 .01
x3dswym_widget .02 31.99 32 .01
USERS .01 32 32 0
x3dswym_media .01 32 32 0
第一列是使用率,第二列是剩余多少GB,第三列是最大多少GB,第四列是使用多少GB
SYSTEM YES
SYSAUX YES
UNDOTBS2 YES
USERS YES
x3dpassadmin YES
UNDOTBS1 YES
x3dpasstokens YES
x3ddashadmin YES
M1 YES
I1_DATA YES
I1_INDEX YES
M1_DATA YES
M1_INDEX YES
V1_DATA YES
V1_INDEX YES
x3dswym YES
x3dswym_media YES
x3dswym_widget YES
x3dcomment YES
x3dnotif YES
I1_INDEX NO
I1_DATA NO
I1_INDEX NO
I1_DATA NO
I1_DATA NO
I1_DATA NO
yes代表设置了自动扩展表空间
内容保存到两个文件里:
[oracle@racdb2:/home/oracle]$ ll /tmp/ora_*
-rw-r--r-- 1 oracle oinstall 2627 Jun 6 10:52 /tmp/ora_autex.txt
-rw-r--r-- 1 oracle oinstall 4020 Jun 6 10:52 /tmp/ora_tablespace.txt