免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 12703 | 回复: 38

请教各位大牛:获取日志信息的关键字,logger发送给messages文件,谢谢. [复制链接]

论坛徽章:
0
发表于 2011-05-17 15:39 |显示全部楼层
30可用积分
本帖最后由 deargentle 于 2011-05-20 15:29 编辑

目的:定时检查alert.log是否包含定义的关键字,获取数据库的信息,并将信息发送给系统的messages

描述:
数据库日志文件:/oracle/alert.log
读取alert.log检查是否含有ORA-和error、Error的错误信息,如有把日志行,通过logger (logger -f 可以实现),写到/var/log/messages文件里。
每隔30分钟检查,如果log有新增的错误信息则再发送给messages,没有则不发送,之前检查出的不要重复写
(如果下一个检查期间里增加的日志里有匹配的关键字则记录,不管关键字是否重复,有几条就写几条。)

不知道我描述清楚了吗,请帮忙写个脚本,谢谢。

alert日志样本:
Mon Feb 16 16:25:31 2009
ORA-1543 signalled during: create tablespace activemq datafile '/oracle/oradata/db/mq.dbf' size 256m extent management local segment space  management auto
Wed Feb 25 16:35:38 2009
WARNING: inbound connection timed out (ORA-3136)
Errors in file /oracle/admin/db/bdump/db_j000_31126.trc:
ORA-12012: error on auto execute of job 8887
ORA-44003: invalid SQL name
Tue Mar 17 23:01:00 2009
ARC1: Failed to archive thread 1 sequence 1568 (19809)
Wed Jul 29 12:50:57 2009
Errors in file /oracle/admin/db/bdump/db_arc0_3206.trc:
ORA-19815: WARNING: db_recovery_file_dest_size of 2147483648 bytes is 100.00% used, and has 0 remaining bytes available.
Wed Jul 29 12:50:57 2009
Linux-x86_64 Error: 13: Permission denied
Wed Jul 29 12:55:24 2009
ARCH: Error 19504 Creating archive log file to '/archivelog/1_1568_673098364.dbf'
ARCH: Failed to archive thread 1 sequence 1568 (19504)
ORA-16038 signalled during: alter database open...
Wed Jul 29 12:55:24 2009
Errors in file /oracle/admin/db/bdump/db_arc1_29171.trc:
ORA-19504: failed to create file "/archivelog/1_1569_673098364.dbf"
ORA-27040: file create error, unable to create file


ywlscpl  脚本tail -f 不能实时,美中不足。tail -f 肯定没有问题,不知道tail 监测文件变化的机制
nuclearxin  脚本再次运行前需要清空日志,否则重复检查,美中不足。

   谢了各位朋友

论坛徽章:
0
发表于 2011-05-17 15:39 |显示全部楼层
本帖最后由 nuclearxin 于 2011-05-17 18:58 编辑
时间可以忽略掉,主要是判断不重复写日志

如果log有新增的错误信息则再发送给messages,没有则不发送,曾 ...
deargentle 发表于 2011-05-17 17:22

  1. #!/bin/sh
  2. STARTLINE=1;
  3. while :
  4. do
  5. NEXTSTART=`wc -l /oracle/alert.log|cut -d" " -f1`

  6. sed -rn "$STARTLINE,\${/(ORA-)|(error)|(Error)/p}" /oracle/alert.log |xargs -i logger -t oracle -p syslog.error {}
  7. STARTLINE=$NEXTSTART
  8. STARTLINE=`expr $STARTLINE + 1 `
  9. sleep 1800
  10. ENDLINE=`wc -l /oracle/alert.log|cut -d" " -f1`

  11. done
复制代码

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
发表于 2011-05-17 16:01 |显示全部楼层
回复 1# deargentle


    供你参考:
  1. #!/bin/bash

  2. if grep 'ORA-|error|Error' /oracle/alert.log >/dev/null
  3. then
  4. logger -f /oracle/alert.log > /var/log/messages
  5. sleep 1800
  6. else
  7. sleep 1800
  8. fi
复制代码

论坛徽章:
10
天蝎座
日期:2013-09-22 22:32:23程序设计版块每日发帖之星
日期:2016-08-07 06:20:00lufei
日期:2016-06-17 17:38:40程序设计版块每日发帖之星
日期:2016-06-12 06:20:002016科比退役纪念章
日期:2016-05-31 15:47:20CU十四周年纪念徽章
日期:2016-05-27 12:24:562015年亚洲杯之阿曼
日期:2015-05-03 21:01:352015年辞旧岁徽章
日期:2015-03-03 16:54:15天蝎座
日期:2013-10-20 21:05:24程序设计版块每日发帖之星
日期:2016-08-11 06:20:00
发表于 2011-05-17 16:08 |显示全部楼层
回复 2# yinyuemi


    没有满足每隔30分钟检查啊?
    怎样在linux 下建一个任务,每30min 运行一次脚本啊,求大神赐教

论坛徽章:
0
发表于 2011-05-17 16:12 |显示全部楼层
本帖最后由 deargentle 于 2011-05-17 16:16 编辑

crontab也可以啊,呵呵
*/30   *   *   *   *   cmd

关键是怎么实现不能重复 , alert.log日志有时间戳的,就是不知道怎么做
“如果log有新增的错误信息则再发送给messages,没有则不发送,之前检查出的不要重复写”
单纯的把日志发送给message,没有问题。

我需求的目的是通过日志服务器收集日志,再做分析和预警。

论坛徽章:
10
天蝎座
日期:2013-09-22 22:32:23程序设计版块每日发帖之星
日期:2016-08-07 06:20:00lufei
日期:2016-06-17 17:38:40程序设计版块每日发帖之星
日期:2016-06-12 06:20:002016科比退役纪念章
日期:2016-05-31 15:47:20CU十四周年纪念徽章
日期:2016-05-27 12:24:562015年亚洲杯之阿曼
日期:2015-05-03 21:01:352015年辞旧岁徽章
日期:2015-03-03 16:54:15天蝎座
日期:2013-10-20 21:05:24程序设计版块每日发帖之星
日期:2016-08-11 06:20:00
发表于 2011-05-17 16:14 |显示全部楼层
本帖最后由 liion631818 于 2011-05-17 16:16 编辑

怎么区分是不同的error? 只用error Error ORA- 没办法判断啊

论坛徽章:
0
发表于 2011-05-17 16:25 |显示全部楼层
本帖最后由 nuclearxin 于 2011-05-17 18:57 编辑

  1. #!/bin/sh
  2. STARTLINE=1;
  3. while :
  4. do
  5. NEXTSTART=`wc -l /oracle/alert.log|cut -d" " -f1`

  6. sed -rn "$STARTLINE,\${/(ORA-)|(error)|(Error)/p}" /oracle/alert.log |xargs -i logger -t oracle -p syslog.error {}
  7. STARTLINE=$NEXTSTART
  8. STARTLINE=`expr $STARTLINE + 1 `
  9. sleep 1800
  10. ENDLINE=`wc -l /oracle/alert.log|cut -d" " -f1`

  11. done
复制代码

论坛徽章:
0
发表于 2011-05-17 16:28 |显示全部楼层
时间无法精确了。。。
除非用 fork
perl 容易fork
shell的话需要 用来read pipe 来模拟
麻烦 容易被抢hehe

论坛徽章:
0
发表于 2011-05-17 16:43 |显示全部楼层
本帖最后由 deargentle 于 2011-05-17 16:51 编辑

时间不用太精确,循环检查过的不用重复写。

呵呵。

论坛徽章:
0
发表于 2011-05-17 16:56 |显示全部楼层
文件是一直变大吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP