Oracle資料表空間增長率監控指令碼

來源:互聯網
上載者:User

由於最近業務量大增大,導致資料表空間增長速度變得很快,客戶也開始擔憂資料表空間的增長率。因此也提出了每日監控資料表空間增長量的需求。筆者根據客戶的需求,在這裡寫了個簡單的指令碼,主體思想是通過,將每日查詢到的資料表空間增長率插入到自己建的表中,然後通過構造查詢語句,反映出資料表空間的增長率,具體實施不走如下

在資料主機上建立tbs_usage表反映資料中資料檔案的使用量,其中tbs_timeid為該表主鍵,作為唯一標識當日資料庫資料表空間的id構造tbs_timeid為df.tablespace_name||"-"||(sysdate)
1、pansky使用者作為日常管理,目前主要使用者資料表空間資料量的監控
SQL> create user pansky identified by pansky default tablespace users quota 50M on users;
User created.

SQL> grant create session to pansky;
Grant succeeded.

SQL> grant create table to pansky;
Grant succeeded.

SQL> grant select on dba_data_files to pansky;
Grant succeeded.

SQL>  grant select on dba_free_space to pansky;
Grant succeeded.

2、以pansky使用者建立tbs_usage表
create table tbs_usage
as
SELECT df.tablespace_name||"-"||(sysdate) tbs_timeid ,df.tablespace_name||"-"||(sysdate-1) ys_tbs_timeid,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 df,
(SELECT tablespace_name,
file_id,
SUM(BYTES) BYTES,
MAX(BYTES) maxbytes
FROM dba_free_space
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 8;

3、建立主鍵約束
alter table tbs_usage add constraint tbs_usage_pk_tbs_timeid primary key(tbs_timeid);

4、在crontab中運行每日7點30分更新資料庫資料表空間資訊的指令碼update_tbs_info.sh
30 07 * * * /Oracle10g/update_tbs_info.sh
其中 update_tbs_info.sh指令碼內容如下
#!/bin/ksh
#FileName: update_tbs_info.sh
#CreateDate:2011-10-09
#Discription:take the basic information to  insert into  the table tbs_usage
PATH=/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/oracle/bin:/home/                                                                   oracle/bin:/oracle10g/app/oracle/product/10.2.0/db_1/bin;export PATH

ORACLE_SID=zgscdb1;export ORACLE_SID
ORACLE_BASE=/oracle10g/app/oracle;export ORACLE_BASE
ORACLE_HOME=/oracle10g/app/oracle/product/10.2.0/db_1;export ORACLE_HOME
PATH=$ORACLE_HOME/bin:$PATH;export PATH

date >> /oracle10g/log/update_tbs_info.log
sqlplus pansky/pansky <<EOF  >> /oracle10g/log/update_tbs_info.log 2>&1
insert into pansky.tbs_usage
SELECT df.tablespace_name||"-"||(sysdate) tb_timeid,df.tablespace_name||"-"||(sysdate-1) y                                                                   s_tb_timeid,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 df,
(SELECT tablespace_name,
file_id,
SUM(BYTES) BYTES,
MAX(BYTES) maxbytes
FROM dba_free_space
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 8;
commit;
EOF
echo >> /oracle10g/log/update_tbs_info.log

4、查詢資料庫資料表空間使用方式的SQL,下例可查詢出2011-10-08的資料表空間使用方式以及相較於2011-10-09日的資料表空間增長量(MB),並根據pct_used降序排列。
Set linesize 150
Col tablespace_name for a22
select a.tablespace_name,a.datafile_count,a.size_mb,a.free_mb,a.used_mb,a.maxfree,a.pct_used,a.pct_free,to_char(a.time,"yyyy-mm-dd hh24:mi") time,(a.USED_MB-b.USED_MB) increase_mb from pansky.tbs_usage a,pansky.tbs_usage b
where a.YS_TBs_TIMEid= b.TBs_TIMEid
and a.time>=to_date("2011-11-02","yyyy-mm-dd") and a.time< to_date("2011-11-03","yyyy-mm-dd")  order by pct_used desc;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.