归档—监控ORACLE数据库告警日志
ORACLE的告警日志里面包含许多有用的信息,尤其是一些ORACLE的ORA错误信息,所以有必要及时归档、监控数据库告警日志的ORA错误,及时提醒数据库管理员DBA处理这些错误信息,那么我们首先来看看告警日志的内容片断:
Thread 1 advanced to log sequence 37749 (LGWR switch)
Current log# 6 seq# 37749 mem# 0: /u01/oradata/SCM2/redo06.log
Thu Jun 27 15:02:30 2013
Thread 1 advanced to log sequence 37750 (LGWR switch)
Current log# 2 seq# 37750 mem# 0: /u01/oradata/SCM2/redo02.log
Thu Jun 27 15:13:43 2013
Thread 1 advanced to log sequence 37751 (LGWR switch)
Current log# 3 seq# 37751 mem# 0: /u01/oradata/SCM2/redo03.log
Thu Jun 27 15:25:30 2013
Thread 1 advanced to log sequence 37752 (LGWR switch)
Current log# 4 seq# 37752 mem# 0: /u01/oradata/SCM2/redo04.log
Thu Jun 27 15:32:20 2013
ORA-00060: Deadlock detected. More info in file /u01/app/oracle/admin/SCM2/bdump/scm2_s001_14052.trc.
Thu Jun 27 15:35:05 2013
Thread 1 advanced to log sequence 37753 (LGWR switch)
Current log# 5 seq# 37753 mem# 0: /u01/oradata/SCM2/redo05.log
Thu Jun 27 15:43:11 2013
Thread 1 advanced to log sequence 37754 (LGWR switch)
Current log# 1 seq# 37754 mem# 0: /u01/oradata/SCM2/redo01.log
Thu Jun 27 15:49:58 2013
Thread 1 advanced to log sequence 37755 (LGWR switch)
Current log# 6 seq# 37755 mem# 0: /u01/oradata/SCM2/redo06.log
Thu Jun 27 16:01:25 2013
Thread 1 advanced to log sequence 37756 (LGWR switch)
Current log# 2 seq# 37756 mem# 0: /u01/oradata/SCM2/redo02.log
Thu Jun 27 16:12:14 2013
Thread 1 advanced to log sequence 37757 (LGWR switch)
Current log# 3 seq# 37757 mem# 0: /u01/oradata/SCM2/redo03.log
Thu Jun 27 16:24:10 2013
Thread 1 advanced to log sequence 37758 (LGWR switch)
归档告警日志文件
告警日志文件如果不加管理的话,那么文件会持续增长,有时候文件会变得非常大,不利于读写。一般建议将告警日志按天归档,归档文件保留三个月(视情况而定),下面来看看将告警日志文件归档的两个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数据库告警日志
目标:查看alert日志 su - oracle cd $ORACLE_BASE/diag/rdbms/LXY/LXY/trace tail -100f alert_LXY.log 我的ORACLE_ ...
- Oracle数据库重做日志及归档日志的工作原理说明
Oracle数据库重做日志及归档日志的工作原理: lgwr进程将redo log buffer中的重做数据写入到redo log中,此时的redo log分组,每当一个redo log group写满 ...
- 使用zabbix监控oracle的后台日志
本文将介绍如何使用zabbix监控oracle的后台日志,当oracle后台日志出现“ORA-”或“Error”时,第一时间将该信息报警出来 zabbix agent端 以下所有操作均用root执行 ...
- Zabbix+Orabbix监控oracle数据库表空间
Orabbix 是设计用来为 zabbix 监控 Oracle 数据库的插件,它提供多层次的监控,包括可用性和服务器性能指标. 它提供了从众多 oracle 实例采集数据的有效机制,进而提供此信息的监 ...
- Linux如何用脚本监控Oracle发送警告日志ORA-报错发送邮件
Linux如何用脚本监控Oracle发送警告日志ORA-报错发送邮件 前言 公司有购买的监控软件北塔系统监控,由于购买的版权中只包含了有限台数据库服务器的监控,所以只监控了比较重要的几台服务器. 后边 ...
- 利用zabbix监控oracle数据库
一.概述 zabbix是一款非常强大,同时也是应用最为广泛的开源监控软件,本文将给大家介绍如何利用zabbix监控oracle数据库. 二.环境介绍 以下是我安装的环境,实际部署时并不需要跟我的环境一 ...
- 8个DBA最常用的监控Oracle数据库的常用shell脚本
本文介绍了8个常用的监控数据shell脚本.首先回顾了一些DBA常用的Unix命令,以及解释了如何通过Unix Cron来定时执行DBA脚本.网上也有好多类似的文章,但基本上都不能正常运行,花点时间重 ...
- 使用Zabbix监控Oracle数据库
Orabbix介绍 监控Oracle数据库我们需要安装第三方提供的Zabbix插件,我们先测试比较有名的Orabbix,http://www.smartmarmot.com/product/orabb ...
- 搜索表字段包含某字符串的SQL和监控Oracle数据库的SQL。
1.第一个SQL 背景:需要找到SQL Server数据库中,包含某个字符串的表,输出表和包含该字符串的列. )='=' --这里填要搜索的字符串 DECLARE @sql NVARCHAR(MAX) ...
随机推荐
- GCD-两个网络请求同步问题
在网络请求的时候有时有这种需求 两个接口请求数据,然后我们才能做最后的数据处理.但是因为网络请求是移步的 .我们并不知道什么时候两个请求完成 . 通常面对这样的需求会自然的想到 多线程 啊 .表现真正 ...
- 解决ajax跨域请求 (总结)
ajax跨域请求,目前已用几种方法实现: 1)用原生js的xhr对象实现. var url="http://freegeoip.net/json/" ...
- SIFT特征详解
1.SIFT概述 SIFT的全称是Scale Invariant Feature Transform,尺度不变特征变换,由加拿大教授David G.Lowe提出的.SIFT特征对旋转.尺度缩放.亮度变 ...
- 【十大经典数据挖掘算法】CART
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 前言 分类与回归树(Class ...
- 百度编辑器UEditor常用设置函数大全
在线文档对UEditor说明不够全面,收集了一些常用的方法和基本设置,以供参考.1.创建编辑器UE.getEditor('editor', { initialFrameWidth:"100% ...
- XmlReader和XElement组合之读取大型xml文档
简介 在.NET framework 中存在大量操作xml数据的类库和api,但在.NET framework 3.5后我们的首选一般就是linq to xml. linq to xml操作xml数据 ...
- 找不到方法:"!!0[] System.Array.Empty()".
找不到方法:"!!0[] System.Array.Empty()". 这个原因是没装.net framework 4.6
- SharePoint中新创建的Web Application在浏览器中报404错误
问题描述:在安装完成SharePoint 2010后,进入Central Administration,创建一个新的Web Application,可以正常创建,但访问时却返回404. 平台环境:Wi ...
- 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换
[源码下载] 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 作者:webabcd 介绍速战速决 之 PHP ...
- Linux中设定umask的作用
在linux中,常常都要提示设置: umask 022 其作用如下: 功能说明:指定在建立文件时预设的权限掩码.语 法:umask [-S][权限掩码]补充说明:umask可用来设定[权限掩 ...