你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux/Unix shell 自动发送AWR report

[日期:2013-03-10] 来源:CSDN  作者:robinson [字体:大 中 小]

观察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@<span style="color: rgb(0, 0, 0);">12306</span>.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_${ORACLE_SID}_stauts.log
    MAIL_SUB=" $ORACLE_SID is not available on ${MACHINE} !!!"
    MAIL_BODY=" $ORACLE_SID is not available on ${MACHINE} at `date` when try to generate AWR."
    $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY
    exit 1
fi;

# ---------------------------------------------
#  Generate the awr report
# ---------------------------------------------

sqlplus -S "/ as sysdba" @${AWR_CMD}/autoawr_by_time.sql $begin_date $end_date

status=$?
if [ $status != 0 ];then
    echo " $ORACLE_SID is not available on ${MACHINE} !!!"  # >>/tmp/db_${ORACLE_SID}_stauts.log
    MAIL_SUB=" Occurred error while generate AWR for ${ORACLE_SID}  !!!"
    MAIL_BODY=" Some exceptions encountered during generate AWR report for $ORACLE_SID on `hostname`."
    $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY
    exit
fi

# ------------------------------------------------
# Send email with AWR report
# ------------------------------------------------

filename=`ls ${AWR_DIR}/${ORACLE_SID}_awrrpt_?_${begin_date}_${end_date}*`
if [ -e "${filename}" ];then
    MAIL_SUB="AWR report from ${ORACLE_SID} on `hostname`."
    MAIL_BODY="This is an AWR report from ${ORACLE_SID} on `hostname`.Time period: $begin_date,$end_date. "
    $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY -a ${filename}
    echo ${filename}
fi

# ------------------------------------------------
# Removing files older than $RETENTION parameter
# ------------------------------------------------

find ${AWR_DIR} -name "*awrrpt*" -mtime +$RETENTION -exec rm {} \;

exit

linux
相关资讯       Unix shell  AWR report 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款