(Oracle)自定义调用AWR&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

 

 

 

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