Linux/Unix shell 监控Oracle告警日志(monitor alter log file)(一)

2014-11-24 18:13:33 · 作者: · 浏览: 0

Linux Shell的相关参考:


1、监控Oracle告警日志脚本


robin@SZDB:~/dba_scripts/custom/bin> more ck_alert.sh
#!/bin/bash
# --------------------------------------------------------------------------+
# CHECK ALERT LOG FILE |
# Filename: ck_alert.sh |
# Desc: |
# The script use to check alert log file. |
# Once any error was caught, a mail alert will be sent. |
# Deploy it by crontab. e.g. per 15 min |
# Usage: |
# ./ck_alert.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.chen@12306.com'
export MAIL_FM='oracle@szdb.com'


# ----------------------------------------------
# check 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_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -o message-file=/tmp/db_${ORACLE_SID}_stauts.log
exit 1
fi;


# --------------------------------------
# Get the location of alert log file
# --------------------------------------


sqlplus '/ as sysdba' << EOF > /tmp/${ORACLE_SID}_monitor_temp.txt
column xxxx format a10
column value format a80
set lines 132
SELECT 'xxxx' ,value FROM v\$parameter WHERE name = 'background_dump_dest'
/
exit
EOF


cat /tmp/${ORACLE_SID}_monitor_temp.txt | awk '$1 ~ /xxxx/ {print $2}' > /tmp/${ORACLE_SID}_monitor_location.txt
read ALERT_DIR < /tmp/${ORACLE_SID}_monitor_location.txt
rm /tmp/${ORACLE_SID}_monitor_temp.txt 2>/dev/null


# ----------------------------------------
# Define archive directory and log file
# ----------------------------------------


DT=`date +%Y%m%d`
DT_DIR=`date +%Y%m`
ARCH_DIR=${ALERT_DIR}/${DT_DIR}


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


ORIG_ALERT_LOG=${ALERT_DIR}/alert_${ORACLE_SID}.log
NEW_ALERT_LOG=${ARCH_DIR}/alert_${ORACLE_SID}.log.${DT}
TEMP_ALERT_LOG=${ORIG_ALERT_LOG}.temp
AWK_DIR=/users/robin/dba_scripts/custom/bin


# -------------------------------------
# Check alert log file and send email
# -------------------------------------
cat ${ORIG_ALERT_LOG} | awk -f $AWK_DIR/check_alert.awk > /tmp/${ORACLE_SID}_check_monitor_log.log
if [ -s "/tmp/${ORACLE_SID}_check_monitor_log.log" ];
then
echo "Found errors in sid ${ORACLE_SID}, mailed errors"
echo -e "The following errors were found in the alert log for ${ORACLE_SID} \n" > /tmp/${ORACLE_SID}_check_monitor_log.mail
echo -e "Alert log was copied into ${NEW_ALERT_LOG} \n">> /tmp/${ORACLE_SI