日期:2014-05-16  浏览次数:20647 次

Linux/Unix shell 自动发送AWR report(二)

       观察Oracle数据库性能,Oracle自带的awr 功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告。不过awrrpt.sql脚本执行时需要我们提供一些交互信息,因此可以将其整合到shell脚本中来实现自动产生指定时段的awr报告并发送给相关人员。本文对Linux/Unix shell 自动发送AWR report的功能进行了完善和补充。   

 

1、shell脚本

robin@SZDB:~/dba_scripts/custom/awr> more autoawr_by_time.sh
#!/bin/bash
# --------------------------------------+
#                 Generate AWR report and send mail automatically           |
#   Filename: autoawr_by_time.sh                                            |
#   Desc:                                                                   |
#       The script use to generate awr report by time period.               |
#       Three parameter for it.                                             |
#           para1: <ORACLE_SID>   mandatory parameter                       |
#           para2: [begin time]   optional parameter                        |  
#           para3: [end time  ]   optional parameter                        |
#       Deploy it by crontab as requirement                                 |  
#   Usage:                                                                  |
#       ./autoawr_by_time.sh <instance_name> [begin time] [end time]        |  
#   Example:                                                                |
#       ./autoawr_by_time.sh TESTDB                                         |   
#            --default,time period is from last midnight to today midnight  |
#       ./autoawr_by_time.sh TESTDB 2013031009                              |
#            --time period is from 2013031009 to now                        |
#       ./autoawr_by_time.sh TESTDB 2013031009 2013031012                   |
#            --time period by speicifed                                     | 
#   Author : Robinson                                                       | 
#   Blog   : http://blog.csdn.net/robinson_0612                             |
# --------------------------------------+
#
# -------------------------------
#  Set environment here 
# ------------------------------

if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

# ------------------------
#  Check the parameter, if no specify,then use default value
# ------------------------

if [ -z "${1}" ] ;then
    echo "Usage: "
    echo "      `basename $0` <ORACLE_SID> [begin_date] [end_date]"
fi

if [ -z "${3}" ] && [ -z "${2}" ];then
    begin_date=`date -d yesterday +%Y%m%d`'00'
    end_date=`date +%Y%m%d`'00'
elif [ -z "${3}" ]; then
    begin_date=${2}
    end_date=`date +%Y%m%d%H`
else
    begin_date=${2}
    end_date=${3}
fi

ORACLE_SID=${1}
export ORACLE_SID begin_date end_date 
export MACHINE=`hostname`
export MAIL_DIR=/users/robin/dba_scripts/sendEmail-v1.56
export MAIL_LIST='Robinson.chen@12306.com'
export AWR_CMD=/users/robin/dba_scripts/custom/awr
export AWR_DIR=/users/robin/dba_scripts/custom/awr/report/${ORACLE_SID}
export MAIL_FM='oracle@szdb.com'
RETENTION=31

echo $ORACLE_SID 
echo $begin_date
echo $end_date
# --------------------------------
#  Check the directory for store awr report,if not exist, create it
# --------------------------------

if [ ! -d "${AWR_DIR}" ]; then
    mkdir -p ${AWR_DIR}
fi

# ----------------------------------------------
# check if the database is running, if not exit
# ----------------------------------------------

db_stat=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep| cut -f3 -d_`
if [ -z "$db_stat" ]; then
    #date >/tmp/db_${ORACLE_SID}_stauts.log
    echo " $ORACLE_SID is not available on ${MACHINE} !!!"   # >>/tmp/db