【问题标题】:Logstash: Keeping a value across eventsLogstash:跨事件保持价值
【发布时间】:2016-05-25 18:40:02
【问题描述】:

我有一个在每个日志文件中只出现一次的日期,我试图在匹配一次后将此日期添加到所有后续事件中,使其在某些方面像一个全局变量。 (日期在文档顶部,我无法使用multiline 或更改文件名或内容)

为此,我的方法是使用grep 过滤器和drop => false

grok {
    patterns_dir => "[...]"
    match => [ "message", "%{DATELINE}" ]
    tag_on_failure => [ ]
}
grep {
    add_field => { "grepdate" => "%{mydate}" }
    drop => false
}
date {
    locale => "en"
    timezone => "Europe/Paris"
    match => [ "grepdate", "yyyyMMdd" ]
    target => "grepdate"
}

正则表达式:

DATELINE (= Date: (?<mydate>[0-9]{8}))

我注意到grepdate 字段被正确添加到所有事件中——这是我想要的——但该字段的值不是日期本身(%{mydate} 的值),而是实际字符串"%{mydate}",除了第一次实际匹配时(在我的日志文件中解析实际日期时,grepdate字段包含正确的值)

我能做些什么来解决这个问题?

非常感谢任何帮助。

编辑:

我现在正在尝试使用memorizeplugin 的解决方案。但是,我收到以下错误:

不能使用超过 1 个过滤器,因为以下插件 不要与一个以上的工人一起工作:记住

有没有办法让这个过滤器线程安全?

【问题讨论】:

    标签: date grep logstash multiline logstash-grok


    【解决方案1】:

    也许您应该为此使用官方的aggregate filter,因为memorize 不是官方的并且will not work with Logstash >2.0

    会是这样的:

    # same as what you have now
    grok {
        patterns_dir => "[...]"
        match => [ "message", "%{DATELINE}" ]
        tag_on_failure => [ "not_date_line" ]
    
    }
    # add a fictional taskId field to correlate all lines
    mutate {
       add_field => { "taskId" => "all" }
    }
    
    # if we're processing the first line, remember the date
    if "not_date_line" not in [tags] {
        aggregate {
            task_id => "%{taskId}"
            code => "map['mydate'] = event['mydate']"
        }
    } 
    # if we're processing the next lines, add the date
    else {
        aggregate {
            task_id => "%{taskId}"
            code => "event['mydate'] = map['mydate']"
            map_action => "update"
            timeout => 0
        }
    }
    

    然后,您的所有事件都会有一个 mydate 字段,其中包含第一条日志行上的日期。

    【讨论】:

    • 这有帮助吗?
    猜你喜欢
    • 2015-09-28
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多