Generate a range of AWR reports

The below procedure was originally written by someone else to grab a range of AWR reports in text format and send them to a directory.  You just need to submit the snap_id, inst_id, inst_name, and the directory where the files are sppoled to.  I found it useful and hope you like it too.


create or replace PROCEDURE CreateAwrReports (end_snap number,begin_snap number,dbid_num number,inst_num number,inst_name varchar2, directory varchar2 )

as

     v_Instance_number v$instance.instance_number%TYPE;
     v_Instance_name v$instance.instance_name%TYPE;
     v_instname v$instance.instance_name%TYPE;
     v_dbid V$database.dbid%TYPE;
     v_instnum v$instance.instance_number%TYPE;
     v_file UTL_FILE.file_type;

BEGIN


     SELECT instance_number, instance_name
     into v_Instance_number,v_Instance_name

     FROM   gv$instance
     ORDER BY 1;


     v_dbid := dbid_num;
     v_instnum := inst_num;
     v_instname := inst_name;


     EXECUTE IMMEDIATE('CREATE OR REPLACE DIRECTORY TEMP_DIR AS '''||directory||'''');



   FOR i IN begin_snap..end_snap-1 LOOP
         BEGIN
             --Creating and Naming the file:

             v_file := UTL_FILE.fopen('TEMP_DIR', 'awr_' || v_instname ||'_'|| v_instnum  || '_' || i || '_' || (i+1) || '.txt', 'w', 32767);

             FOR c_AWRReport IN (
                 SELECT output FROM TABLE (DBMS_WORKLOAD_REPOSITORY.AWR_REPORT_TEXT( v_dbid, v_instnum ,  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;

No comments:

Post a Comment