2014-09-04 32 views
0

我对Unix命令非常陌生,并且想知道下面的操作脚本。监视应用程序日志并提取错误计数

  1. 需要阅读应用程序日志并在过去的半小时内拉出所有错误。
  2. 获取每个错误的唯一计数。
  3. 将错误计数邮寄给团队。

已采取的步骤: 我已经使用grep for关键字错误读取文件,并将其写入单独的文件。 给予文件许可。

感谢您的帮助。

代码段:

#!/bin/sh 
cd Service/apache-tomcat-7.0.33/logs 
for file in catalina.out; do 
grep "ERROR" $file >error.txt 
done 
chmod 0777 error.txt 

抽样日志

2014-09-03 16:45:36,814 ERROR xxxService: Could not find tool with id 365 intable: 
2014-09-03 16:45:56,444 ERROR yyyService: summary counts not returned from accessor for xxxx, 1, mapParams 
2014-09-03 16:45:56,444 ERROR yyyService: summary counts not returned from accessor for xxxx, 2, mapParams 
2014-09-03 16:45:56,445 ERROR yyyService: summary counts not returned from accessor for xxxx, 3, mapParams 
2014-09-03 16:45:56,445 ERROR yyyService: summary counts not returned from accessor for xxxx, 4, mapParams 
2014-09-03 16:45:56,445 ERROR yyyService: summary counts not returned from accessor for xxxx, 5, mapParams 
2014-09-03 16:46:00,077 ERROR yyyService: summary counts not returned from accessor for xxxx, 1, mapParams 
2014-09-03 16:46:00,078 ERROR yyyService: summary counts not returned from accessor for xxxx, 2, mapParams 
2014-09-03 16:46:00,078 ERROR yyyService: summary counts not returned from accessor for xxxx, 3, mapParams 
2014-09-03 16:46:00,078 ERROR yyyService: summary counts not returned from accessor for xxxx, 4, mapParams 
2014-09-03 16:46:00,079 ERROR yyyService: summary counts not returned from accessor for xxxx, 5, mapParams 
2014-09-03 16:46:05,415 ERROR yyyService: summary counts not returned from accessor for xxxx, 1, mapParams 
2014-09-03 16:46:05,416 ERROR yyyService: summary counts not returned from accessor for xxxx, 2, mapParams 
2014-09-03 16:46:05,416 ERROR yyyService: summary counts not returned from accessor for xxxx, 3, mapParams 
2014-09-03 16:46:05,416 ERROR yyyService: summary counts not returned from accessor for xxxx, 4, mapParams 
2014-09-03 16:46:05,417 ERROR yyyService: summary counts not returned from accessor for xxxx, 5, mapParams 
2014-09-03 16:46:59,881 ERROR yyyService: summary counts not returned from accessor for xxxx, 5, mapParams 
2014-09-03 16:47:03,109 ERROR ErrorManager: 1409780823108: A General Exception Occurred 
    null 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 

Error Message Count 
ERROR xxxService: Could not find tool with id 365 intable: 1 
ERROR yyyService: summary counts not returned from accessor for xxxx, 1, mapParams 3 
ERROR yyyService: summary counts not returned from accessor for xxxx, 2, mapParams 3 
ERROR yyyService: summary counts not returned from accessor for xxxx, 3, mapParams 3 
ERROR yyyService: summary counts not returned from accessor for xxxx, 4, mapParams 3 
ERROR yyyService: summary counts not returned from accessor for xxxx, 5, mapParams 4 
ERROR ErrorManager: 1409780823108: A General Exception Occurred 1 
+0

请提供日志文件的样本输出,包括一些线路是错误,还有一些不是。 – lxg 2014-09-04 19:14:59

+0

@lxg:感谢您宝贵的时间阅读我的疑问,并提供您现在需要的信息。再次感谢 – Vigneshwaran 2014-09-04 20:23:41

+0

哪些信息表明错误的类型? (你说你想按错误类型对它进行分组。)是xxxService/yyyService列吗? – lxg 2014-09-04 21:05:44

回答

0

我真的不能写一个完整的脚本,因为我并不完全清楚自己想要什么,但这里有一个策略。

  1. grep文件的字符串'错误'(看起来你已经这样做了)。
  2. 使用'cut -d''-f4提取第四个空格分隔的字段(yyyService)。
  3. 将输出管道排序,然后uniq -c获取计数。
  4. 管这样的结果为“邮件”

所以,你最终的东西是这样的:

grep ERROR /shipmentService/apache-tomcat-7.0.33/logs/catalina.out | cut -d ' ' -f4 | sort | uniq -c | mail -s "This is the subject" [email protected] 
+0

感谢您分享此脚本。你如何获得最后30分钟的日志。 – Vigneshwaran 2014-09-05 14:28:54

+0

这部分我不太确定 - 也许你可以预处理文件,提取时间戳并在'30分钟前'取回所有内容。然后将该文件传入我的代码上面? – 2014-09-05 15:09:56

相关问题