【DB笔试面试821】在Oracle中,如何定时生成AWR报告?
♣ 答案部分
有的系统需要定时生成html格式的AWR报告,这个需求可以使用SHELL脚本来完成。
下面给出相应的SHELL脚本:
1[oracle@rhel6lhr awr]$ ll
2total 68
3-rwxr-xr-x. 1 oracle oinstall 3112 Oct 23 2014 autogetawr.sh
4drwxr-xr-x. 2 oracle oinstall 49152 May 23 11:05 log
5-rw-r--r--. 1 oracle oinstall 276 Oct 22 2014 oracle_env.conf
6-rw-r--r--. 1 oracle oinstall 235 Oct 22 2014 readme.txt
7-rwxr-xr-x. 1 oracle oinstall 303 Oct 23 2014 rungetawr.sh
8
9[oracle@rhel6lhr ~]$ crontab -l
105 * * * * /home/oracle/lhr/awr/rungetawr.sh
11[oracle@rhel6lhr ~]$ more /home/oracle/lhr/awr/rungetawr.sh
12#!/bin/bash
13# define end snapshot time
14NH=`date +%Y%m%d%H`
15# define begin snapshot time
16LH=`date -d "last-hour" +%Y%m%d%H`
17# define the report format HTML or TEXT
18FH=HTML
19#get the awr
20#echo autogetawr.sh -f $LH -t $NH p $FH
21. $HOME/lhr/awr/oracle_env.conf
22$SCRIPT_DIR/autogetawr.sh -f $LH -t $NH -p $FH
23[oracle@rhel6lhr ~]$ more $HOME/lhr/awr/oracle_env.conf
24export ORACLE_BASE=/u01/app/oracle
25export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
26export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib
27export PATH=$ORACLE_HOME/bin:$PATH
28export ORACLE_USER=lhr/lhr@orclasm
29
30export SCRIPT_DIR=$HOME/lhr/awr
31export LOG_DIR=$SCRIPT_DIR/log
32[oracle@rhel6lhr ~]$ more /home/oracle/lhr/awr/autogetawr.sh
33# !/bin/bash
34# get the env about oracle
35# must define SID and ORACLE_HOME in profile
36#. ~oracle/.bash_profile
37. $HOME/lhr/awr/oracle_env.conf
38
39# ********************************
40# * dba_oracle_awr.sh
41# ********************************
42# Usage: dba_oracle_awr.sh
43# -f [from time]
44# -t [to time]
45# -p [report type, html or text]
46#
47# time format: 'yyyymmddhh24'.
48# E.g 2011030417 means 05pm, Mar 04, 2011
49#**********************
50# get parameters
51#**********************
52while getopts ":f:t:p" opt
53 do
54 case $opt in
55 f) from=$OPTARG
56 ;;
57 t) to=$OPTARG
58 ;;
59 p) type=$OPTARG
60 type=$(echo $type|tr "[:upper:]" "[:lower:]")
61 ;;
62 '?') echo "$0: invalid option ?$OPTARG">&2
63 exit 1
64 ;;
65 esac
66done
67
68if [ "$from" = "" ]
69then
70 echo "from time (?f} needed"
71 echo "program exiting..."
72 exit 1
73fi
74if [ "$to" = "" ]
75then
76 echo "to time (?t) needed"
77 echo "program exiting..."
78 exit 1
79fi
80if [ "$type" = "" ]
81then
82 type="html"
83fi
84
85
86# ********************
87# trim function
88# ********************
89function trim()
90{
91 local result
92 result=`echo $1|sed 's/^ *//g' | sed 's/ *$//g'`
93 echo $result
94}
95
96#*******************************
97# get begin and end snapshot ID
98# *******************************
99define_dur()
100{
101begin_id=`sqlplus -s $ORACLE_USER <<EOF
102 set pages 0
103 set head off
104 set feed off
105 select max(SNAP_ID) from DBA_HIST_SNAPSHOT where
106 END_INTERVAL_TIME<=to_date($from,'yyyymmddhh24');
107EOF`
108ret_code=$?
109if [ "$ret_code" != "0" ]
110then
111 echo "sqlplus failed with code $ret_code"
112 echo "program exiting..."
113 exit 10
114fi
115end_id=`sqlplus -s $ORACLE_USER <<EOF
116 set pages 0
117 set head off
118 set feed off
119 select min(SNAP_ID) from DBA_HIST_SNAPSHOT where
120 END_INTERVAL_TIME>=to_date($to,'yyyymmddhh24');
121 spool off
122EOF`
123ret_code=$?
124if [ "$ret_code" != "0" ]
125then
126 echo "sqlplus failed with code $ret_code"
127 echo "program exiting..."
128 exit 10
129fi
130begin_id=$(trim ${begin_id})
131end_id=$(trim ${end_id})
132#echo "begin_id: $begin_id end_id: $end_id"
133}
134
135#*******************************
136# generate AWR report
137# *******************************
138generate_awr()
139{
140 tmp1_id=${begin_id}
141 while [[ ${tmp1_id} -lt ${end_id} ]]
142 do
143 let tmp2_id=${tmp1_id}+1
144 if [ $type = "text" ]
145 then
146 report_name=$LOG_DIR/"awrrpt_$from}_${to}.txt"
147 else
148 report_name=$LOG_DIR/"awrrpt_${from}_${to}.html"
149 fi
150#echo $report_name
151sqlplus -s $ORACLE_USER >/dev/null<<EOF
152 set term off
153 define report_type=$type
154 define num_days=1
155 define begin_snap=${tmp1_id}
156 define end_snap=${tmp2_id}
157 define report_name=${report_name}
158 @?/rdbms/admin/awrrpt.sql
159 exit;
160EOF
161 tmp1_id=${tmp2_id}
162 done
163}
164
165#*******************************
166# main routing
167# *******************************
168define_dur
169generate_awr
170[oracle@rhel6lhr ~]$
本文选自《Oracle程序员面试笔试宝典》,作者:小麦苗
==================================================================================================================
【干货来了|小麦苗IT资料分享】
★小麦苗DB职场干货:https://mp.weixin.qq.com/s/Vm5PqNcDcITkOr9cQg6T7w
★小麦苗数据库健康检查:https://share.weiyun.com/5lb2U2M
★小麦苗微店:https://weidian.com/?userid=793741433
★各种操作系统下的数据库安装文件(Linux、Windows、AIX等):https://pan.baidu.com/s/1hqff3Evv6oj2-Tn87MpFkQ
★小麦苗分享的资料:https://share.weiyun.com/57HUxNi
★小麦苗课堂资料:https://share.weiyun.com/5fAdN5m
★小麦苗课堂试听资料:https://share.weiyun.com/5HnQEuL
★小麦苗出版的相关书籍:https://share.weiyun.com/5sQBQpY
★小麦苗博客文章:https://share.weiyun.com/5ufi4Dx
★数据库系列(Oracle、MySQL、NoSQL):https://share.weiyun.com/5n1u8gv
★公开课录像文件:https://share.weiyun.com/5yd7ukG
★其它常用软件分享:https://share.weiyun.com/53BlaHX
★其它IT资料(OS、网络、存储等):https://share.weiyun.com/5Mn6ESi
★Python资料:https://share.weiyun.com/5iuQ2Fn
★已安装配置好的虚拟机:https://share.weiyun.com/5E8pxvT
★小麦苗腾讯课堂:https://lhr.ke.qq.com/
★小麦苗博客:http://blog.itpub.net/26736162/
★OCP培训:https://mp.weixin.qq.com/s/2cymJ4xiBPtTaHu16HkiuA
★12c的OCP培训:https://mp.weixin.qq.com/s/hMLHlyjMHhLmA0xN4hLvfw
★OCM培训:https://mp.weixin.qq.com/s/7-R6Cz8RcJKduVv6YlAxJA
★高可用(RAC+DG+OGG)培训:https://mp.weixin.qq.com/s/4vf042CnOdAD8zDyjUueiw
★小麦苗课堂腾讯视频:http://v.qq.com/vplus/71f69a319a24c6808cd6e6189ae90664
==================================================================================================================
● 本文作者:小麦苗,只专注于数据库的技术,更注重技术的运用
● 作者博客地址:http://blog.itpub.net/26736162/abstract/1/
● 本系列题目来源于作者的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
● 版权所有,欢迎分享本文,转载请保留出处
● QQ:646634621 QQ群:230161599、618766405
● 微信:lhrbestxh
● 微信公众号:DB宝
● 提供Oracle OCP、OCM、高可用(rac+dg+ogg)和MySQL最实用的技能培训
● 题目解答若有不当之处,还望各位朋友批评指正,共同进步
长按下图识别二维码或微信扫描下图二维码来关注小麦苗的微信公众号:DB宝,学习最实用的数据库技术。
本文分享自微信公众号 - DB宝(lhrdba)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。