logtash if grok语句中的语句

迈克尔·科尤特

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

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

这是过滤器语句:

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字段完整保留用于其他用途,同时撤消我想要的数据。

预计到达时间:我还应该指出,完全删除该if语句可以使configtest通过并且过滤器可以解析日志。

提前致谢...

阿兰·柯林斯(Alain Collins)

条件过滤器位于过滤器之外,因此类似:

if [field] == "value" {
     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功能一口气完成所有这些工作{}。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章