yanchuen 发表于 2018-8-27 08:39:47

SHELL脚本监控oracle alert日志

#!/bin/bash  
scriptHome=`dirname $0`
  
scriptName=`basename $0`
  
logDir=$scriptHome/logs
  
#logfile=$logDir/$scriptName_$(date "+%Y%m%d").log
  
logfile=$logDir/ora-error.log
  
runfile=$logDir/run.log
  
alertLogFile=/u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log
  
ip=`ifconfig | sed '2q' | awk -F '[ :]+' '/inet/{print $4}'`
  
oraErrMsg=ORA-
  
mailTo=737735250@qq.com
  
if [ ! -d $logDir ]; then
  
   mkdir -p $logDir
  
fi
  
if [ ! -f $alertLogFile ]; then
  
   echo "Error: $alertLogFile no such file or directory."
  
   exit 1
  
fi
  
touch $runfile
  
startLine=$(cat $runfile)
  
endLine=$(cat $alertLogFile | wc -l)
  
if [ "x$startLine" == "x" ]; then
  
startLine=1
  
else
  
startLine=$(expr $startLine + 1)
  
fi
  
echo "=== 正在获取警告日志信息..."
  
if [ $endLine -le $startLine ]; then
  
#echo "未扫描到错误信息."
  
echo ""
  
exit 0
  
fi
  
echo "===$(date "+%Y-%m-%d %H:%M:%S") 扫描行${startLine},${endLine}..." >>$logfile
  
content=`cat $alertLogFile | sed -n "${startLine},${endLine}p"`
  
oraError=`echo "$content" | grep "$oraErrMsg" | tee -a $logfile`
  
if [ "x$oraError" != "x" ]; then
  
echo "$oraError" | mutt -s "$(date "+%Y-%m-%d %H:%M:%S") $ip 警告日志告警" -b $mailTo
  
fi
  
echo $endLine > $runfile


页: [1]
查看完整版本: SHELL脚本监控oracle alert日志