【DB筆試面試821】在Oracle中,如何定時生成AWR報告?

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

         題目         部分

【DB筆試面試821】在Oracle中,如何定時生成AWR報告?html


     




         答案部分          


有的系統須要定時生成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程序員面試筆試寶典》,做者:小麥苗
sql


watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

==================================================================================================================【乾貨來了|小麥苗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
==================================================================================================================
 

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

 本文做者:小麥苗,只專一於數據庫的技術,更注重技術的運用數據庫

● 做者博客地址:http://blog.itpub.net/26736162/abstract/1/bash

 本系列題目來源於做者的學習筆記,部分整理自網絡,如有侵權或不當之處還請諒解微信

 版權全部,歡迎分享本文,轉載請保留出處網絡

 QQ:646634621  QQ羣:23016159九、618766405oracle

 微信:lhrbestxhapp

 微信公衆號:DB寶

 提供Oracle OCP、OCM、高可用(rac+dg+ogg)和MySQL最實用的技能培訓

● 題目解答如有不當之處,還望各位朋友批評指正,共同進步

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

長按下圖識別二維碼或微信掃描下圖二維碼來關注小麥苗的微信公衆號:DB寶,學習最實用的數據庫技術。

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=


本文分享自微信公衆號 - DB寶(lhrdba)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索