通过shell监控告警日志

自动监控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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28881244/viewspace-1394099/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28881244/viewspace-1394099/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值