Liron Amitzi Oracle ACE

Oded Raz Oracle ACE Director

Creating Multiple AWR reports

Posted by Roni Vered on Jun 22nd, 2010 and filed under Administration, Oracle, Oracle - Latest Articles, Tips & Tricks. You can follow any responses to this entry through the RSS 2.0. You can leave a response or trackback to this entry

Analyzing AWR (Automated Workload Repository) is a common operation while doing a performance and tuning sessions on a database instance.

If your database contains the Oracle Database Diagnostic Pack, then by default, the database generates an AWR snapshot in an hourly interval (it of course can be changed by using the DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS procedure).

Once taken, each two snapshots can be compared in an AWR report and greatly assist in the performance and tuning job.

There’re several methods of generating these AWR report, however, most of the methods are manual methods – creating one report at the time.

The following procedure will create several AWR reports in HTML format in a given directory, based on a begin/end snapshot id parameters.

Procedure’s Parameters:

  • Begin_snap – the first AWR snapshot to be compared.
  • End_snap – the last AWR snapshot to be compared.
  • Directory – the directory in which it will generate the reports

The required begin/end snapshot values can be retrieved from the following query on DBA_HIST_SNAPSHOT:


SELECT     	SNAP_ID,
                TO_CHAR(BEGIN_INTERVAL_TIME,'DD/MM/YYYY HH24:MI') ,
                TO_CHAR(END_INTERVAL_TIME,'DD/MM/YYYY HH24:MI')</pre>
FROM DBA_HIST_SNAPSHOT 
--The following WHERE clause can be used in order to limit the snapshot details.
--WHERE TO_CHAR(BEGIN_INTERVAL_TIME,'DD/MM/YYYY') > TO_CHAR(SYSDATE -2,'DD/MM/YYYY'); 

Once Created, the procedure can be executed as the following example:

EXEC CreateAwrReports (465, 475, ‘C:\TEMP’);

Below is the procedure’s script:

CREATE OR REPLACE PROCEDURE CreateAwrReports (begin_snap number,end_snap number, directory varchar2 )
as
/*---------------------------------------------------------------------------
 Name: CreateAwrReports Procedure
 Purpose : 	Create several AWR reported based on begin_snap and end_snap input parameters.
			The AWR reports will be created in the directory input parameter.
 Date    : 06.21.2010.
 Author  : Roni Vered
 Website : WWW.DBSNAPS.COM
 Execution :	exec CreateAwrReports (462, 472, 'c:\temp');

 Remarks : Run as privileged user
 --------------------------------------------------------------------------- */
	v_Instance_number v$instance.instance_number%TYPE;
	v_Instance_name v$instance.instance_name%TYPE;
	v_dbid V$database.dbid%TYPE;
	v_file UTL_FILE.file_type;

BEGIN

/* Collecting instance information: Instance_number, Instance_name and Dbid */
	SELECT instance_number, instance_name
	into v_Instance_number,v_Instance_name
	FROM   gv$instance
	ORDER BY 1;

	SELECT dbid
	INTO v_dbid
	FROM v$database;

/* Creating a database directory which will point to the acual wanted report directory in the OS */
	EXECUTE IMMEDIATE('CREATE OR REPLACE DIRECTORY TEMP_DIR AS '''||directory||'''');

/*
	Looping on all the snapshots from the begin_snap input parameter to the end_snap input parameter
	On each snapshot pair we will create a file in the given directory which will contain the AWR report.
	We use DBMS_WORKLOAD_REPOSITORY.AWR_REPORT_HTML build in procedure to create the HTML report.
*/
  FOR i IN begin_snap..end_snap-1 LOOP
		BEGIN
			--Creating and Naming the file:
			v_file := UTL_FILE.fopen('TEMP_DIR', 'awr_' || v_Instance_name ||'_'|| v_Instance_number || '_' || i || '_' || (i+1) || '.html', 'w', 32767);
			FOR c_AWRReport IN (
				SELECT output FROM TABLE (DBMS_WORKLOAD_REPOSITORY.AWR_REPORT_HTML( v_dbid, v_Instance_number,  i, i+1))
					   ) LOOP
						--Writing the AWR HTML report content to the file:
						UTL_FILE.PUT_LINE(v_file, c_AWRReport.output);
					   END LOOP;
			--Closing the file:
			UTL_FILE.fclose(v_file);
		END;
	END LOOP;

/* Dropping the database directory which we've created earlier. */
	EXECUTE IMMEDIATE('DROP DIRECTORY TEMP_DIR');

END;

3 Responses for “Creating Multiple AWR reports”

  1. zamaracha says:

    hi it doesn’t work:(
    when executed awrrpt it works properly

    error message below:
    SQL> EXEC CreateAwrReports (14, 16, ‘C:\TEMP’);
    BEGIN CreateAwrReports (14, 16, ‘C:\TEMP’); END;

    *
    ERROR at line 1:
    ORA-29283: invalid file operation
    ORA-06512: at “SYS.UTL_FILE”, line 488
    ORA-29283: invalid file operation
    ORA-06512: at “SYS.CREATEAWRREPORTS”, line 42
    ORA-06512: at line 1

    Any help would be much appreciated.

  2. Roni Vered says:

    Hi zamaracha,
    1. Did you execute the procedure as SYS ?
    2. Did the directory c:\temp exists in your computer and you have permissions to create files on it?

    I took the procedure, implement it and ran it with no issues on c:\temp, however, when I ran it with a non-existing directory, I got you error:

    SQL> exec sys.CreateAwrReports (1367,1368, ‘c:\temp\trilili’);
    BEGIN sys.CreateAwrReports (1367,1368, ‘c:\temp\trilili’); END;

    *
    ERROR at line 1:
    ORA-29283: invalid file operation
    ORA-06512: at “SYS.UTL_FILE”, line 536
    ORA-29283: invalid file operation
    ORA-06512: at “SYS.CREATEAWRREPORTS”, line 42
    ORA-06512: at line 1

  3. zamaracha says:

    Hi Roni,I’ve tested this procedure successfully under the Linux…- I updated script execution to be compliant with Linux environment (ie.: replaced c:\tmp with /tmp)- testing as a sys user- having an access to /tmp directory- testing in 10g and 11g database- running the following command:exec CreateAwrReports (440, 450, ‘/tmp’);Thanks for the script.If I could suggest something different which doesn’t requires installing any procedures in SYS schema:#!/bin/bashif [ $# != 4 ]thenecho “Syntax  genawrs.sh dbid instanceId startsnapid endsnapid”exit 1fil_dbid=$1l_instid=$2l_start_snapid=$3let l_end_snapid=$4-1# For all snapshot id’s# Set the next snapshot id as current snapshot id + 1# Spool a log file# Log into sqlplus and call dbms_workload_repository.awr_report_text# To generate the awrfor i in `seq $l_start_snapid $l_end_snapid`dolet l_next_snapid=$i+1;l_awr_log_file=awrs_out/awrrpt_${2}_${i}_${l_next_snapid}.htmlsqlplus -s / as sysdba << EOCset head offset pages 0set lines 132set echo offset feedback offspool $l_awr_log_fileSELECToutputFROMTABLE(dbms_workload_repository.awr_report_html($l_dbid,$l_instid,$i,$l_next_snapid));spool offEOCdoneCheers!!zamaracha

Leave a Reply

 
Log in / Advanced NewsPaper by Gabfire Themes