Oracle->自動發送AWR報告 2016年9月21日 09:31 需求描述: 每日或定期手動使用AWR報告來檢查Oracle資料庫狀態不僅耗時也費力,需求使用腳本自動收集AWR報告。 分析方案: 收集AWR報告的腳本:$ORACLE_HOME/rdbms/admin/awrrpt.sql ->主 ...
Oracle->自動發送AWR報告 2016年9月21日 09:31 需求描述: 每日或定期手動使用AWR報告來檢查Oracle資料庫狀態不僅耗時也費力,需求使用腳本自動收集AWR報告。
分析方案: 收集AWR報告的腳本:$ORACLE_HOME/rdbms/admin/awrrpt.sql ->主要運行腳本。 首先來查看腳本都做了寫什麼,用了哪些視圖。 cat $ORACLE_HOME/rdbms/admin/awrrpt.sql |grep -v 'Rem'|grep -v '^--' (排除‘Rem’和‘--’相關 得到主要信息-> 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; 和關聯腳本@@awrrpti 再查看@@awrrpti腳本都執行了些什麼 cat $ORACLE_HOME/rdbms/admin/awrrpt.sql |grep -v 'Rem'|grep -v '^--' (排除‘Rem’和‘--’相關註釋)
得到核心內容->(這就是生成AWR報告的程式)
spool &report_name; select output from table(dbms_workload_repository.&fn_name( :dbid, :inst_num, :bid, :eid, :rpt_options )); spool off; select output from table(dbms_workload_repository.&fn_name( :dbid,:inst_num,:bid, :eid,:rpt_options )); &fn_name->類型名稱如awr_report_html和awr_report_text :dbid->資料庫id號 :inst_num->實例名 :bid->snap開始id :eid->snap結束id :rpt_options->控制顯示ADDM
關聯腳本 @@awrinput.sql ->主要獲取snapshots id號 @@awrinpnm.sql 'awrrpt_' &&ext ->主要生成AWR報告名稱 AWR生成腳本的流程: @awrrpt.sql(提供dbid和inst_num) @@awrrpti.sql(主程式腳本) @@awrinput.sql(提供snap id) @@awrinpnm.sql(提供awr生成名稱) select output from table(dbms_workload_repository.&fn_name( :dbid,:inst_num,:bid, :eid,:rpt_options ));(生成awr報表) 制定腳本:<ora_auto_awrrpt.sql> 複製 @@awrrpti.sql內容並且加以更正就行
--conn / as sysdba; set echo off; set veri off; set feedback off; set termout on; set heading off; variable rpt_options number; define NO_OPTIONS = 0; define ENABLE_ADDM = 8; -- according to your needs, the value can be 'text' or 'html' define report_type='html'; begin :rpt_options := &NO_OPTIONS; end; / 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;--生成的awr 快照開始時間可以自定義。註意如果中間有重啟資料庫,會導致獲取awr報告失敗。 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; / column ext new_value ext noprint column fn_name new_value fn_name noprint; column lnsz new_value lnsz noprint; --select 'txt' ext from dual where lower('&report_type') = 'text'; select 'html' ext from dual where lower('&report_type') = 'html'; --select 'awr_report_text' fn_name from dual where lower('&report_type') = 'text'; select 'awr_report_html' fn_name from dual where lower('&report_type') = 'html'; --select '80' lnsz from dual where lower('&report_type') = 'text'; select '1500' lnsz from dual where lower('&report_type') = 'html'; select :dbname||'_'||:btime||'to'||:etime||'.'||'&ext' report_name from dual; set linesize &lnsz; -- print the AWR results into the report_name file using the spool command: column report_name new_value report_name noprint; select :dbname||'_'||:btime||'to'||:etime||'.'||'&ext' report_name from dual; set termout off; spool &report_name; select output from table(dbms_workload_repository.&fn_name(:dbid, :inst_num,:bid, :eid,:rpt_options )); spool off; set termout on; clear columns sql; ttitle off; btitle off; repfooter off; undefine report_name undefine report_type undefine fn_name undefine lnsz undefine NO_OPTIONS exit
設定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 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 soput="`cat $ORA_AUTO_PATH/sqlplus_$sid_output.log|grep html`" if [ -f $soput ]; thenecho " mv $soput $ORA_AUTO_PATH/$DAY_TIME">>mail.log mv $soput $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 "[xxxx][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
針對腳本進行加密 使用shc工具 wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.7.tgz 參考http://www.thegeekstuff.com/2012/05/encrypt-bash-shell-script/ [root@xxx oracle]# tar xvfz shc-3.8.7.tgz [root@xxxx shc-3.8.7]# ./shc -r -f oracle_auto_awr.sh ->對shell腳本進行加密 oracle_auto_awr.sh ->(原文件) oracle_auto_awr.sh.x ->(二進位文件) oracle_auto_awr.sh.x.c ->(C語言) shc常用參數: -e date Expiration date in dd/mm/yyyy format [none](指定過期日誌) -m message Message to display upon expiration ["Please contact your provider"](指定提示的信息) -f script_name File name ofr the script to complie(指定要編譯的shell路徑及文件名) -f Relax security. Make a redistributable binary which executes on different systems run(可以相同操作系統的不同系統中執行) -V Verbose compilation(編譯的詳細情況) 參考: http://blog.csdn.net/tianlesoftware/article/details/6319198 http://blog.itpub.net/12457158/viewspace-753875/