1. LINUX系统下:
##sh脚本,sh脚本调用sql脚本
#!/bin/bash
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
export AWR_CMD=/home/oracle/awr
export AWR_DIR=/home/oracle/awr/report
RETENTION=31
# ----------------------------------------------
# Generate awr report
# ----------------------------------------------
$ORACLE_HOME/bin/sqlplus / as sysdba<<EOF
@${AWR_CMD}/autoawr.sql;
exit;
EOF
exit
##sql脚本
SET ECHO OFF;
SET VERI OFF;
SET FEEDBACK OFF;
SET TERMOUT ON;
SET HEADING OFF;
VARIABLE dbid NUMBER;
VARIABLE inst_num NUMBER;
VARIABLE bid NUMBER;
VARIABLE eid NUMBER;
BEGIN
SELECT MIN (snap_id) INTO :bid FROM dba_hist_snapshot WHERE TO_CHAR (end_interval_time, ‘yyyymmdd hh24‘) > TO_CHAR (SYSDATE-2/24, ‘yyyymmdd hh24‘);
SELECT MAX (snap_id) INTO :eid FROM dba_hist_snapshot WHERE TO_CHAR (end_interval_time, ‘yyyymmdd hh24‘) > TO_CHAR (SYSDATE-2/24, ‘yyyymmdd hh24‘);
SELECT dbid INTO :dbid FROM v$database;
SELECT instance_number INTO :inst_num FROM v$instance;
END;
/
set pagesize 0;
set linesize 121;
COLUMN report_name NEW_VALUE report_name NOPRINT;
SELECT instance_name || ‘_awrrpt_‘ || instance_number || ‘_‘ || c.timestamp ||‘_‘ || b.timestamp || ‘.‘ || ‘html‘
report_name
FROM v$instance a,
(SELECT TO_CHAR (end_interval_time, ‘yyyymmdd_HH24MI‘) timestamp
FROM dba_hist_snapshot
WHERE snap_id = :eid) b,
(SELECT TO_CHAR (end_interval_time, ‘yyyymmdd_HH24MI‘) timestamp
FROM dba_hist_snapshot
WHERE snap_id = :bid) c;
SET TERMOUT OFF;
SPOOL $AWR_DIR/&report_name;
SELECT output
FROM TABLE (DBMS_WORKLOAD_REPOSITORY.awr_report_html(:dbid,
:inst_num,
:bid,
:eid));
SPOOL OFF;
SET TERMOUT ON;
CLEAR COLUMNS SQL;
TTITLE OFF;
BTITLE OFF;
REPFOOTER OFF;
UNDEFINE report_name
2. HP-UX系统下:
##sh脚本,sh脚本调用sql脚本
#!/sbin/sh
if [ -f ~/.profile ]; then
. ~/.profile
fi
export AWR_CMD=/home/oracle/awr
export AWR_DIR=/home/oracle/awr/report
RETENTION=31
# ----------------------------------------------
# Generate awr report
# ----------------------------------------------
$ORACLE_HOME/bin/sqlplus / as sysdba<<EOF
@${AWR_CMD}/autoawr.sql;
exit;
EOF
exit
##sql脚本
SET ECHO OFF;
SET VERI OFF;
SET FEEDBACK OFF;
SET TERMOUT ON;
SET HEADING OFF;
VARIABLE dbid NUMBER;
VARIABLE inst_num NUMBER;
VARIABLE bid NUMBER;
VARIABLE eid NUMBER;
BEGIN
SELECT MIN (snap_id) INTO :bid FROM dba_hist_snapshot WHERE TO_CHAR (end_interval_time, ‘yyyymmdd‘) = TO_CHAR (SYSDATE-7, ‘yyyymmdd‘);
SELECT MAX (snap_id) INTO :eid FROM dba_hist_snapshot WHERE TO_CHAR (begin_interval_time,‘yyyymmdd‘) = TO_CHAR (SYSDATE-1, ‘yyyymmdd‘);
SELECT dbid INTO :dbid FROM v$database;
SELECT instance_number INTO :inst_num FROM v$instance;
END;
/
set pagesize 0;
set linesize 121;
COLUMN report_name NEW_VALUE report_name NOPRINT;
SELECT instance_name || ‘_awrrpt_‘ || instance_number || ‘_‘ || c.timestamp ||‘_‘ || b.timestamp || ‘.‘ || ‘html‘
report_name
FROM v$instance a,
(SELECT TO_CHAR (end_interval_time, ‘yyyymmdd_HH24MI‘) timestamp
FROM dba_hist_snapshot
WHERE snap_id = :eid) b,
(SELECT TO_CHAR (end_interval_time, ‘yyyymmdd_HH24MI‘) timestamp
FROM dba_hist_snapshot
WHERE snap_id = :bid) c;
SET TERMOUT OFF;
SPOOL $AWR_DIR/&report_name;
SELECT output
FROM TABLE (DBMS_WORKLOAD_REPOSITORY.awr_report_html(:dbid,
:inst_num,
:bid,
:eid));
SPOOL OFF;
SET TERMOUT ON;
CLEAR COLUMNS SQL;
TTITLE OFF;
BTITLE OFF;
REPFOOTER OFF;
UNDEFINE report_name
【ORACLE】自动产生AWR报告
原文:https://www.cnblogs.com/CL-learning/p/12760621.html