归档—监控ORACLE数据库告警日志
Posted 张英爱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了归档—监控ORACLE数据库告警日志相关的知识,希望对你有一定的参考价值。
ORACLE的告警日志里面包含许多有用的信息,尤其是一些ORACLE的ORA错误信息,所以有必要及时归档、监控数据库告警日志的ORA错误,及时提醒数据库管理员DBA处理这些错误信息,那么我们首先来看看告警日志的内容片断:
归档告警日志文件
告警日志文件如果不加管理的话,那么文件会持续增长,有时候文件会变得非常大,不利于读写。一般建议将告警日志按天归档,归档文件保留三个月(视情况而定),下面来看看将告警日志文件归档的两个Shell脚本:
- #*************************************************************************
- # FileName :alert_log_archive.sh
- #*************************************************************************
- # Author :Kerry
- # CreateDate :2013-07-02
- # blogs :www.cnblogs.com/kerrycode
- # Description :this script is made the alert log archived every day
- #*************************************************************************
- #! /bin/bash
- date=`date +%Y%m%d`
- alert_log_path="$ORACLE_BASE/admin/$ORACLE_SID/bdump"
- alert_log_file="alert_$ORACLE_SID.log"
- alert_arc_file="alert_$ORACLE_SID.log""."${date}
- cd ${alert_log_path};
- if [ ! -e "${alert_log_file}" ]; then
- echo "the alert log didn\'t exits, please check file path is correct!";
- exit;
- fi
- if [ -e ${alert_arc_file} ];then
- echo "the alert log file have been archived!"
- else
- cat ${alert_log_file} >> ${alert_arc_file}
- cat /dev/null > ${alert_log_file}
- fi
其实脚本1和脚本差别不大,仅仅是mv与cat >>的区别
- #*************************************************************************
- # FileName :alert_log_archive.sh
- #*************************************************************************
- # Author :Kerry
- # CreateDate :2013-07-02
- # blogs :www.cnblogs.com/kerrycode
- # Description :this script is made the alert log archived every day
- #*************************************************************************
- #! /bin/bash
- date=`date +%Y%m%d`
- alert_log_path="$ORACLE_BASE/admin/$ORACLE_SID/bdump"
- alert_log_file="alert_$ORACLE_SID.log"
- alert_arc_file="alert_$ORACLE_SID.log""."${date}
- cd ${alert_log_path};
- if [ ! -e "${alert_log_file}" ]; then
- echo "the alert log didn\'t exits, please check file path is correct!";
- exit;
- fi
- if [ -e ${alert_arc_file} ];then
- echo "the alert log file have been archived!"
- else
- mv ${alert_log_file} ${alert_arc_file}
- cat /dev/null > ${alert_log_file}
- fi
然后在crontab定时任务里面加上下面语句,每天23点59对告警日志进行归档。
[oracle@DB-Server scripts]$ crontab -l
# the alert log archived every day Add by kerry 2013-07-02
59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>$1
细心的朋友可能已经发现上面的脚本、配置错误了,我在部署测试的过程中,是指定二十分钟执行一次,但是等了四十分钟,发现定时任务一次都没有执行,手工执行上面脚本是完全没有问题的,最后仔细的检查一遍,居然发现悲剧的发现时自己一时粗心将&符号写成了$,真是很二的一个错误
59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>$1
59 23 * * * /home/oracle/scripts/alert_log_archive.sh >/dev/null 2>&1
接下来测试发现脚本执行有问题,在crontab 里执行该shell脚本时,获取不到ORACLE的环境变量,这是因为crontab环境变量问题,Crontab的环境默认情况下并不包含系统中当前用户的环境。所以,你需要在shell脚本中添加必要的环境变量的设置,修改的脚本如下:
- #*************************************************************************
- # FileName :alert_log_archive.sh
- #*************************************************************************
- # Author :Kerry
- # CreateDate :2013-07-02
- # blogs :www.cnblogs.com/kerrycode
- # Description :this script is made the alert log archived every day
- #*************************************************************************
- #! /bin/bash
- # these solved the oracle variable problem.
- export ORACLE_SID=gps
- export ORACLE_BASE=/u01/app/oracle
- date=`date +%Y%m%d`
- alert_log_path="$ORACLE_BASE/admin/$ORACLE_SID/bdump"
- alert_log_file="alert_$ORACLE_SID.log"
- alert_arc_file="alert_$ORACLE_SID.log""."${date}
- cd ${alert_log_path};
- if [ ! -e "${alert_log_file}" ]; then
- echo "the alert log didn\'t exits, please check file path is correct!";
- exit;
- fi
- if [ -e ${alert_arc_file} ];then
- echo "the alert log file have been archived!"
- else
- cat ${alert_log_file} >> ${alert_arc_file}
- cat /dev/null > ${alert_log_file}
- fi
- #*************************************************************************
- # FileName :alert_log_archive.sh
- #*************************************************************************
- # Author :Kerry
- # CreateDate :2013-07-0
- # blogs :www.cnblogs.com/kerrycode
- # Description :this script is made the alert log archived every day
- #*************************************************************************
- #! /bin/bash
- # these solved the oracle variable problem.
- export ORACLE_SID=gps
- export ORACLE_BASE=/u01/app/oracle
- date=`date +%Y%m%d`
- alert_log_path="$ORACLE_BASE/admin/$ORACLE_SID/bdump"
- alert_log_file="alert_$ORACLE_SID.log"
- alert_arc_file="alert_$ORACLE_SID.log""."${date}
- cd ${alert_log_path};
- if [ ! -e "${alert_log_file}" ]; then
- echo "the alert log didn\'t exits, please check file path is correct!";
- exit;
- fi
- if [ -e ${alert_arc_file} ];then
- echo "the alert log file have been archived!"
- else
- mv ${alert_log_file} ${alert_arc_file}
- cat /dev/null > ${alert_log_file}
- fi
监控告警日志文件
接下来看看如何监控告警日志文件的ORA错误,这里是采用Perl结合Shell的方式,因为Shell获取错误的时间、行数等不如Perl操作字符串方便。
- #**********************************************************************************
- # FileName :monitoring_alert_log.pl
- #**********************************************************************************
- # Author :Kerry
- # CreateDate :2013-07-01
- # blogs :www.cnblogs.com/kerrycode
- # Description :check the alert log and find out the ora error
- #**********************************************************************************
- # Modified Date Modified User Version Modified Reason
- # 2013-07-02 Kerry V01.0.1 add comment for this script
- #***********************************************************************************
- #! /usr/bin/perl
- use strict;
- my($argv) = @ARGV;
- if ( @ARGV != 1)
- {
- print \'
- Parameter error: you must assined the alert log file as a input parameter or the number of prarameter is not right.
- \';
- exit
- }
- if( ! -e $argv )
- {
- print \'
- Usage: monitoring_alert_log.pl
- $ cat alert_[sid].log | monitoring_alert_log.pl
- $ tail -f alert_[sid].log | monitoring_alert_log.pl
- $ monitoring_alert_log.pl alert_[sid].log
- \';
- exit;
- }
- my $err_regex = \'^(\\w+ \\w+ \\d{2} \\d{2}:\\d{2}:\\d{2} \\d{4})|(ORA-\\d+:.+)$\';
- my $date = "";
- my $line_counter = 0;
- while ( <> )
- {
- $line_counter++;
- if( m/$err_regex/oi )
- {
- if ($1)
- {
- $date = $1;
- next;
- }
- print "$line_counter | $date | $2 \\n" if ($2);
- }
- }
- #**********************************************************************************
- # FileName : monitoring_alert_log.sh
- #**********************************************************************************
- # Author : Kerry
- # CreateDate : 2013-07-01
- # blogs : www.cnblogs.com/kerrycode
- # Description: check the alert log and find out the ora error
- #**********************************************************************************
- # Modified Date Modified User Version Modified Reason
- # 2013-07-02 Kerry V01.0.1 add comment and modified script
- #***********************************************************************************
- #!/bin/bash
- # these solved the oracle variable problem.
- export ORACLE_SID=gsp
- export ORACLE_BASE=/u01/app/oracle
- logfile="/home/oracle/scripts/alter_err_log.txt"
- pl_monitoring_alert="/home/oracle/scripts/monitoring_alert_log.pl"
- pl_sendmail="/home/oracle/scripts/sendmail.pl"
- alert_logfile="$ORACLE_BASE/admin/$ORACLE_SID/bdump/alert_$ORACLE_SID.log"
- #delete the old alter error log file
- rm -f${logfile}
- rm -f${pl_sendmail}
- #run the perl and check if exists the ora error
- perl ${pl_monitoring_alert} ${alert_logfile}> ${logfile}
- #if have no error in alert log then exit the program
- if [[ -e "${logfile}" && ! -s "${logfile}" ]]; then
- exit;
- fi
- date_today=`date +%Y_%m_%d`
- subject="Monitoring the Oracle Alert logs and find ora errors"
- content="Dear All,
- The Instance ${ORACLE_SID}\\\' alert log occured the ora errors ,please see the detail in attachment and take action for it. many thanks!
- Oracle Alert Services
- "
- echo "#!/usr/bin/perl" >> ${pl_sendmail}
- echo "use Mail::Sender;" >> ${pl_sendmail}
- echo "\\$sender = new Mail::Sender {smtp => \'10.xxx.xxx.xxx\', from => \'xxxx@xxxx.com\'}; ">> ${pl_sendmail}
- echo "\\$sender->MailFile({to => \'kerry@xxxxx.com\',">> ${pl_sendmail}
- echo "cc=>\'konglb@esquel.com\'," >> ${pl_sendmail}
- echo "subject => \'$subject\',">> ${pl_sendmail}
- echo "msg => \'$content\',">> ${pl_sendmail}
- echo "file => \'$logfile\'});">> ${pl_sendmail}
- perl ${pl_sendmail}
*/20 6-21 * * * /home/oracle/scripts/monitoring_alert_log.sh >/dev/null 2>&1
问题/优化脚本:Crontab 定时任务配置每二十分钟执行一次,结果,又有麻烦事情来了,假如8点发生了ORA错误,之后到下午6点都没有发生ORA错误,上面的脚本会每隔二十分钟发送一次邮件,重复发送,感觉比较烦人,而我需要的是:只有当新的ORA错误出现,才给DBA发送邮件,否则就不要发送,其次,感觉二十分钟的时间段太长了,如果出现了严重错误,二十分钟后才去处理,就显得时延比较滞后,但是如果你频率短的话, 基于第一个bug,你回收到N多邮件,那么我们继续改写,优化下面脚本吧
- #****************************************************************************************************
- # FileName :monitoring_alert_log.sh
- #****************************************************************************************************
- # Author :Kerry
- # CreateDate :2013-07-01
- # Description :check the alert log and find out the ora error
- #****************************************************************************************************
- # Modified Date Modified User Version Modified Reason
- # 2013-07-02 Kerry V01.0.1 add comment and modified script
- # 2013-07-02 Kerry V01.0.2 Solved the email repated send problems, only
- # the new ora error occured then send the email.
- #****************************************************************************************************
- #!/bin/bash
- # these solved the oracle variable problem.
- export ORACLE_SID=gsp
- export ORACLE_BASE=/u01/app/oracle
- new_log_file="/home/oracle/scripts/new_err_log.txt"
- old_log_file="/home/oracle/scripts/old_err_log.txt"
- pl_monitoring_alert="/home/oracle/scripts/monitoring_alert_log.pl"
- pl_sendmail="/home/oracle/scripts/sendmail.pl"
- alert_logfile="$ORACLE_BASE/admin/$ORACLE_SID/bdump/alert_${ORACLE_SID}.log"
- #delete the old alter error log file
- #rm -f${new_log_file}
- rm -f${old_log_file}
- mv ${new_log_file}${old_log_file}
- rm -f${pl_sendmail}
- #run the perl and check if exists the ora error
- perl ${pl_monitoring_alert} ${alert_logfile}> ${new_log_file}
- #if have no error in alert log then exit the program
- if [[ -e "${new_log_file}" && ! -s "${new_log_file}" ]]; then
- exit;
- fi
- new_err_num=`cat ${new_log_file} | wc -l`
- old_err_num=`cat ${old_log_file} | wc -l`
- if [ ${new_err_num} -le ${old_err_num} ]; then
- exit
- fi
- date_today=`date +%Y_%m_%d`
- subject="xxx (192.168.xxx.xxx) Monitoring the Oracle Alert logs and find ora errors"
- content="Dear All,
- The Instance ${ORACLE_SID}\\\' alert log occured the ora errors ,please see the detail in attachment and take action for it. many thanks!
- Oracle Alert Services
- "
- echo "#!/usr/bin/perl" >> ${pl_sendmail}
- echo "use Mail::Sender;" >> ${pl_sendmail}
- echo "\\$sender = new Mail::Sender {smtp => \'10.xxx.xxx.xxx\', from => \'xxxx@xxxx.com\'}; ">> ${pl_sendmail}
- echo "\\$sender->MailFile({to => \'kerry@xxxxxx.com\',">> ${pl_sendmail}
- echo "cc=>\'xxxxx@xxxxx.com\'," >> ${pl_sendmail}
- echo "subject => \'$subject\',">> ${pl_sendmail}
- echo "msg => \'$content\',">> ${pl_sendmail}
- echo "file => \'${new_log_file}\'});">> ${pl_sendmail}
- perl ${pl_sendmail}
但是我在部署过程中,由于环境问题(多台ORACLE服务器,不同的操作系统、不同的环境),发送邮件的部分出现改动,又有下面两个小版本的改动
- #**********************************************************************************
- # FileName : monitoring_alert_log.sh
- #**********************************************************************************
- # Author : Kerry
- # CreateDate : 2013-07-01
- # Description: check the alert log and find out the ora error
- #***********************************************************************************
- # Modified Date Modified User Version Modified Reason
- # 2013-07-02 Kerry V01.0.1 add comment and modified script
- # 2013-07-02 Kerry V01.0.2 Solved the email repated send problems, only
- # the new ora error occured then send the email
- #***********************************************************************************
- #!/bin/bash
- new_log_file="/home/oracle/scripts/new_err_log.txt"
- old_log_file="/home/oracle/scripts/old_err_log.txt"
- pl_monitoring_alert="/home/oracle/scripts/monitoring_alert_log.pl"
- email_content="/home/oracle/scripts/sendmail.txt"
- alert_logfile="$ORACLE_BASE/admin/$ORACLE_SID/bdump/alert_$ORACLE_SID.log"
- #delete the old alter error log file
- rm -f${old_log_file}
- mv ${new_log_file} ${old_log_file}
- rm -f${pl_sendmail}
- #run the perl and check if exists the ora error
- perl ${pl_monitoring_alert} ${alert_logfile}> ${new_log_file}
- #if have no error in alert log then exit the program
- if [[ -e "${new_log_file}" && ! -s "${new_log_file}" ]]; then
- exit;
- fi
- new_err_num=`cat ${new_log_file} | wc -l`
- old_err_num=`cat ${old_log_file} | wc -l`
- if [ ${new_err_num} -le ${old_err_num} ]; then
- exit
- fi
- date_today=`date +%Y_%m_%d`
- subject="Monitoring the Oracle Alert logs and find ora errors"
- content="Dear All,
- The Instance ${ORACLE_SID}\\\' alert log occured the ora errors ,please see the detail in attachment and take action for it. many thanks!
- The Error is blow :
- "
- echo \'Content-Type: text/html\' > ${email_content}
- echo \'To: xxxxx@xxxxx.com\' >> ${email_content}
- echo ${subject} >> ${email_content}
- echo \'<pre style="font-family: courier; font-size: 9pt">\' >> ${email_content}
- echo ${content} >> ${email_content}
- cat ${new_log_file} >>${email_content} 2>&1
- echo \'Oracle Alert Services\' >> ${email_content}
- /usr/sbin/sendmail -t -f ${subject} < ${email_content}
- rm -f ${email_content}
- #**********************************************************************************
- # FileName : monitoring_alert_log.sh
- #**********************************************************************************
- # Author : Kerry
- # CreateDate :2013-07-01
- # Description: check the alert log and find out the ora error
- #***********************************************************************************
- # Modified Date Modified User Version Modified Reason
- # 2013-07-02 Kerry V01.0.1 add comment and modified script
- # 2013-07-02 Kerry V01.0.2 Solved the email repated send problems, only
- # the new ora error occured then send the email
- #***********************************************************************************
- #!/bin/bash
- new_log_file="/home/oracle/scripts/new_err_log.txt"
- old_log_file="/home/oracle/scripts/old_err_log.txt"
- pl_monitoring_alert="/home/oracle/scripts/monitoring_alert_log.pl"
- email_content="/home/oracle/scripts/sendmail.pl"
- alert_logfile="$ORACLE_BASE/admin/$ORACLE_SID/bdump/alert_$ORACLE_SID.log"
- reportname="alert_log_err.txt"
- #delete the old alter error log file
- rm -f${old_log_file}
- mv ${new_log_file} ${old_log_file}
- rm -f${pl_sendmail}
- #run the perl and check if exists the ora error
- perl ${pl_monitoring_alert} ${alert_logfile}> ${new_log_file}
- #if have no error in alert log then exit the program
- if [[ -e "${new_log_file}" && ! -s "${new_log_file}" ]]; then
- exit;
- fi
- date_today=`date +%Y_%m_%d`
- subject="Monitoring the Oracle Alert logs and find ora errors"
- content="Dear All,
- The Instance ${ORACLE_SID}\\\' alert log occured the ora errors ,please see the detail in attachment and take action for it. many thanks!
- Oracle Alert Services
- "
- ( ${content} ; uuencode ${new_log_file} ${reportname} ) | /bin/mail -s ${subject} xxxx@xxxx.com xxxxx@xxx.com
- /bin/mail
以上是关于归档—监控ORACLE数据库告警日志的主要内容,如果未能解决你的问题,请参考以下文章
Oracle归档日志满了导致Oracle连接(ORA-00257)报错处理