在grok语句中使用logstash if语句

mic*_*ote 4 logstash logstash-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"]
    } 
}
Run Code Online (Sandbox Code Playgroud)

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

$ /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 
Run Code Online (Sandbox Code Playgroud)

我是logstash的新手,我意识到在grok语句中使用条件可能是不可能的,但我更喜欢用这种方式对其他match行进行条件处理,因为这会使daemon_message字段保持原样用于其他用途,同时拉出我想要的数据.

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

提前致谢...

Ala*_*ins 14

条件在过滤器之外,所以类似于:

if [field] == "value" {
     grok {
          ...
     }
]
Run Code Online (Sandbox Code Playgroud)

会是对的.在你的情况下,做第一个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}" ]
    }  
}
Run Code Online (Sandbox Code Playgroud)

这实际上是为匹配的记录运行两个正则表达式.由于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}" ]
}
Run Code Online (Sandbox Code Playgroud)

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

如果你真的想要坚持下去,你可以使用break_on_match功能在一个grok {}中完成所有这些操作.

  • @ElzoValugi,他们不断移动文档,但目前的链接位于https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html#conditionals (3认同)