使用shell指令碼檢視資料庫負載情況

yuntui發表於2016-11-03

平時在檢視資料庫的問題時,會有種迷茫的感覺,如果沒有任何人反饋問題,基本上沒有主動查詢問題的方向,awr,ash都是在得知問題發生的時間段或者一些時間戳來從歷史記錄中查詢相關的資訊,個人整理瞭如下的指令碼,能夠顯示當天的時間段內資料庫的負載資訊,能夠很好掌握資料庫的忙閒情況。

來看一個簡單的例子,比如我要檢視早上6點到中午12點資料庫的負載情況

指令碼 showdbtime.sh 顯示的是在制定的時間段內的 資料庫負載的一個綜合值。比如6點到7點個小時(60分鐘),dbtime如果是120分鐘,那麼顯示的負載就是200%

BEGIN_TIME                                       END_TIME                                  ELAPSED_TIME       DBTIME      WORKLOAD_PER
----------------------------------- ----------------------------------- ------------- ---------- --------------------
201 ** 21-MAY-14 06.07.33.893 PM    201 ** 21-MAY-14 07.07.33.893 PM             60          120            200%

指令碼內容如下:

sqlplus -s $DB_CONN_STR@$SH_DB_SID <<EOF

set linesize 200
col begin_time format a35
col end_time format a35
col elapsed_time format 99999999.999
col workload_per format a20
SELECT
begin_time,end_time,
elapsed_time,
dbtime,
trunc(dbtime/decode(elapsed_time,0,1,elapsed_time),2)*100||'%' workload_per
from
(
select
B.SNAP_ID||' ** '||B.END_INTERVAL_TIME begin_time,
E.SNAP_ID||' ** '||E.END_INTERVAL_TIME end_time,
EXTRACT(DAY FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME) * 1440 +
EXTRACT(HOUR FROM E.END_INTERVAL_TIME - B.END_INTERVAL_TIME)* 60 +
EXTRACT(MINUTE FROM E.END_INTERVAL_TIME -B.END_INTERVAL_TIME) +
EXTRACT(SECOND FROM E.END_INTERVAL_TIME -B.END_INTERVAL_TIME) / 60  elapsed_time,
db_time.dbtime
FROM DBA_HIST_SNAPSHOT B, DBA_HIST_SNAPSHOT E,
                        (
                        SELECT b.snap_id begin_snap, e.snap_id end_snap ,
                        round((sum(e.value) -
                        sum(b.value)) / 1000000 /60,2) dbtime
                        FROM DBA_HIST_SYS_TIME_MODEL e, DBA_HIST_SYS_TIME_MODEL b,
                                        (
                                        select min(snap_id)  begin_snap,max(snap_id)  end_snap from DBA_HIST_SNAPSHOT where begin_interval_time > sysdate -1
                                        and EXTRACT(HOUR FROM END_INTERVAL_TIME) between $1 and $2
                                        ) temp_snap
                        WHERE
                         e.STAT_NAME = 'DB time'
                         and b.snap_id=temp_snap.begin_snap
                        and e.snap_id =temp_snap.end_snap
                        AND b.STAT_NAME = 'DB time'
                        group by e.snap_id,b.snap_id
                        ) db_time
WHERE b.begin_interval_time > sysdate -1
and EXTRACT(HOUR FROM e.END_INTERVAL_TIME) between $1  and $2
and b.snap_id=db_time.begin_snap
and e.snap_id=db_time.end_snap
)
/

EOF
exit

 

在此基礎上如果要檢視每個小時的資料庫負載情況,可以略做一些改進。

指令碼showdbtimerpt.sh

sqlplus -s $DB_CONN_STR@$SH_DB_SID <<EOF
prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set head off
set pages0
set feedback off
set serveroutput on
spool showdbtimerpt_tmp.sh
begin
for i in $1..$2 loop
dbms_output.put_line('ksh showdbtime  '||i||' '||(i+1));
end loop;
end;
/
spool off;

EOF
clear
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo BEGIN_TIME------------------------- END_TIME--------------------------- ELAPSED_TIME- BTIME----- WORKLOAD_PER--------
echo ----------------------------------- ----------------------------------- ------------- ---------- --------------------
ksh showdbtimerpt_tmp.sh
rm showdbtimerpt_tmp.sh
exit

 

執行結果類似下面的樣子。

BEGIN_TIME                                       END_TIME                                  ELAPSED_TIME       DBTIME      WORKLOAD_PER
----------------------------------- ----------------------------------- ------------- ---------- --------------------
201 ** 21-MAY-14 06.07.33.893 PM    201 ** 21-MAY-14 07.07.33.893 PM             60          120            200%

201 ** 21-MAY-14 07.07.33.893 PM    201 ** 21-MAY-14 08.07.33.893 PM             60          150            250%

201 ** 21-MAY-14 08.07.33.893 PM    201 ** 21-MAY-14 09.07.33.893 PM             60          240            400%

201 ** 21-MAY-14 09.07.33.893 PM    201 ** 21-MAY-14 10.07.33.893 PM             60          60              100%

201 ** 21-MAY-14 10.07.33.893 PM    201 ** 21-MAY-14 11.07.33.893 PM             60          120            200%

可以生成每個時間段的負載報表,這樣就一目瞭然了。可以有針對性的根據負載進行相關的效能抓取。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30633755/viewspace-2127759/,如需轉載,請註明出處,否則將追究法律責任。

相關文章