在 logstash 过滤条件中使用 ruby 变量

Use ruby variable in logstash filter condition

我有以下 logstash 配置:

input { stdin { } }

output {
  elasticsearch { hosts => ["localhost:9200"] }
  stdout { codec => rubydebug }
}

filter {


  ruby {
    code => "
      @@exists_pattern = ['foo', 'bar'].any?{ |pattern| event.get('message').include?(pattern) }
      event.add('keep_line', @@exists_pattern)
    "
  }


  if not [keep_line] { drop { } }


  grok {
    match => {
      "message" => '%{IP:serverip} \[%{HTTPDATE:my_timestamp}\]'
    }
  }

  date {
    match => [ "my_timestamp", "dd/MMM/YYYY:HH:mm:ss Z"]
    target => "@timestamp"
  }
}

但是当我尝试 运行 logstash 使用这个配置文件时,我得到这个错误:

[ERROR][logstash.agent] Cannot create pipeline {:reason=>"Expected one of #, ( at line 30, column 10 (byte 923) after filter {\n  # grok...

如何在 if 指令中使用 ruby 代码块(事件['keep_line'])中设置的变量?

您不能在 Logstash 过滤器的 if 条件中使用否定。尝试将布尔条件转换为正值。