如果匹配则添加字段

时间:2017-01-12 18:01:34

标签: match logstash logstash-grok

我正在监控irc服务器。只有当消息与内部的特定单词匹配时,我才会掠夺创建新数字字段(例如:Alert_level)的方法。 示例:消息:ABC | Alert_level:1;消息:ZYX | Alert_level:3。

正在运行的代码

input {
        irc {
                channels => "#xyz"
                host => "a.b.c"
                nick => "myusername"
                catch_all => true
          get_stats => true
        }
}


output {
        stdout { codec => "rubydebug" }
        elasticsearch {
                                        hosts => "localhost"
                                        index => "logstash-irc-%{+YYYY.MM.dd}"
        }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

正如上面提到的@Val,您可能需要使用grok过滤器来匹配require "date" (array_of_date_ranges.flat_map(&:to_a) & given_date_range.to_a) .uniq.sort.chunk_while{|x, y| x.next == y} .flat_map{|x, *, y| [x, y]}[1...-1] .each_slice(2) .map{|x, y| [x + 1, y - 1]} # => [ [#<Date: 2014-08-11 ((2456881j,0s,0n),+0s,2299161j)>, #<Date: 2015-03-01 ((2457083j,0s,0n),+0s,2299161j)>], [#<Date: 2015-04-10 ((2457123j,0s,0n),+0s,2299161j)>, #<Date: 2016-03-04 ((2457452j,0s,0n),+0s,2299161j)>] ] 中的内容。例如,您的input可能如下所示:

filter

注意您必须进行转换才能通过filter { grok { match => { "message" => "%{GREEDYDATA:somedata}" } } if "ZYX" in [message]{ <-- change your condition accordingly mutate { add_field => { "%{Alert_level}" => "12345" } <-- somefield is the field name convert => { "Alert_level" => "integer" } <-- do the conversion } } } 创建数字字段,您无法直接创建数字字段。以上只是一个示例,以便您可以重现。根据您的要求更改logstash匹配。希望它有所帮助!