SHELL版的自动批量生成AWR报告的脚本

用法:

bash ./dba_oracle_awr.sh -s ORCL -f 20110312070000 -t 20110312090000 -p TEXT -h /usr/local/oracle/10.2.0-64

 

脚本:

#!/usr/bin/bash
# ********************************
# * dba_oracle_awr.sh
# ********************************
# Usage: dba_oracle_awr.sh -s [instance_name]
#          -f [from time]
#          -t [to time]
#          -p [report type, html or text]
#          -h [oracle home]
#          -n [tns admin]
#
#         time format: 'yyyymmddhh24miss'.
#         E.g 20110304170000 means 05:00:00pm, Mar 04, 2011
#
#

# **********************
# get parameters
# **********************
  while getopts ":s:f:t:p:h:n" opt
  do
    case $opt in
    s) instance=$OPTARG
       ;;
    f) from=$OPTARG
       ;;
    t) to=$OPTARG
       ;;
    p) type=$OPTARG
       type=$(echo $type|tr "[:upper:]" "[:lower:]")
       ;;
    h) oracle_home=$OPTARG
       ;;
    n) tns_admin=$OPTARG
       ;;
    '?') echo "$0: invalid option -$OPTARG">&2
       exit 1
       ;;
    esac
done

if [ "$instance" = "" ]
then
  echo "instance name(-s) needed"
  echo "program exiting..."
  exit 1
fi

if [ "$from" = "" ]
then
  echo "from time (-f} needed"
  echo "program exiting..."
  exit 1
fi

if [ "$to" = "" ]
then
  echo "to time (-t) needed"
  echo "program exiting..."
  exit 1
fi

if [ "$oracle_home" ="" ]
then
  echo "oracle home (-h) needed"
  echo "program exiting..."
  exit 1
fi
sqlplus="${oracle_home}/bin/sqlplus"
echo $sqlplus

if [ "$type" = "" ]
then
  type="html"
fi

# ********************
# trim function
# ********************
function trim()
{
  local result
  result=`echo $1|sed 's/^ *//g' | sed 's/ *$//g'`
  echo $result
}

# *******************************
# read interchange ID & passwd
# *******************************
read_act()
{
echo "interchange ID: "
read user
echo "password: "
stty -echo
read pswd
stty echo
}

# *******************************
# get begin and end snapshot ID
# *******************************
define_dur()
{
begin_id=`$sqlplus -s /nolog<   conn $user/$pswd@$instance
  set pages 0
  set head off
  set feed off
  select max(SNAP_ID) from DBA_HIST_SNAPSHOT where
    BEGIN_INTERVAL_TIME<=to_date($from,'yyyymmddhh24miss');
EOF`

ret_code=$?
if [ "$ret_code" != "0" ]
then
  echo "sqlplus failed with code $ret_code"
  echo "program exiting..."
  exit 10
fi

end_id=`$sqlplus -s /nolog<   conn $user/$pswd@$instance
  set pages 0
  set head off
  set feed off
  select min(SNAP_ID) from DBA_HIST_SNAPSHOT where
    END_INTERVAL_TIME>=to_date($to,'yyyymmddhh24miss');
  spool off
EOF`

ret_code=$?
if [ "$ret_code" != "0" ]
then
  echo "sqlplus failed with code $ret_code"
  echo "program exiting..."
  exit 10
fi

begin_id=$(trim ${begin_id})
end_id=$(trim ${end_id})
# echo "begin_id: $begin_id  end_id: $end_id"
}

# *******************************
# generate AWR report
# *******************************
generate_awr()
{
  awrsql="${oracle_home}/rdbms/admin/awrrpt.sql"
  if [ ! -e $awrsql ]
  then
    echo "awrrpt.sql does not exist, exiting..."
    exit 20
  fi

  tmp1_id=${begin_id}
  #echo "begin_id is: $begin_id"
  #echo "tmp1_id is: $tmp1_id"
  while [ ${tmp1_id} -lt ${end_id} ]
  do
    let tmp2_id=${tmp1_id}+1
    if [ $type = "text" ]
    then
      report_name="awrrpt_${instance}_${tmp1_id}_${tmp2_id}.txt"
    else
      report_name="awrrpt_${instance}_${tmp1_id}_${tmp2_id}.html"
    fi
    #echo $report_name

$sqlplus -s $user/$pswd@$instance>/dev/null<
      set term off
      define report_type=$type
      define num_days=1
      define begin_snap=${tmp1_id}
      define end_snap=${tmp2_id}
      define report_name=${report_name}
      @${oracle_home}/rdbms/admin/awrrpt.sql
      exit;
EOF

    tmp1_id=${tmp2_id}
  done
}

# *******************************
# main routing
# *******************************
read_act
define_dur
generate_awr


手工生成AWR报告方法记录

AWRAutomatic Workload Repository)报告是我们进行日常数据库性能评定、问题SQL发现的重要手段。熟练掌握AWR报告,是做好开发、运维DBA工作的重要基本功。

 

 

AWR报告的原理是基于Oracle数据库的定时镜像功能。默认情况下,Oracle数据库后台进程会以一定间隔(一小时)收集系统当前状态镜像,并且保存在数据库中。生成AWR报告时,只需要指定进行分析的时间段(开始镜像编号和结束镜像编号),就可以生成该时间段的性能分析情况。AWR镜像保存在数据库中的时间为一个月左右。

 

 

目前Oracle10g之后,AWR报告取代了原先的Statspack报告成为一个主流性能分析报告。通常可以从OEMOracle Enterprise Manager Console)平台上生成查看AWR报告。在OEM中,使用图形化方法更加容易。本篇中介绍使用手工脚本方式生成AWR的方法,脱离OEM的限制。

 

 

