ELK文集

Logstash Filter 中文解读

2017-11-28  本文已影响89人  Zparkle

1.grok

Description

Parse arbitrary text and structure it.
Grok is a great way to parse unstructured log data into something structured and queryable.
This tool is perfect for syslog logs, apache and other webserver logs, mysql logs, and in general, any log format that is generally written for humans and not computer consumption.
Logstash ships with about 120 patterns by default. You can find them here:https://github.com/logstash-plugins/logstash-patterns-core/tree/master/patterns. You can add your own trivially. (See the'patterns_dir'setting)

grok可以解析任意的文本并将其结构化。
该工具在针对syslog, apache或者其他的webserver或者例如mysql跟其他一些杂七杂八的东西会特别好用=- =。而且对log的格式化仅仅是为了数据显示更加人性化,不会增加计算消耗。
Logstash本身针对不同语言有120种默认的匹配模式(实际上很容易看到是正则表达式),你也可以写你自己的表达式并且提一个pull request;

Grok Basics

Grok 通过将文本标签与log内容进行匹配实现格式化。
格式:%{SYNTAX:SEMANTIC}
SYNTAX是标签的名字,SEMANTIC是通过标签映射得到的数据的存放变量。

栗子
1234 55.3.244.1
可以匹配 %{NUMBER:IDKFKDATA} %{IP:clientaddr}
该匹配会将前一个数字存入名为“IDKFKDATA”的字段内,而后一个数据会被识别为IP地址并存入clientaddr字段。

默认情况下所有字段的存储类型为String,如果你希望其他的存储类型
%{NUMBER:num:int}使用这种匹配将产生int类型的字段

Examples: With that idea of a syntax and semantic, we can pull out useful fields from a sample log like this fictional http request log:

5.3.244.1 GET /index.html 15824 0.043

The pattern for this could be:

%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}

自定义过滤标签

(?<field_name>the pattern here)

直接用这个就能自定义一个正则把数据存入field_name.
也可以写文件自定义一个pattern

一些选项

Example:
match => {
"field1" => "value1"
"field2" => "value2"
...
}

最常用的就是对message字段进行格式化

filter {
grok { match => { "message" => "Duration: %{NUMBER:duration}" } }

}

如果想对同一个字段多次格式化

filter {
grok { match => { "message" => [ "Duration: %{NUMBER:duration}", "Speed: %{NUMBER:speed}" ] } }
}

其中"Duration: "是正则直接匹配相对应的字符,%{}是grok的匹配标签,前者为正则后者为字段

试验部分 发送一个字段包含message="May 29 16:37:11 sadness logger: hello world"
调用

filter {
 grok {
   match => { "message" => "%{SYSLOGBASE} %{DATA:message}" }
   overwrite => [ "message" ]
 }
}

试验结果,grok有作用 %{SYSLOGBASE}在linuxlog文件中书写,成功得到对应字段
但是%{DATA:message}匹配失败
查询发现DATA -> .*?加了问号导致非贪婪模式什么都没有匹配到(garbage garbage garbage)

不加override设置的效果是message字段变成了数组,[0]是原来的message内容,[1],存储的是匹配出来的"hello world"
增加了override设置之后message字段只有"hello world"数据
顺便测试了自定义匹配的可用性还是靠谱的。

一些公共选项(后续不再重复介绍)

filter {
  grok {
    add_field => { "foo_%{somefield}" => "Hello world, from %{host}" }
  }
}

没错很好用,每一款filter都可以加这个玩意儿

结果
区别还是比较大的,add_tag实际上是在tags字段下添加数据,不会新产生字段,而add_field可以产生新的字段

You can also remove multiple fields at once:

filter {
 grok {
   remove_field => [ "foo_%{somefield}", "my_extraneous_field" ]
 }
}

字面意思,删除某些字段

值得注意的是某些标签的使用需要在过滤器成功工作的前提下,如果你的标签没有效果,记得检查一下前面的过滤主体(有的标签必须在有过滤得情况下才能起作用)


2.Aggregate

Description

The aim of this filter is to aggregate information available among several events (typically log lines) belonging to a same task, and finally push aggregated information into final task event.

You should be very careful to set Logstash filter workers to 1 (-w 1 flag) for this filter to work correctly otherwise events may be processed out of sequence and unexpected results will occur.

该过滤器的目的是将多条消息的数据聚合成一条消息,提供"code"字段可以对int属性进行自定义的增加减少,然后丢到某一个最终消息中去,然后进入output过程。

不过为了使用这个过滤器,你需要将Logstash的过滤器参数设置为1 (-w 1 flag)这样该过滤器才能正确工作。否则会掉头发。

总的来说是一个很迷的过滤器,请尽量在来源或者Kibana中完成消息聚合, 使用该Filter极其麻烦。

拒绝翻译这个东西 = =


3.Mutate

英文文档

Description

The mutate filter allows you to perform general mutations on fields. You can rename, remove, replace, and >modify fields in your events.

变形(?)过滤器。 允许你对字段做一般的改变。 你可以改名,删除,替代,修改收到消息中的参数。

如果你认真读了上面的你会发现grok也提供删除字段的功能。实际上相当多的过滤器提供了大量的重复功能,不过我认为针对不同操作尽量调用相对应的过滤器会令配置简洁明了。

Example:

filter {
 mutate {
   convert => { "fieldname" => "integer" }
 }
}

其中涉及到True False有一些转换的规则,详情请从title下面的英文文档链接点进去。(没错自己看吧hhhh)

Example:

filter {
 mutate {
    copy => { "source_field" => "dest_field" }
 }
}

Example:

filter {
 mutate {
   gsub => [
     # replace all forward slashes with underscore
     "fieldname", "/", "_",
     # replace backslashes, question marks, hashes, and minuses
     # with a dot "."
     "fieldname2", "[\\?#-]", "."
   ]
 }
}

你会发现有两个反斜杠,这没错,你需要给正则里面的所有反斜杠加反斜杠。。。我知道这有点绕口hhh。

Example:

filter {
 mutate {
   join => { "fieldname" => "," }
 }
}

Example:

filter {
 mutate {
   lowercase => [ "fieldname" ]
 }
}

hash也可以merge
反正你自己玩=。=

Example:

filter {
 mutate {
    merge => { "dest_field" => "added_field" }
 }
}

Example:

filter {
 mutate {
   replace => { "message" => "%{source_host}: My new message" }
 }
}
mutate{
  split => {"fieldname" => "," }
}

4.Date

Descriptionedit

The date filter is used for parsing dates from fields, and then using that date or timestamp as the logstash timestamp for the event.
For example, syslog events usually have timestamps like this:

"Apr 17 09:32:01"

You would use the date format

MMM dd HH:mm:ss

to parse this.
The date filter is especially important for sorting events and for backfilling old data. If you don’t get the date correct in your event, then searching for them later will likely sort out of order.
In the absence of this filter, logstash will choose a timestamp based on the first time it sees the event (at input time), if the timestamp is not already set in the event. For example, with file input, the timestamp is set to the time of each read.

日期过滤器是一个用来解析日期格式的过滤器,并将解析出来的日期作为logstash的时间戳使用。
栗子
日期过滤器是一个对整理消息重新回填旧数据非常重要的过滤器。如果你没有在你的消息中正确的获取到时间,那么之后对他们的搜索很可能会失去顺序。
如果没有该过滤器并且时间中没有设置时间戳,logstash会根据他首次获取到消息的时间设置时间戳,比如从文件中读取消息,那么每次读取的时间将会作为时间戳。

Date Filter Configuration Options 日期过滤器配置选项

上一篇 下一篇

猜你喜欢

热点阅读