logstash6配置语法中的条件判断

详情可见官方文档-conditionals。

有时您只想在特定条件下过滤或输出事件。为此,您可以使用条件(conditional)。比如在elk系统中想要添加一个type类型的关键字来根据不同的条件赋值,最后好做统计。

Logstash中的条件查看和行为与编程语言中的条件相同。

条件语支持if,else if和else语句并且可以嵌套。

条件语法如下:

if EXPRESSION {
  ...
} else if EXPRESSION {
  ...
} else {
  ...
}

比较操作:

相等: ==, !=, <, >, <=, >=
正则: =~(匹配正则), !~(不匹配正则)
包含: in(包含), not in(不包含)

布尔操作:

and(与), or(或), nand(非与), xor(非或)
一元运算符:

表达式可能很长且很复杂。表达式可以包含其他表达式,您可以使用!来取反表达式,并且可以使用括号(…)对它们进行分组。

! (取反)
() (复合表达式), !() (对复合表达式结果取反)
实例
如若action的值是login,则通过mutate filter删除secret字段:

filter {
  if [action] == "login" {
    mutate { remove_field => "secret" }
  }
}

若是需要通过正则匹配(即,当if的表达式为正则时),匹配成功后添加自定义字段:

filter {
    if [message] =~ /\w+\s+\/\w+(\/learner\/course\/)/ {
        mutate {
            add_field => { "learner_type" => "course" }
        }
    }
}

在一个条件里指定多个表达式:

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" }
  }
}

not in示例:

output {
  if "_grokparsefailure" not in [tags] {
    elasticsearch { ... }
  }
}

@metadata field
在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 }
  }
}

输出结果

$ bin/logstash -f ../test.conf
Pipeline main started
asdf
{
    "@timestamp" => 2016-06-30T02:42:51.496Z,
      "@version" => "1",
          "host" => "windcoder.com",
          "show" => "This data will be in the output",
       "message" => "asdf"
}

"asdf"变成message字段内容。条件与@metadata内嵌的test字段内容判断成功,但是输出并没有展示@metadata字段和其内容。

不过,如果指定了metadata => true,rubydebug codec允许显示@metadata字段的内容。

   stdout { codec => rubydebug { metadata => true } }

输出结果

$ bin/logstash -f ../test.conf
Pipeline main started
asdf
{
    "@timestamp" => 2016-06-30T02:46:48.565Z,
     "@metadata字段及其子字段内容。" => {
           "test" => "Hello",
        "no_show" => "This data will not be in the output"
    },
      "@version" => "1",
          "host" => "windcoder.com",
          "show" => "This data will be in the output",
       "message" => "asdf"
}

现在就可以见到@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 }
}

输出结果

$ bin/logstash -f ../test.conf
Pipeline main started
02/Mar/2014:15:36:43 +0100
{
    "@timestamp" => 2014-03-02T14:36:43.000Z,
      "@version" => "1",
          "host" => "windcoder.com",
       "message" => "02/Mar/2014:15:36:43 +0100"
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值