Oracle Check Alert Log (shell script)

記錄 script 內容,可依自行需求修改

#!/usr/bin/ksh
#==========================================================================
#       USAGE : ora_check.sh
# DESCRIPTION : Check oracle alert log and send mail as needed per 5 minute
#      AUTHOR : 
#     CREATED : 
#    REVISION : 1
#=========================================================================
. ~/.bash_profile

# set scripts ENV
setEnv(){
  hostname=${ORACLE_HOSTNAME}
  alertDir=/u01/app/oracle/diag/rdbms/${ORACLE_UNQNAME}/${ORACLE_SID}/trace
  alertLog=$(ls -l ${alertDir}/"alert_"${ORACLE_SID}".log"|awk '{print $NF}')
  time="`date +%Y`/`date +%m`/`date +%d` `date +%H`:`date +%M`:`date +%S`"
  script_home=/home/oracle/script
}

sendmail(){
  maillist=`cat /home/oracle/script/mail.list | egrep ''${1}'' | awk '{print $2}'`
  sender=`hostname`
  subject="[ErrorLog] ${hostname} Alert - $time "
  cat ${2} | mail -v -r ${sender} -s "${subject}" ${maillist}
}

main(){
  setEnv
  cd ${script_home}
  alertline_cur=`wc -l ${alertLog} | awk '{print $1}'`
  alertline_pre=`cat checkalertline.last`
  if [[ -z ${alertline_pre} ]]; then
    alertline_pre=${alertline_cur}
    captureline=-${alertline_cur}
  else
    if [ ${alertline_cur} -gt ${alertline_pre} ]; then
      captureline=$((${alertline_pre}-${alertline_cur}))
    elif [ ${alertline_cur} -lt ${alertline_pre} ]; then
      captureline=-${alertline_cur}
    else # -eq
      captureline=0
    fi
  fi

  echo ${alertline_cur} > checkalertline.last

  cat /dev/null > ora_err_${ORACLE_SID}.tail
  # do tail $captureline
  if [ ${captureline} -ne 0 ]; then
    tail ${captureline} ${alertLog}|egrep "ORA-|Shutting down instance|Starting ORACLE instance"|egrep -v "ORA-235"|egrep -v "ORA-00235"|egrep -v "ORA-3136"|egrep -v "ORA-16401" > ora_err_${ORACLE_SID}.tail
  fi

  if [ -s ora_err_${ORACLE_SID}.tail ];then
    sendmail ora_err_${ORACLE_SID}.tail
  fi
}

main
$ crontab -e
*/5 * * * * /home/oracle/script/ora_check.sh > /dev/null 2>&1

已轉向 blogger 記錄

https://slowlife-notes.blogspot.com