1 需求
每天定时发送重要的数据库AWR到DBA邮件中,DBA接收到AWR邮件之后简单查看数据库是否有异常等待事情和异常SQL,尽量避免等用户发现数据库慢时再去分析相关SQL
2 相关环境
操作系统
本脚本是放在Red Hat下面运行,其它操作系统上面运行还未测试,因为使用的是dbms_workload_repository生成报表,所以可以分析任何操作系统下面的数据库,也就是说和数据库的操作系统没有关系
数据库版本
本脚本可以分析Oracle 10g ,11g任何子版本数据库
3 脚本详细介绍
脚本存放目录和文件
[oracle@svr7-71 awrrpt]$ pwd
/home/oracle/awrrpt
[oracle@svr7-71 awrrpt]$ ls -lrt
-rwxrwx--- 1 oracle oinstall 1968 May 24 11:05 awrrpt.sh
drwxr-xr-x 2 oracle oinstall 4096 May 24 11:09 reports
-rwxrwx--- 1 oracle oinstall 360 May 24 11:52 run.sh
其中awrrpt.sh为生成AWR的脚本,reports为存放生成后的AWR报告的文件夹
,run.sh为生成AWR的入口可以配置多个数据库
[oracle@svr7-71 awrrpt]$ more awrrpt.sh
#!/bin/bash
# $1 startTime
# $2 endTime
# $3 TNS user/passpassword@sid
# creater: eymit.wu
# last modified: 2012-05-23
. ~/.bash_profile
PROJECT_HOME='/home/oracle/awrrpt'
REPORT_HOME='/home/oracle/awrrpt/reports'
TODAY=`date +%Y%m%d`
cd ${PROJECT_HOME}
INSTANCE_NAME=`sqlplus -s "$3" <
set feed off
set term off
set echo off
set pages 0
set heading off
select instance_name from v\\$instance;
exit;
EOF`
FILE_NAME=`sqlplus -s "$3" <
set feed off
set term off
set echo off
set pages 0
set heading off
select '${REPORT_HOME}/awrrpt_' || to_char(sysdate, 'yyyymmdd') || '_' ||'$INSTANCE_NAME' || '_' || to_char(min(snap_id)) || '_' ||
to_char(max(snap_id)) ||'.html' file_name
from dba_hist_snapshot t
where t.end_interval_time between trunc(sysdate) + $1 / 24 and
trunc(sysdate) + ($2 + 1) / 24;
exit;
EOF`
echo " instance_name $INSTANCE_NAME"
echo " FILE_NAME $FILE_NAME"
sqlplus "$3" << EOF
column dbid new_value dbid;
select dbid from v\$database;
column instance_number new_value instance_number;
select instance_number instance_number from v\$instance;
column max_id new_value end_snap;
column min_id new_value begin_snap;
select to_char(max(snap_id)) max_id,
to_char(min(snap_id)) min_id
from dba_hist_snapshot t
where t.end_interval_time between trunc(sysdate) + $1 / 24 and
trunc(sysdate) + ($2 + 1) / 24;
set echo off;
set veri off;
set feedback off;
set termout on;
set heading off;
set linesize 1500;
set trimspool on;
spool $FILE_NAME;
select output from table(dbms_workload_repository.AWR_REPORT_HTML(&dbid, &instance_number,&begin_snap, &end_snap,0 ));
spool off;
EOF
echo "Report file name is : "$FILE_NAME
echo "AWR report of $INSTANCE_NAME " | mutt -s "[info]AWR report of $INSTANCE_NAME ${TODAY}" -a "$FILE_NAME" linux@hotmail.com
发送的邮件和主题不要忘记修改,run.sh我们在下面进行介绍
4 使用方法
可以看到调用awrrpt脚本需要传三个参数,分别为开始时间,结束时间,连接数据库的用户名密码还有tns,下面Run.sh配置了两个数据9点至11点的AWR报告,数据库只需要修改run.sh就可以了
[oracle@svr7-71 awrrpt]$ more run.sh
./awrrpt.sh 9 11 test1/test1@ora71
./awrrpt.sh 9 11 test1/test1@ora72
最后别忘记把要生成数据库的tnsname配置到tnsnames.ora中,最后把run.sh配置到crontab中
阅读(1419) | 评论(0) | 转发(0) |