概述
自动监控oracle的告警日志,使用crontab 每小时自动执行一次,每次执行的时候检查告警日志的最后100行,如发现错误的话,会自动发送错误信息和时间到指定的邮箱。
crontab:
#check alert log file
00 */1 * * * sh -x /home/oracle/sbin/ch_alert_log.sh orcl
#!/bin/bash
# -------------------------------------------------------------------------- +
# CHECK ALERT LOG FILE |
# Filename: ck_alert_log.sh |
# ./ck_alert_log.sh $ORACLE_SID |
# --------------------------------------------------------------------------+
#
# --------------------------
# Check SID
# --------------------------
if [ -z "${1}" ];then
echo "Usage: "
echo " `basename $0` ORACLE_SID"
exit 1
fi
export ORACLE_SID=$1
export MACHINE=`hostname`
export USER_MAIL="XXXXX@outlook.com"
# ----------------------------------------------
# check the database is running, if not exit
# ----------------------------------------------
db_status=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep| cut -f3 -d_`
if [ -z "$db_status" ]; then
date >/tmp/db_${ORACLE_SID}_stauts.log
echo " $ORACLE_SID is not available on ${MACHINE} !!!" >>/tmp/db_${ORACLE_SID}_stauts.log
mailx -s "$ORACLE_SID is not available" ${USER_MAIL} < /tmp/db_${ORACLE_SID}_stauts.log
exit 1
fi;
# ----------------------------------------
# Define archive directory and log file
# ----------------------------------------
ora_date=`date +%Y%m%d`
ALERT_DIR=/opt/oracle/admin/$ORACLE_SID/bdump
ALERT_LOG=${ALERT_DIR}/alert_${ORACLE_SID}.log
ARCH_DIR=${ALERT_DIR}/${ora_date}
NEW_ALERT_LOG=/tmp/${ora_date}_alert_${ORACLE_SID}.log
TEMP_ALERT_LOG=${ALERT_LOG}.temp
if [ ! -d "${ARCH_DIR}" ] ; then
mkdir $ARCH_DIR
fi
# -------------------------------------
# Check alert log file and send email
# -------------------------------------
tail -n 100 ${ALERT_LOG} | grep -B 1 'ORA-' > ${NEW_ALERT_LOG}
if [ -s "${NEW_ALERT_LOG}" ];then
mailx -s "${ora_date} Found errors in ${ORACLE_SID} on ${MACHINE}" ${USER_MAIL} < ${NEW_ALERT_LOG}
else
echo "${ora_date} no error in ${ORACLE_SID} on ${MACHINE}"
fi
# --------------------------------
# delete NEW_ALERT_LOG every day
# --------------------------------
yesterday=`date -d "1 day ago" +%Y%m%d`
if [ -e /tmp/${yesterday}_alert_${ORACLE_SID}.log ];then
rm /tmp/${yesterday}_alert_${ORACLE_SID}.log
else
echo "file is not exists"
fi
# --------------------------------
# Backup current alert log file
# --------------------------------
#if [ -e ${ARCH_DIR}/alert_${ORACLE_SID}.log ];then
# echo "ALERT_LOG is exists"
#else
# mkdir ${ALERT_DIR}/${ora_date}
# cp ${ALERT_LOG} ${ARCH_DIR}
#fi
exit
crontab:
#check alert log file
00 */1 * * * sh -x /home/oracle/sbin/ch_alert_log.sh orcl
#!/bin/bash
# -------------------------------------------------------------------------- +
# CHECK ALERT LOG FILE |
# Filename: ck_alert_log.sh |
# ./ck_alert_log.sh $ORACLE_SID |
# --------------------------------------------------------------------------+
#
# --------------------------
# Check SID
# --------------------------
if [ -z "${1}" ];then
echo "Usage: "
echo " `basename $0` ORACLE_SID"
exit 1
fi
export ORACLE_SID=$1
export MACHINE=`hostname`
export USER_MAIL="XXXXX@outlook.com"
# ----------------------------------------------
# check the database is running, if not exit
# ----------------------------------------------
db_status=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep| cut -f3 -d_`
if [ -z "$db_status" ]; then
date >/tmp/db_${ORACLE_SID}_stauts.log
echo " $ORACLE_SID is not available on ${MACHINE} !!!" >>/tmp/db_${ORACLE_SID}_stauts.log
mailx -s "$ORACLE_SID is not available" ${USER_MAIL} < /tmp/db_${ORACLE_SID}_stauts.log
exit 1
fi;
# ----------------------------------------
# Define archive directory and log file
# ----------------------------------------
ora_date=`date +%Y%m%d`
ALERT_DIR=/opt/oracle/admin/$ORACLE_SID/bdump
ALERT_LOG=${ALERT_DIR}/alert_${ORACLE_SID}.log
ARCH_DIR=${ALERT_DIR}/${ora_date}
NEW_ALERT_LOG=/tmp/${ora_date}_alert_${ORACLE_SID}.log
TEMP_ALERT_LOG=${ALERT_LOG}.temp
if [ ! -d "${ARCH_DIR}" ] ; then
mkdir $ARCH_DIR
fi
# -------------------------------------
# Check alert log file and send email
# -------------------------------------
tail -n 100 ${ALERT_LOG} | grep -B 1 'ORA-' > ${NEW_ALERT_LOG}
if [ -s "${NEW_ALERT_LOG}" ];then
mailx -s "${ora_date} Found errors in ${ORACLE_SID} on ${MACHINE}" ${USER_MAIL} < ${NEW_ALERT_LOG}
else
echo "${ora_date} no error in ${ORACLE_SID} on ${MACHINE}"
fi
# --------------------------------
# delete NEW_ALERT_LOG every day
# --------------------------------
yesterday=`date -d "1 day ago" +%Y%m%d`
if [ -e /tmp/${yesterday}_alert_${ORACLE_SID}.log ];then
rm /tmp/${yesterday}_alert_${ORACLE_SID}.log
else
echo "file is not exists"
fi
# --------------------------------
# Backup current alert log file
# --------------------------------
#if [ -e ${ARCH_DIR}/alert_${ORACLE_SID}.log ];then
# echo "ALERT_LOG is exists"
#else
# mkdir ${ALERT_DIR}/${ora_date}
# cp ${ALERT_LOG} ${ARCH_DIR}
#fi
exit
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28881244/viewspace-1394099/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28881244/viewspace-1394099/
最后
以上就是粗犷美女为你收集整理的通过shell监控告警日志的全部内容,希望文章能够帮你解决通过shell监控告警日志所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复