logstash支持的数据类型有:html
logstash字段引用语法。要在 Logstash 配置中使用字段的值,只须要把字段的名字写在中括号 [] 里就好了,这就叫字段引用。还需注意字段层次。若是引用的是一个顶级字段,能够省略[],直接指定字段名。要引用嵌套的字段,须要指定完整的路径,如[top-level field][nested field]。mysql
下面有五个顶级字段(agent, ip, request, response, ua) 和三个嵌套字段 (status, bytes, os)。sql
{ "agent": "Mozilla/5.0 (compatible; MSIE 9.0)", "ip": "192.168.24.44", "request": "/index.html" "response": { "status": 200, "bytes": 52353 }, "ua": { "os": "<a href="http://www.ttlsa.com/windows/" title="windows"target="_blank">Windows</a> 7" } }apache
1编程 2json 3windows 4数组 5ruby 6elasticsearch 7 8 9 10 11 12 |
{ "agent": "Mozilla/5.0 (compatible; MSIE 9.0)", "ip": "192.168.24.44", "request": "/index.html" "response": { "status": 200, "bytes": 52353 }, "ua": { "os": "Windows 7" } } |
为了引用os字段,需指定[ua][os]。引用顶级字段如request,能够简单指定request便可。
字段引用格式也能够用于logstash调用sprintf格式。这种格式能够从其余字符串中引用字段值。如:
output { statsd { increment => "apache.%{[response][status]}" } }
1 2 3 4 5 |
output { statsd { increment => "apache.%{[response][status]}" } } |
也能够格式化时间。如:
output { file { path => "/var/log/%{type}.%{+yyyy.MM.dd.HH}" } }
1 2 3 4 5 |
output { file { path => "/var/log/%{type}.%{+yyyy.MM.dd.HH}" } } |
使用条件来决定filter和output处理特定的事件。
logstash条件相似于编程语言。条件支持if、else if、else语句,能够嵌套。
条件语法以下:
if EXPRESSION { ... } else if EXPRESSION { ... } else { ... }
1 2 3 4 5 6 7 |
if EXPRESSION { ... } else if EXPRESSION { ... } else { ... } |
比较操做有:
==
, !=
, <
, >
, <=
, >=
=~(匹配正则)
, !~(不匹配正则)
in(包含)
, not in(不包含)
布尔操做:
and(与)
, or(或)
, nand(非与)
, xor(非或)
一元运算符:
!(取反)
()
(复合表达式), !()
(对复合表达式结果取反)如mutate filter删除secret字段对于action是login的:
filter { if [action] == "login" { mutate { remove => "secret" } } }
1 2 3 4 5 |
filter { if [action] == "login" { mutate { remove => "secret" } } } |
在一个条件里指定多个表达式:
output { # Send production errors to pagerduty if [loglevel] == "ERROR" and [deployment] == "production" { pagerduty { ... } } }
1 2 3 4 5 6 7 8 |
output { # Send production errors to pagerduty if [loglevel] == "ERROR" and [deployment] == "production" { pagerduty { ... } } } |
在in条件,能够比较字段值:
filter { if [foo] in [foobar] { mutate { add_tag => "field in field" } } if [foo] in "foo" { mutate { add_tag => "field in string" } } if "hello" in [greeting] { mutate { add_tag => "string in field" } } if [foo] in ["hello", "world", "foo"] { mutate { add_tag => "field in list" } } if [missing] in [alsomissing] { mutate { add_tag => "shouldnotexist" } } if !("foo" in ["hello", "world"]) { mutate { add_tag => "shouldexist" } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
filter { if [foo] in [foobar] { mutate { add_tag => "field in field" } } if [foo] in "foo" { mutate { add_tag => "field in string" } } if "hello" in [greeting] { mutate { add_tag => "string in field" } } if [foo] in ["hello", "world", "foo"] { mutate { add_tag => "field in list" } } if [missing] in [alsomissing] { mutate { add_tag => "shouldnotexist" } } if !("foo" in ["hello", "world"]) { mutate { add_tag => "shouldexist" } } } |
output { if "_grokparsefailure" not in [tags] { elasticsearch { ... } } }
1 2 3 4 5 |
output { if "_grokparsefailure" not in [tags] { elasticsearch { ... } } } |
字段引用、sprintf格式、条件判断只能用于filter和output,不能用于input。
在logstash1.5版本开始,有一个特殊的字段,叫作@metadata。@metadata包含的内容不会做为事件的一部分输出。
input { stdin { } } filter { mutate { add_field => { "show" => "This data will be in the output" } } mutate { add_field => { "[@metadata][test]" => "Hello" } } mutate { add_field => { "[@metadata][no_show]" => "This data will not be in the output" } } } output { if [@metadata][test] == "Hello" { stdout { codec => rubydebug } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 |
input { stdin { } }
filter { mutate { add_field => { "show" => "This data will be in the output" } } mutate { add_field => { "[@metadata][test]" => "Hello" } } mutate { add_field => { "[@metadata][no_show]" => "This data will not be in the output" } } }
output { if [@metadata][test] == "Hello" { stdout { codec => rubydebug } } } |
查看输出:
$ bin/logstash -f ../test.conf Logstash startup completed asdf { "message" => "asdf", "@version" => "1", "@timestamp" => "2015-03-18T23:09:29.595Z", "host" => "www.ttlsa.com", "show" => "This data will be in the output" }
1 2 3 4 5 6 7 8 9 10 |
$ bin/logstash -f ../test.conf Logstash startup completed asdf { "message" => "asdf", "@version" => "1", "@timestamp" => "2015-03-18T23:09:29.595Z", "host" => "www.ttlsa.com", "show" => "This data will be in the output" } |
"asdf"变成message字段内容。条件与@metadata内嵌的test字段内容判断成功,可是输出并无展现@metadata字段和其内容。
不过,若是指定了metadata => true,rubydebug codec容许显示@metadata字段的内容。
stdout { codec => rubydebug { metadata => true } }
1 |
stdout { codec => rubydebug { metadata => true } } |
下面是输出的内容:
$ bin/logstash -f ../test.conf Logstash startup completed asdf { "message" => "asdf", "@version" => "1", "@timestamp" => "2015-03-18T23:10:19.859Z", "host" => "www.ttlsa.com", "show" => "This data will be in the output", "@metadata" => { "test" => "Hello", "no_show" => "This data will not be in the output" } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ bin/logstash -f ../test.conf Logstash startup completed asdf { "message" => "asdf", "@version" => "1", "@timestamp" => "2015-03-18T23:10:19.859Z", "host" => "www.ttlsa.com", "show" => "This data will be in the output", "@metadata" => { "test" => "Hello", "no_show" => "This data will not be in the output" } } |
能够看到@metadata字段及其子字段内容。
注意:只有rubydebug codec能够显示@metadata字段内容。
确保@metadata字段临时须要,不但愿最终输出。最多见的情景是filter的时间字段,须要一临时的时间戳。如:
input { stdin { } } filter { grok { match => [ "message", "%{HTTPDATE:[@metadata][timestamp]}" ] } date { match => [ "[@metadata][timestamp]", "dd/MMM/yyyy:HH:mm:ss Z" ] } } output { stdout { codec => rubydebug } }
1 2 3 4 5 6 7 8 9 10 |
input { stdin { } }
filter { grok { match => [ "message", "%{HTTPDATE:[@metadata][timestamp]}" ] } date { match => [ "[@metadata][timestamp]", "dd/MMM/yyyy:HH:mm:ss Z" ] } }
output { stdout { codec => rubydebug } } |
输出结果:
$ bin/logstash -f ../test.conf Logstash startup completed 02/Mar/2014:15:36:43 +0100 { "message" => "02/Mar/2014:15:36:43 +0100", "@version" => "1", "@timestamp" => "2014-03-02T14:36:43.000Z", "host" => "example.com" }
1 2 3 4 5 6 7 8 9 |
$ bin/logstash -f ../test.conf Logstash startup completed 02/Mar/2014:15:36:43 +0100 { "message" => "02/Mar/2014:15:36:43 +0100", "@version" => "1", "@timestamp" => "2014-03-02T14:36:43.000Z", "host" => "example.com" } |