Automatically generate an AWR report using the Stored Procedure

Source: Internet
Author: User
The content of the stored procedure is as follows: CREATEORREPLACEPROCEDUREsp_auto_awrreport (reporttimevarchar2defaultnull,

The following is the content of the stored PROCEDURE: create or replace procedure sp_auto_awrreport (reporttime varchar2 default null,

The content of the stored procedure is as follows:

Create or replace procedure sp_auto_awrreport (reporttime varchar2 default null,
Rpt_interval number default 2)
AS
---- Purpose: automatically generate an AWR report
---- Created by: cavlin
---- Creation date: 20130210
---- Reporttime Report End Time
---- Rpt_interval report interval:, 2: 60 minutes
Start_snap number;
End_snap number;
Start_time varchar2 (14 );
End_time varchar2 (14 );
Awr_file utl_file.file_type;
N_dbid number;

BEGIN

If reporttime is null then
--- Query the largest snapshot id
Select max (snap_id) into end_snap from dba_hist_snapshot;
Start_snap: = end_snap-rpt_interval;
Else
--- Take the snap_id corresponding to the input time
Select snap_id
Into end_snap
From dba_hist_snapshot
Where to_char (end_interval_time, 'yyyymmddhh24mi ') = reporttime
And instance_number = 1;
Start_snap: = end_snap-rpt_interval;
End if;

--- Format snapshot time
Select dbid, to_char (end_interval_time-rpt_interval/48, 'yyyymmddhh24mi '), to_char (end_interval_time, 'yyyymmddhh24mi ')
Into n_dbid, start_time, end_time
From dba_hist_snapshot
Where snap_id = end_snap and instance_number = 1;

--- Instance 1
Awr_file: = utl_file.fopen ('dir _ awrreport', 'awr _ NODE1 _ '| start_time |' _ '| end_time | '.html', 'A', 32767 );
For awr_info in (select output from table (dbms_workload_repository.awr_report_html (n_dbid, 1, start_snap, end_snap, 0) loop
UTL_FILE.put_line (awr_file, awr_info.output );
End loop;
Utl_file.fclose (awr_file );

--- Instance 2
Awr_file: = utl_file.fopen ('dir _ awrreport', 'awr _ NODE2 _ '| start_time |' _ '| end_time | '.html', 'A', 32767 );
For awr_info in (select output from table (dbms_workload_repository.awr_report_html (n_dbid, 2, start_snap, end_snap, 0) loop
UTL_FILE.put_line (awr_file, awr_info.output );
End loop;
Utl_file.fclose (awr_file );

END sp_auto_awrreport;

It can be called directly in the database or through shell:

Cat/opt/cron/autoawr. sh
#! /Bin/bash

Sqlplus-s/as sysdba <EOF
Exec sp_auto_awrreport;
Exit
EOF

Find/opt/awrreport/-mtime + 7-name "*. html"-exec rm-f {}\;

Call through crontab:

$ Crontab-l
5 22 *** sh/opt/cron/autoawr. sh

In this way, the awr Report of-is automatically generated in the/opt/awrreport directory every day.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.