(Oracle) custom call AWR

Keywords: Oracle SQL Database shell

Oracle - > auto send AWR Report
September 21, 2016
09:31
Requirement Description:
Daily or regular manual use of AWR reports to check the status of Oracle database is not only time-consuming but also labor-intensive, requiring the use of scripts to automatically collect AWR reports.
 

 

Analysis scheme:
Script to collect AWR report: $oracle_home / RDBMS / admin / awrrpt. SQL - > main run script.
First, let's see what the script has done and what views it has used.
Cat $Oracle | home / RDBMS / admin / awrrpt.sql | grep - V 'Rem' | grep - V '^ --' (exclude 'Rem' and '--'
Get main information - >
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;
And associated scripts @ awrrpti
See what the @ awrrpti script does
Cat $Oracle | home / RDBMS / admin / awrrpt.sql | grep - V 'Rem' | grep - V '^ --' (exclude comments related to 'Rem' and '--'
   

 

Get the core content - >
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->The type name is as follows awr_report_html and awr_report_text
:dbid->data base id Number
:inst_num->Instance name
:bid->snap start id
:eid->snap End id
:rpt_options->Control display ADDM

 

 
Association script
@@awrinput.sql - > get snapshot ID
@@Awrinpnm.sql 'awrrpt' & & ext - > main AWR report name
 
AWR script generation process:
@awrrpt.sql (provide dbid and inst_num)
@ @ awrrpti.sql (main script)
@ @ awrinput.sql (provide snap id)
@ @ awrinpnm.sql (provide awr build name)
Select output from table (DBMS ﹣ workload ﹣ repository. & FN ﹣ name (: dbid,: Inst ﹣ num,: bid,: Eid,: RPT ﹣ options)); (generate awr report)
Script development: < ora auto awrrpt. SQL >
Copy the contents of @ awrrpti.sql and correct them
 
--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;--Generated awr The snapshot start time can be customized. Note that if you restart the database in the middle, you will get awr Report failed.
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

 

 

Set the timing of shell script execution and sending mail.

 
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
#
 
#Quote oracle Database environment variable 
.
/home/oracle/.bash_profile ->(Attention plus oracle Environment variable, otherwise crontab The schedule cannot be executed normally sql Script). /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' #SQL statement of custom awr script 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
#Script followed by multiple database names for collection 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
###Connect to the database string. Note whether the account connected to the database has permission to perform 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` take awr Report compression reduces mail traffic `/usr/bin/zip -r $AWR_ZIP $DAY_TIME` echo " Thanks!">>mail.log mutt -s "[xxxx][DATABASE][AWR]: time: `date '+%y%m%d-%H%M'`" "Accept email address" -a $ORA_AUTO_PATH/$AWR_ZIP < $ORA_AUTO_PATH/mail.log rm -rf $DAY_TIME //Set schedule [x x x. XXX. XX. X] 15 07 * * * /xxx/app/oracle/ora_auto/oracle_auto_awr.sh oracle01 oracle02 oracle03

 

 

 
 
Encrypt for script
[root@xxx oracle]# tar xvfz shc-3.8.7.tgz
[root @ XXXX shc-3.8.7]. / SHC - R - f Oracle auto awr.sh - > encrypt shell script
Oracle auto AWR. SH - >
Oracle auto AWR. Sh.x - > binary
Oracle auto AWR. Sh.x.C - >
Common parameters of 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 of the script to compile
-f Relax security.
Make a redistributable binary which executes on different systems run
-V Verbose compilation
 
 
 
 
Reference resources:

Posted by DoomyCheese on Tue, 31 Mar 2020 21:18:39 -0700