观察Oracle数据库性能,Oracle自带的awr 功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告。不过awrrpt.sql脚本执行时需要我们提供一些交互信息,因此可以将其整合到shell脚本中来实现自动产生指定时段的awr报告并发送给相关人员。本文即是描述linux shell脚本来实现此功能。
1、shell脚本
robin@SZDB:~/dba_scripts/custom/awr> more autoawr.sh
#!/bin/bash
# --------------------------------------------------------------------------+
# CHECK ALERT LOG FILE |
# Filename: autoawr.sh |
# Desc: |
# The script use to generate AWR report and send mail automatic. |
# The sql script autoawr.sql call by this shell script. |
# Default, the whole day AWR report will be gathered. |
# Deploy it to crontab at 23:30 |
# If you want to change the snap interval,please change autoawr.sql |
# and crontab configuration |
# Usage: |
# ./autoawr.sh $ORACLE_SID |
# |
# Author : Robinson |
# Blog : http://blog.csdn.net/robinson-0612 |
# --------------------------------------------------------------------------+
#
# --------------------------
# Check SID
# --------------------------
if [ -z "${1}" ];then
echo "Usage: "
echo " `basename $0` ORACLE_SID"
exit 1
fi
# -------------------------------
# Set environment here
# ------------------------------
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
export ORACLE_SID=$1
export MACHINE=`hostname`
export MAIL_DIR=/users/robin/dba_scripts/sendEmail-v1.56
export MAIL_LIST='Robinson.cheng@12306.com'
export AWR_CMD=/users/robin/dba_scripts/custom/awr
export AWR_DIR=/users/robin/dba_scripts/custom/awr/report
export MAIL_FM='oracle@szdb.com'
RETENTION=31
# ----------------------------------------------
# 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 awr report
# ----------------------------------------------
$ORACLE_HOME/bin/sqlplus /nolog/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
# ------------------------------------------------
dt=`date -d yesterday +%Y%m%d`
filename=`ls ${AWR_DIR}/${ORACLE_SID}_awrrpt_?_${dt}*`
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`."
$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