利用shell腳本自動獲取awr報表
阿新 • • 發佈:2017-07-25
name begin tle 觀察 pre 報表 lec title 實現
觀察Oracle數據庫性能,oracle自帶的awr功能為我們提供了一個近乎完美的解決方案,通過awr特性我們可以隨時從數據庫提取awr報告。通過報告可以了解一個系統的整個運行情況,生成的報告包括多個部分。
如何獲取awr報告?
- 通過awrrpt.sql腳本執行
- 通過調用ORALCE的包dbms_workload_repository.awr_report_html/dbms_workload_repository.awr_report_text
獲取awr報告通常是采用html形式;打開頁面比較友好;也方便查看。
通過方法1 awrrpt.sql腳本執行需要我們提供一些交互信息。操作也比較簡單。本文通過shell腳本中來實現自動產生指定時段的awr報告。采用方法2。
1. 產生awr report 的sql腳本 autoawr.sql
SET ECHO OFF; SET VERI OFF; SET FEEDBACK OFF; SET TERMOUT ON; SET HEADING OFF; VARIABLE dbid NUMBER; VARIABLE inst_num NUMBER; VARIABLE bid NUMBER; VARIABLE eid NUMBER; BEGIN SELECT MIN (snap_id) INTO :bid FROM dba_hist_snapshot WHERE TO_CHAR (end_interval_time, ‘yyyymmdd‘) = TO_CHAR (SYSDATE-1, ‘yyyymmdd‘); SELECT MAX (snap_id) INTO :eid FROM dba_hist_snapshot WHERE TO_CHAR (begin_interval_time,‘yyyymmdd‘) = TO_CHAR (SYSDATE-1, ‘yyyymmdd‘); SELECT dbid INTO :dbid FROM v$database; SELECT instance_number INTO :inst_num FROM v$instance;END; / set pagesize 0; set linesize 121; COLUMN report_name NEW_VALUE report_name NOPRINT; SELECT instance_name || ‘_awrrpt_‘ || instance_number || ‘_‘ || b.timestamp || ‘.‘ || ‘html‘ report_name FROM v$instance a, (SELECT TO_CHAR (begin_interval_time, ‘yyyymmdd‘) timestamp FROM dba_hist_snapshot WHERE snap_id = :eid) b; SET TERMOUT OFF; SPOOL $AWR_DIR/&report_name; SELECT output FROM TABLE (DBMS_WORKLOAD_REPOSITORY.awr_report_html(:dbid, :inst_num, :bid, :eid)); SPOOL OFF; SET TERMOUT ON; CLEAR COLUMNS SQL; TTITLE OFF; BTITLE OFF; REPFOOTER OFF; UNDEFINE report_name
2. 產生awr report 的shell腳本autoawr.sh
#!/bin/bash if [ -f ~/.bash_profile ]; then source ~/.bash_profile fi export AWR_CMD=/home/oracle/awr export AWR_DIR=/home/oracle/awr/report RETENTION=31 # ---------------------------------------------- # Generate awr report # ---------------------------------------------- $ORACLE_HOME/bin/sqlplus / as sysdba<<EOF @${AWR_CMD}/autoawr.sql; exit; EOF # ------------------------------------------------ # Removing files older than $RETENTION parameter # ------------------------------------------------ find ${AWR_DIR} -name "*awrrpt*" -mtime +$RETENTION -exec rm {} \; exit
利用shell腳本自動獲取awr報表