您的位置:首页 > 数据库 > Oracle

Oracle AWR报告自动生成并ftp脚本

2013-07-09 14:22 369 查看
脚本主要由以下几个部分组成:

awr.sql 用来在sqlplus 中执行的脚本,该脚本从网上直接找的。

awr.sh 用来调用sqlplus来生成awr报告。

ftp.sh 用来打包压缩每天生成的awr报告(压缩率大于50倍),并进行ftp传输,清理过期的报告,对于linux和solaris略有不同。

crontab 用来执行定时任务,根据需求进行调整。

下面为具体的脚本内容,可以根据需要进行调整。

awr.sql :

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;

rem 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;
begin
select max(snap_id)-1 into :bid from dba_hist_snapshot;
select max(snap_id) into :eid from dba_hist_snapshot;
select dbid into :dbid from v$database;
select instance_number into :inst_num from v$instance;
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';

set linesize &lnsz;

column report_name new_value report_name noprint;

select 'sp_'||:bid||'_'||:eid||'.'||'&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


awr.sh:

mydate='date +%y%m%d'
ORACLE_SID=orcl; export ORACLE_SID
ORACLE_BASE=/opt/app/ora11g; export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1; export ORACLE_HOME
cd /opt/awr
$ORACLE_HOME/bin/sqlplus /nolog<<!
connect / as sysdba;
@awr.sql
exit
!


ftp.sh(RHEL6版本):

#!/usr/bin/bash
mydate=`date +%y%m%d`
mydir=/opt/awr
cd ${mydir}
find *.html -daystart -mtime -1 | xargs tar -zcvf awr_${mydate}.tar.gz
echo "======================FTP start========================="
ftp -n<<!
open 11.11.11.11 21
user username passwd
binary
lcd /opt/awr
cd /ftp/orcl
put awr_${mydate}.tar.gz
close
bye
!
echo "=======================FTP end============================"
echo "=================delete the tar file====================="
rm awr_${mydate}.tar.gz
echo "=================delete the tar file end====================="
echo "=================delete the old file ====================="
find ${mydir} -name "*.html" -type f -mtime +3 -exec rm {} \;
echo "=================delete the old file end====================="


ftp.sh (Solaris 10版本):

#!/usr/bin/sh
mydate=`date +%y%m%d`
mytoday=`date +%m%d`
mydir=/opt/awr
cd ${mydir}
touch ${mytoday}0000 TODAY
find *.html -newer TODAY | xargs tar -cvf awr_${mydate}.tar
gzip -c  awr_${mydate}.tar > awr_${mydate}.tar.gz
echo "======================FTP start========================="
ftp -n<<!
open 11.11.11.11 21
user username passwd
binary
lcd /opt/awr
cd /ftp/orcl
put awr_${mydate}.tar.gz
close
bye
!
echo "=======================FTP end============================"
echo "=================delete the tar and temp file====================="
rm awr_${mydate}.tar
rm TODAY
rm awr_${mydate}.tar.gz
echo "=================delete the tar and temp file end====================="
echo "=================delete the old file ====================="
find ${mydir} -name "*.html" -type f -mtime +0 -exec rm -rf {} \;
echo "=================delete the old file end====================="


crontab:

0 0-23 * * * sh /opt/awr/awr.sh
5 23 * * * sh /opt/awr/ftp.sh
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: