資料庫運作之自動檢查

2024/03/14閱讀時間約 9 分鐘

1. 問題:

(1)資料庫軟體每日運作產生的訊息,若不去檢查,將無法掌握運作狀況

(2)當發生嚴重之錯誤訊息,可能致資料庫故障,無法運作

(3) 錯誤訊息重複出現,一段期間後,若不採取對應,資料庫效能將受影響

(4) 若不去檢視錯誤訊息之log 檔案,致檔案過於膨大,系統將無法正常運行

(5) 若要人工檢查維護,須於系統手動輸入指令一一檢查,耗時且效率差

2. 改善:

(1)每日自動檢查資料庫運作所產生的訊息,若發現有錯誤,自動寄出警告信給擔當人員

(2)資料庫嚴重錯誤訊息出現,可以及早對應,避免系統突然故障之風險發生

(3)根據所發現的錯誤,事先調整運作參數,進行改善,可以防範未然

(4) 每日固定時間,排定自動執行檢查,並將執行結果以報表出力,讓負責人員可掌握運作狀況,確保重要系統能正常運作

(5)每日自動執行檢查,不須人工作業,可以節省工時,效率提升

3. 方法:

以ORACLE資料庫軟體為例

3.1 於 Windows cmd 內執行Oracle SQLPLUS , 設計執行SQL file

該SQL file功能為從Oracle alert log 檔案內,選取有錯誤的訊息,並將其出力txt檔

3.2 從上述出力之錯誤訊息檔案內,尋找特定錯誤字串,若有發現,則自動寄出警告信

3.3 警告email附加錯誤訊息之txt檔案,提供給擔當,及早防範對應


4. 舉例

4.1 STEP 1

(1) Cmd 內 執行 SQLPLUS 程式,進入SQLPLUS模式,執行 SQL file


(2)上述cmd 設計如下:

CALL SQLPLUS /nolog @D:\XXX-IT\DB_alert\select_PDB_alert.sql


SQL file select_PDB_alert.sql執行後,會產出錯誤訊息之txt檔案

txt檔名如下:

"select_DB_alert-output.txt"


4.2 STEP 2

(1)上述STEP 1 之sql 執行後,所產出之錯誤訊息txt檔案

從txt內容找尋是否有"ORA-" 開頭之訊息字串,若找到,則寄出警告email給擔當人員

(2)上述cmd 設計如下:

FOR /F   " delims=:  tokens=1 " %%i  in ('findstr /C:"ORA-"  "D:\XXX-IT\DB_alert\select_DB_alert-output.txt"  ') do (      
set r=Y
goto EMAIL
)

(3)上述出力檔案" select_DB_alert-output.txt"

其出力內容參考如下:       

raw-image

4.3本例設計作成bat 檔,以便可設定wimdows 自動執行排程,讓本項作業可以每日自動化執行,並自動寄出email。

 

(1)本作業執行檔Bat設計如下:  

rem STEP1 ----- in bat  to   execute SQLPLUS  and run  SQL file  -----------------
CALL SQLPLUS /nolog @D:\XXX-IT\DB_alert\select_PDB_alert.sql      
rem STEP2 ----- once find "ORA-" in output file then Send_email program ----
set r=N
FOR /F " delims=: tokens=1 " %%i in ('findstr /C:"ORA-" "D:\XXX-IT\DB_alert\select_DB_alert-output.txt" ') do (
set r=Y
goto EMAIL
)
:EMAIL
if %r% ==Y (
CALL XXX_mail_ReadF.exe )
rem pause ---- job DB_alert_select.bat finished -------------
exit 

(2) 上述sql file  “select_PDB_alert.sql” 設計如下:

SET ECHO OFF								
rem --- first to conn DB instances,可依序連上多個DB
rem --- then to exec the "select_dbgalertext.sql" for selecting error text in log
rem --- spool "D:\XXX-IT\DB_alert\select_DB_alert-output.txt" append
spool "D:\XXX-IT\DB_alert\select_DB_alert-output.txt"
set newpage 1
set linesize 132
set pagesize 0
prompt ' * ** *** The Oracle DB error List *** ** *;
prompt ;
prompt ;
rem ##### Noted that the statement before conn command must be had ";" as below
rem --- conn to TLIVE 後,再執行 select_dbgalertext.sql --------;
@D:\XXX-IT\DB_alert\conn_DB.sql sys PW-xxx TLIVE
@D:\XXX-IT\DB_alert\select_dbgalertext.sql TLIVE
spool off;
exit

(3)上述sql file : @D:\XXX-IT\DB_alert\conn_DB.sql sys PW-xxx TLIVE

設計內容如下:

conn &1/&2@&3 as sysdba;

rem --連接進入 DB: conn user/password@instance,值由3個引數 &1 &2 &3 取得

rem ---注意: conn之前之 command 須於尾端付有 ";"

(4)上述sql file @D:\XXX-IT\DB_alert\select_dbgalertext.sql TLIVE

設計內容如下:

rem ---本例錯誤訊息篩選條件: 含有 ORA-  字串 與 24 小時前的錯誤訊息
rem ---可選擇指定的錯誤訊息字串, 如:'%ORA-%' or '%TNS-%' or '%Fatal%' or '%error%' etc.
rem --- 可選擇 幾日前,或 幾小時前之 alert messges
prompt ============== &1 DB alert error Inquiry ==============;
select distinct
originating_timestamp, message_text
from x$dbgalertext
where
( originating_timestamp > sysdate - 24/24 )
and
( message_text like '%ORA-%'
);

---------------recorded by linct -------------------

8會員
47內容數
樂趣體驗紀錄,沙龍房間有歌唱,生活,科技
留言0
查看全部
發表第一個留言支持創作者!