1、 运行脚本

 

首先,准备一个目录作为AWR生成报告的路径。

 

 

[oracle@bspdev /]$ ls -l | grep test

drwxr-xr-x.  2 oracle oinstall 4096 Jun 21 13:01 test

 

[oracle@bspdev /]$ cd test

 

 

启动sqlplus开发工具,调用生成脚本。程序脚本一般保存在$ORACLE_HOME下的rdbms/admin中,名称为awrrpt.sql

 

 

[oracle@bspdev test]$ sqlplus /nolog

 

SQL*Plus: Release11.2.0.1.0 Production on Tue Jun 21 13:04:44 2011

 

Copyright (c) 1982, 2009, Oracle. All rights reserved.

 

SQL> conn / as sysdba

Connected.

 

--调用脚本,生成文件

SQL> @?/rdbms/admin/awrrpt.sql

 

 

之后进入报告参数输入模块。

 

2、输入报告参数

 

之后,要持续输入一系列的报告参数。

 

ü       输入生成报告类型,目前AWR提供txthtml两种格式。需要确认生成格式,默认是html格式。

 

 

Current Instance

~~~~~~~~~~~~~~~~

 

  DB Id   DB Name     Inst Num Instance

----------- ------------ -------- ------------

 4143510747 ORA11G             1 ora11g

 

 

Specify the Report Type

~~~~~~~~~~~~~~~~~~~~~~~

Would you like an HTML report, or a plain text report?

Enter 'html' for an HTML report, or 'text' for plain text

Defaults to 'html'

 

 

ü       报告涉及天数范围

 

启动报告后,会显示生成实例的名称等基本信息。

 

默认情况下,AWR会将镜像信息保留一个月。手工生成的时候,需要确认生成AWR报告的时间范围。一般情况下,特别是生产环境下,我们通常设置1-7天也就够用了。

 

 

Instances in this Workload Repository schema

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

  DB Id    Inst Num DB Name     Instance    Host

------------ -------- ------------ ------------ ------------

* 4143510747       1 ORA11G      ora11g      bspdev.local

                                               domain

 

Using 4143510747 for database Id

Using         1 for instance number

 

 

Specify the number of days of snapshots to choose from

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Entering the number of days (n) will result in the most recent

(n) days of snapshots being listed. Pressing without

specifying a number lists all completed snapshots.

 

Enter value for num_days:3 

 

 

ü       输入开始和结束的snapshot编号

 

输入天数信息后,AWR生成代码会将天数范围内的snapshot镜像点列出,供输入选择。

 

 

Listing the last 3 days of Completed Snapshots

 

                                                       Snap

Instance    DB Name       Snap Id   Snap Started   Level

------------ ------------ --------- ------------------ -----

ora11g      ORA11G           1789 20 Jun 2011 13:01     1

                              1790 20 Jun 2011 14:00     1

                              1791 20 Jun 2011 15:00     1

                              1792 20 Jun 2011 16:00     1

                              (篇幅原因,有省略……

                              1811 21 Jun 2011 11:00     1

                              1812 21 Jun 2011 12:00     1

                              1813 21 Jun 2011 13:00     1

 

 

 

Specify the Begin and End Snapshot Ids

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

之后,我们需要根据列出的时间范围,输入开始和结束的snap编号。

 

 

Specify the Begin and End Snapshot Ids

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter value for begin_snap:1796

Begin Snapshot Id specified: 1796

 

Enter value for end_snap:1813 

 

 

ü       确定报告名称

 

最后就是确定生成报告的名称。一般采用默认的名称就可以了。

 

 

Specify the Report Name

~~~~~~~~~~~~~~~~~~~~~~~

The default report file name is awrrpt_1_1796_1813.html. To use this name,

press to continue, otherwise enter an alternative.

 

Enter value for report_name:

 

 

之后输出内容很多,此处不加以累述。最后提示报告生成成功。

 

 

Report written to awrrpt_1_1796_1813.html

 

 

于是,指定目录上可以看到相应的报告文件。

 

 

[oracle@bspdev test]$ ls -l

total 508

-rw-r--r--. 1 oracle oinstall 515262 Jun 21 13:10 awrrpt_1_1796_1813.html

 

 

 

3、说明两个问题

 

首先,此处生成的html格式的报表。如果要求生成txt格式,就在生成过程中选择text格式报表。

 

 

Specify the Report Type

~~~~~~~~~~~~~~~~~~~~~~~

Would you like an HTML report, or a plain text report?

Enter 'html' for an HTML report, or 'text' for plain text

Defaults to 'html'

Enter value for report_type: text

 

Type Specified: text

 

End of Report

Report written to awrrpt_1_1789_1800.txt

 

[oracle@bspdev test]$ ls -l

total 692

-rw-r--r--. 1 oracle oinstall 180601 Jun 21 13:27 awrrpt_1_1789_1800.txt

-rw-r--r--. 1 oracle oinstall 515262 Jun 21 13:10 awrrpt_1_1796_1813.html

 

 

 

第二个就是调用脚本的方式问题。调用时使用的sqlplus客户端可以在Oracle服务器本机上(远程登录),也可以在客户端机器本机上。笔者建议是在客户端本机上进行生成,这样可以避免报告文件来回拷贝的工作。但是最好要保证客户端版本与服务器版本相匹配。

 

 

4、结论

 

手工生成AWR报告,可以避免受到OEM的限制约束,而且灵活度高。本篇记录,权当备忘。



请使用浏览器的分享功能分享到微信等