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<
@${AWR_CMD}/autoawr.sql;
exit;
EOF
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
# ------------------------------------------------
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}
f