Oracle->自定義調用AWR&ADDM 需求描述: 前面設定每天自動生成AWR用於提供前一天的資料庫狀態信息,但因資料庫和信息過多不利於直觀檢查。此次新增ADDM診斷。 ADDM診斷腳本分析: $ORACLE_HOME/rdbms/admin/addmrpt.sql--ADDM自動診斷資料庫的主 ...
Oracle->自定義調用AWR&ADDM
需求描述:
前面設定每天自動生成AWR用於提供前一天的資料庫狀態信息,但因資料庫和信息過多不利於直觀檢查。此次新增ADDM診斷。
ADDM診斷腳本分析:
$ORACLE_HOME/rdbms/admin/addmrpt.sql--ADDM自動診斷資料庫的主腳本
用於顯示當前的資料庫和實例,並調用@@addmrpti.sql腳本
[oracle@xxx~]$ cat $ORACLE_HOME/rdbms/admin/addmrpt.sql |grep -v "#"|grep -v "-"|grep -v "Rem"|grep -v "^$" define num_days = 3; set heading on echo off feedback off verify off underline on timing off; column inst_num heading "Inst Num" new_value inst_num format 99999; column inst_name heading "Instance" new_value inst_name format a12; column db_name heading "DB Name" new_value db_name format a12; column dbid heading "DB Id" new_value dbid format 9999999999 just c; prompt prompt Current Instance prompt ~~~~~~~~~~~~~~~~ select d.dbid dbid , d.name db_name , i.instance_number inst_num , i.instance_name inst_name from v$database d, v$instance i; @@addmrpti set heading on echo off feedback 6 verify on underline on timing off; undefine num_days undefine report_name undefine inst_num undefine inst_name undefine db_name undefine dbid
$ORACLE_HOME/rdbms/admin/addmrpti.sql ---查詢當前snapshot快照點,接收用戶輸入,設置和執行dbms_advisor程式包。
這兩個sql腳本用於接收和顯示awr快照點的信息下,需說明addm診斷報告會在每個awr快照點生成後自動產生。@@awrinput.sql
@@awrinpnm.sql &report_name_prefix &report_name_extension
[oracle@xxxx~]$ cat $ORACLE_HOME/rdbms/admin/addmrpti.sql |grep -v "#"|grep -v "-"|grep -v "Rem"|grep -v "^$" define report_name_prefix = 'addmrpt_'; define report_name_extension = '.txt'; set heading on echo off feedback off verify off underline on timing off; @@awrinput.sql @@awrinpnm.sql &report_name_prefix &report_name_extension set pagesize 0; set heading off echo off feedback off verify off; variable task_name varchar2(40); prompt prompt prompt Running the ADDM analysis on the specified pair of snapshots ... prompt begin declare id number; name varchar2(100); descr varchar2(500); BEGIN name := ''; descr := 'ADDM run: snapshots [' || :bid || ', ' || :eid || '], instance ' || :inst_num || ', database id ' || :dbid; dbms_advisor.create_task('ADDM',id,name,descr,null); :task_name := name; dbms_advisor.set_task_parameter(name, 'START_SNAPSHOT', :bid); dbms_advisor.set_task_parameter(name, 'END_SNAPSHOT', :eid); dbms_advisor.set_task_parameter(name, 'INSTANCE', :inst_num); dbms_advisor.set_task_parameter(name, 'DB_ID', :dbid); dbms_advisor.execute_task(name); end; end; / prompt prompt Generating the ADDM report for this analysis ... prompt prompt spool &report_name; set long 1000000 pagesize 0 longchunksize 1000 column get_clob format a80 select dbms_advisor.get_task_report(:task_name, 'TEXT', 'TYPICAL') from sys.dual; spool off; prompt prompt End of Report prompt Report written to &report_name. set termout off; clear columns sql; ttitle off; btitle off; repfooter off; set pagesize 14; set heading on echo off feedback 6 verify on underline on timing off; set long 80 longchunksize 80 set termout on undefine dbid undefine inst_num undefine num_days undefine begin_snap undefine end_snap undefine report_name undefine report_name_prefix undefine report_name_extension whenever sqlerror continue;
開始構造自定義SQL腳本<ora_auto_addmrpt.sql>
[oracle@xxxxxx~]$ cat /opt/app/oracle/ora_auto/ora_auto_addmrpt.sql set echo off; set veri off; set feedback off; set termout on; set heading off; set pagesize 0; set heading off echo off feedback off verify off; variable task_name varchar2(40); variable dbid number; variable inst_num number; variable bid number; variable eid number; variable dbname varchar2(9); variable btime number; variable etime number; begin select dbid into :dbid from v$database; select instance_number into :inst_num from v$instance; select max(snap_id)-24 into :bid from dba_hist_snapshot where DBID=:dbid; select max(snap_id) into :eid from dba_hist_snapshot where DBID=:dbid; select lower(NAME) into :dbname from v$database; select to_char(end_interval_time,'YYYYMMDDHH24') into :btime from dba_hist_snapshot where snap_id=:bid; select to_char(end_interval_time,'DDHH24') into :etime from dba_hist_snapshot where snap_id=:eid; end; / --prompt --prompt --prompt Running the ADDM analysis on the specified pair of snapshots ... --prompt begin declare id number; name varchar2(100); descr varchar2(500); BEGIN name := ''; descr := 'ADDM run: snapshots [' || :bid || ', ' || :eid || '], instance ' || :inst_num || ', database id ' || :dbid; dbms_advisor.create_task('ADDM',id,name,descr,null); :task_name := name; dbms_advisor.set_task_parameter(name, 'START_SNAPSHOT', :bid); dbms_advisor.set_task_parameter(name, 'END_SNAPSHOT', :eid); dbms_advisor.set_task_parameter(name, 'INSTANCE', :inst_num); dbms_advisor.set_task_parameter(name, 'DB_ID', :dbid); dbms_advisor.execute_task(name); end; end; / --prompt --prompt Generating the ADDM report for this analysis ... --prompt --prompt column report_name new_value report_name noprint; select :dbname||'_'||:btime||'to'||:etime||'.'||'txt' report_name from dual; set termout off; spool &report_name; set long 1000000 pagesize 0 longchunksize 1000 column get_clob format a80 select dbms_advisor.get_task_report(:task_name, 'TEXT', 'TYPICAL') from sys.dual; spool off; set termout on; prompt &report_name undefine dbid undefine inst_num undefine report_name exit
將ADDM sql腳本整合到自定義調用AWR shell腳本中
Script: [oracle@01 ~]$ cat /opt/app/oracle/ora_auto/oracle_auto_awr.sh #!/bin/bash # auto gather awrrpt.sql # # parameters: # $1 -> SID # # The whole package includes 2 files; # oracle_auto_awr.sh(this file) # ora_auto_awrrpt.sql # #引用oracle資料庫環境變數 . /home/oracle/.bash_profile ->(註意加上oracle環境變數,不然crontab排程運行時不能正常執行sql腳本). /home/oracle/.bash_profile PATH=$PATH:$HOME/bin:/sbin:/usr/bin:/usr/sbin ORACLE_BASE=/opt/app/oracle;export ORACLE_BASE ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1;export ORACLE_HOME ORA_AUTO_PATH='/opt/app/oracle/ora_auto' #自定義awr腳本的SQL語句 ORACLE_SQL=$ORA_AUTO_PATH/ora_auto_awrrpt.sql
ORACLE_ADDM_SQL=$ORA_AUTO_PATH/ora_auto_addmrpt.sql DAY_TIME=`date +"%Y%m%d"` AWR_ZIP=AWR_$DAY_TIME.zip cd $ORA_AUTO_PATH echo "Dear Sir:">mail.log echo " This is oracle database Awr.">>mail.log mkdir -p $ORA_AUTO_PATH/$DAY_TIME #腳本後跟上多個資料庫名,用於集合 array_1=$@ for sid in ${array_1[@]} do sleep 30 tnschk=`tnsping $sid` tnschk2=` echo $tnschk | grep -c OK` if [ ${tnschk2} -eq 1 ] ; then echo " $sid start gather." >>mail.log ###連接資料庫字元串,註意連接資料庫的賬戶是否有許可權執行awr ORACLE_CONN='xxx/xxxx@'$sid $ORACLE_HOME/bin/sqlplus -s $ORACLE_CONN @$ORACLE_SQL >sqlplus_$sid_output.log $ORACLE_HOME/bin/sqlplus -s $ORACLE_CONN @$ORACLE_ADDM_SQL >>sqlplus_${sid}_output.log
###將生成的檔案名提取出來 soput="`cat $ORA_AUTO_PATH/sqlplus_$sid_output.log|grep html`"
addmsoput="`cat $ORA_AUTO_PATH/sqlplus_${sid}_output.log|grep txt`"
if [ -f $soput ]; thenecho " mv $soput $ORA_AUTO_PATH/$DAY_TIME">>mail.log mv $soput $ORA_AUTO_PATH/$DAY_TIME
mv $addmsoput $ORA_AUTO_PATH/$DAY_TIME fi else echo " No TNS Listener on $sid" >>mail.log fi done echo " zip -r $AWR_ZIP $DAY_TIME" >>mail.log #`/bin/tar -zcf $AWR_ZIP $DAY_TIME` 將awr報表進行壓縮,減少郵件傳輸的流量 `/usr/bin/zip -r $AWR_ZIP $DAY_TIME` echo " Thanks!">>mail.log mutt -s "[CQ][SFIS DATABASE][AWR]: time: `date '+%y%m%d-%H%M'`" "接受郵件地址" -a $ORA_AUTO_PATH/$AWR_ZIP < $ORA_AUTO_PATH/mail.log rm -rf $DAY_TIME 設定排程[xxx.xxx.xx.x] 15 07 * * * /xxx/app/oracle/ora_auto/oracle_auto_awr.sh oracle01 oracle02 oracle03