如何使用bash脚本来捕获运行日志? [名侦探柯南] [英] How to capture running log using bash script? [Case Closed]

查看:186
本文介绍了如何使用bash脚本来捕获运行日志? [名侦探柯南]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在bash脚本一名新球员。还有,我想用bash脚本知道捕获日志文件的东西。

比方说,有是存储日志文下面格式文件每隔一小时一台服务器。

 文件[20160509_130000] .LOG

日志文件有详细的信息是这样的。


  13:00:00 INFO [file.chdev130]事件:ADIEVN_PLAY_DONE,成品,为nbytes = 16360
13:00:00 INFO [file.chdev39] adiCollectDigits()成功


我的问题是我如何可以读取和使用bash脚本存储运行日志或前一小时来获得特定参数(例如事件),以新的文件?

谁能教教我该怎么办呢?谢谢你。

更新
在这里,我要的流量(这时候我想知道的第一点是如何工作的):


  1. 之前获取运行日志或一小时。

  2. 阅读15分钟时间间隔(13点零零分00秒 - 13时15分零零秒)。

  3. 用grep在该时间间隔参数(例如事件)。

  4. 计数的参数。

  5. 将其存储到另一个文件。

解决

下面,以防有人解决方案需要它。


  1. 列出使用基于时间戳 LS -t 中的所有内容,然后用管道

  2. 使用的grep -v ^ D (我仍然不知道^ D确切的解释),管再次

  3. 显示前几行与

所以结果是,


  LS -t * .LOG | grep的-v ^ D |头-1(用于显示正在运行的日志)
LS -t * .LOG | grep的-v ^ D |头-2 |尾-1(用于显示正在运行的日志之前的一个日志)


希望对大家有所帮助。谢谢

==侦探柯南==


解决方案

您可以计算出当前或使用日期,使用您的日志文件的previous日志文件名名约定:

  FNAME =文件[`日期-v-1H'+%Y%M%D_%H0000'`] .LOG

会给你previous文件名。省略 -v-1H 来获得当前之一。

对于15分钟的间隔,你可以使用像正则表达式'^ \\ D \\ D:(0 \\ D | 1 [0-4]) 00为:00-14:59的时间间隔,'^ \\ D \\ D:(1 [5-9] | 2 \\ D) 15:00-29:59,等等
例如,在第一个正则表达式, ^ 行的开始,匹配\\ D \\ D:匹配两数字和冒号,而(0 \\ D | 1 [0-4])可以匹配0与任何相邻的数字,或1与相邻的数字从0到4。在第二个正则表达式,(1 [5-9] | 2 \\ D)用数字5-9,或2匹配所有数字1。

然后的grep -Po(小于=事件:?)+(?=,)在你的日志事件的类型,假设类型事件总是以结束,。这正则表达式会贪婪地匹配任何符号,尽可能多的,因为它可以从一个符号开始,如果他们是字符串事件之间:事件:本身不匹配,这就是后向/向前看符号是)。然后使用排序| uniq的-c 来计算不同事件的条目数。

所以生成的脚本看起来像

  FNAME =文件[`日期-v-1H'+%Y%M%D_%H0000'`] .LOG
grep的-P'^ \\ D \\ D:(0 \\ D | 1 [0-4])'$ FNAME| grep的-Po'; +(=,?)(LT =事件?)。|排序| uniq的-c> 的/ tmp / $ fname_00-14的第15分钟#项
grep的-P'^ \\ D \\ D:(1 [5-9] | 2 \\ D)'$ FNAME| grep的-Po'; +(=,?)(LT =事件?)。|排序| uniq的-c> 的/ tmp / $ fname_15-29为第二15分钟#项
grep的-P'^ \\ D \\ D:(3 \\ D | 4 [0-4])'$ FNAME| grep的-Po'; +(=,?)(LT =事件?)。|排序| uniq的-c> 的/ tmp / $ fname_30-44第三15分钟#项
grep的-P'^ \\ D \\ D:(4 [5-9] | 5 \\ D)'$ FNAME| grep的-Po'; +(=,?)(LT =事件?)。|排序| uniq的-c> 的/ tmp / $ fname_45-59第四15分钟#项

在过去的小时数。

