欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

Linux/Unix shell 自动发送AWR report

程序员文章站 2024-02-14 21:23:40
...

观察Oracle数据库性能,Oracle自带的awr 功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告

观察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 : |
# --------------------------------------------------------------------------+
#
# --------------------------
# 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 /nologconnect / as sysdba;
@${AWR_CMD}/autoawr.sql;
exit;
EOF