【问题标题】:logstash if statement within grok statementgrok 语句中的 logstash if 语句
【发布时间】:2015-03-14 06:14:06
【问题描述】:

我正在创建一个 logstash grok 过滤器以从备份服务器中提取事件,并且我希望能够测试某个字段的模式,如果它与该模式匹配,则进一步处理该字段并提取其他信息.

为此,我将if 语句嵌入grok 语句本身。这导致测试在if 之后以Error: Expected one of #, => 失败。

这是过滤器语句:

filter {
    grok {
        patterns_dir => "./patterns"
        # NetWorker logfiles have some unusual fields that include undocumented engineering codes and what not
        # time is in 12h format (ugh) so custom patterns need to be used.
        match => [ "message", "%{NUMBER:engcode1} %{DATESTAMP_12H:timestamp}  %{NUMBER:engcode2} %{NUMBER:engcode3} %{NUMBER:engcode4} %{NUMBER:ppid} %{NUMBER:pid} %{NUMBER:engcode5} %{WORD:processhost} %{WORD:processname} %{GREEDYDATA:daemon_message}" ]
        # attempt to find completed savesets and pull that info from the daemon_message field
        if [daemon_message] =~ /done\ saving\ to\ pool/  { 
            grok {
                match => [ "daemon_message", "%{WORD:savehost}\:%{WORD:saveset} done saving to pool \'%{WORD:pool}\' \(%{WORD:volume}\) %{WORD:saveset_size}" ]
            }
        }
    }
    date {
        # This is requred to set the time from the logline to the timestamp and not have it create it's own.
        # Note the use of the trailing 'a' to denote AM or PM. 
        match => ["timestamp", "MM/dd/yyyy HH:mm:ss a"]
    } 
}

此块失败并显示以下内容:

$ /opt/logstash/bin/logstash -f ./networker_daemonlog.conf --configtest
Error: Expected one of #, => at line 12, column 12 (byte 929) after # Basic dumb simple networker daemon log grok filter for the NetWorker daemon.log 
# no smarts to this and not really pulling any useful info from the files (yet)
filter {
    grok {
... lines deleted ...
        # attempt to find completed savesets and pull that info from the daemon_message field
        if 

我是 logstash 的新手,我意识到在 grok 语句中使用条件可能是不可能的,但我更喜欢以这种方式进行条件处理而不是额外的 match 行,因为这会留下 daemon_message在提取我想要的数据时,字段完好无损,可用于其他用途。

ETA:我还应该指出,完全删除 if 语句允许 configtest 通过,过滤器解析日志。

提前谢谢...

【问题讨论】:

    标签: logstash logstash-grok


    【解决方案1】:

    条件超出过滤器,例如:

    if [field] == "value" {
         grok {
              ...
         }
    ]
    

    应该是正确的。在你的情况下,做第一个 grok,然后测试运行第二个,即:

    grok {
        match => [ "message", "%{NUMBER:engcode1} %{DATESTAMP_12H:timestamp}  %{NUMBER:engcode2} %{NUMBER:engcode3} %{NUMBER:engcode4} %{NUMBER:ppid} %{NUMBER:pid} %{NUMBER:engcode5} %{WORD:processhost} %{WORD:processname} %{GREEDYDATA:daemon_message}" ]
    }
    if [daemon_message] =~ /done\ saving\ to\ pool/  {
        grok {
            match => [ "daemon_message", "%{WORD:savehost}\:%{WORD:saveset} done saving to pool \'%{WORD:pool}\' \(%{WORD:volume}\) %{WORD:saveset_size}" ]
        }  
    }
    

    这实际上是为匹配的记录运行两个正则表达式。由于 grok 只会在正则表达式匹配时生成字段,因此您可以这样做:

    grok {
        match => [ "message", "%{NUMBER:engcode1} %{DATESTAMP_12H:timestamp}  %{NUMBER:engcode2} %{NUMBER:engcode3} %{NUMBER:engcode4} %{NUMBER:ppid} %{NUMBER:pid} %{NUMBER:engcode5} %{WORD:processhost} %{WORD:processname} %{GREEDYDATA:daemon_message}" ]
    }
    grok {
        match => [ "daemon_message", "%{WORD:savehost}\:%{WORD:saveset} done saving to pool \'%{WORD:pool}\' \(%{WORD:volume}\) %{WORD:saveset_size}" ]
    }
    

    您必须测量实际日志文件的性能,因为这将运行更少的正则表达式,但第二个更复杂。

    如果您真的想发疯,可以使用 break_on_match 功能一口气完成所有这些操作。

    【讨论】:

    • 好吧,我害怕那个。我希望能够在填充新字段时拆分 daemon_message 字段,但这似乎超出了logstash软件的范围。谢谢..
    • 上面的例子是从你的输入中创建新的字段;你还需要什么?
    • 啊,对不起,我误会了。是的,这正是我想要的。再次感谢。
    • 这些条件是否记录在任何地方?
    • @ElzoValugi,他们一直在移动文档,但当前链接位于 elastic.co/guide/en/logstash/current/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 2013-11-05
    • 1970-01-01
    相关资源
    最近更新 更多