另一种方法是使用 logtail :用cron条目来获得最后的日志条目,而不是grepping。你可以设置你的脚本被由cron在00,15,30和45分钟之内,每小时运行,确定它里面的日志文件名和logtail像 logtail -f$ FNAME。这里的渔获将是00几分钟内运行时,你需要使用 -v-1H 开关日期,这种方法并不像grepping出准确时代。

I'm a new player in bash scripting. There's something that I want to know about capture logfile using bash script.

Let's say there is a server which store logfile every hour with format file below.

file[20160509_130000].log

The logfile has detailed information like this.

13:00:00 INFO  [file.chdev130] Event: ADIEVN_PLAY_DONE, Finished  , nbytes=16360
13:00:00 INFO  [file.chdev39] adiCollectDigits() success

My question is how can i read and store the running log or one hour before to get specific parameter (e.g. "Event") to new file using bash scripting?

Can someone teach me how to do it? Thanks.

Update Here the flow that I want (for this time I want to know how the first point works):

  1. Get the running log or one hour before.
  2. Read the 15 minute interval (13:00:00 - 13:15:00).
  3. grep the parameter (e.g. "Event) in that interval.
  4. Count the parameter.
  5. Store it to another file.

SOLVED

Here the solution in case someone need it.

  1. List all the content based on time stamp using ls -t then pipe it
  2. Use grep -v ^d (i still doesn't know the exact explanation for ^d), pipe again
  3. Display first few lines with head

So the result is,

ls -t *.log | grep -v ^d | head -1 (for display the running log)
ls -t *.log | grep -v ^d | head -2 | tail -1 (for display the one log before the running log)

Hope it'll help. Thanks

== Case Closed ==

解决方案

You can figure out the current or the previous log filename using date, using your logfile name convention:

fname="file[`date -v-1H '+%Y%m%d_%H0000'`].log"

will give you the previous filename. Omit -v-1H to get the current one.

For the 15-minute intervals, you may use regexps like '^\d\d:(0\d|1[0-4])' for 00:00-14:59 interval, '^\d\d:(1[5-9]|2\d)' for 15:00-29:59, etc. For example, in the first regex, ^ matches the beginning of the line, \d\d: matches "two digits and a colon", and (0\d|1[0-4]) matches either 0 with any adjacent digit, or 1 with adjacent digit from 0 to 4. In the second regex, (1[5-9]|2\d) matches 1 with digit from 5 to 9, or 2 with any digit.

Then you grep -Po '(?<=Event: ).+(?=,)' the type of events in your log, assuming that the type of event always ends with a ,. That regexp will greedily match any symbols, as many as it can, starting from one symbol, if they are between strings Event: and , (Event: and , themselves are not matched, that's what lookbehind/lookaheads are for). Then use sort | uniq -c to count number of different events entries .

So the resulting script would look something like

fname="file[`date -v-1H '+%Y%m%d_%H0000'`].log"
grep -P '^\d\d:(0\d|1[0-4])' "$fname" | grep -Po '(?<=Event: ).+(?=,)' | sort | uniq -c > "/tmp/$fname_00-14"  # entries for first 15 minutes
grep -P '^\d\d:(1[5-9]|2\d)' "$fname" | grep -Po '(?<=Event: ).+(?=,)' | sort | uniq -c > "/tmp/$fname_15-29"  # entries for second 15 minutes
grep -P '^\d\d:(3\d|4[0-4])' "$fname" | grep -Po '(?<=Event: ).+(?=,)' | sort | uniq -c > "/tmp/$fname_30-44"  # entries for third 15 minutes
grep -P '^\d\d:(4[5-9]|5\d)' "$fname" | grep -Po '(?<=Event: ).+(?=,)' | sort | uniq -c > "/tmp/$fname_45-59"  # entries for fourth 15 minutes

for the last hour log.

Another approach is to use logtail with cron entries to get last log entries, instead of grepping. You can set up your script to be run by cron at 00, 15, 30 and 45 minutes each hour, determine the log filename inside it and logtail the file like logtail -f "$fname". The catch here would be that when running at 00 minutes, you'd need to use the -v-1H switch for date, and this approach is not as accurate as grepping out the times.

这篇关于如何使用bash脚本来捕获运行日志? [名侦探柯南]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