首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >grok语句中的logstash if语句

grok语句中的logstash if语句
EN

Stack Overflow用户
提问于 2015-03-14 14:14:07
回答 1查看 28.5K关注 0票数 6

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

为此,我在grok语句本身中嵌入了一条if语句。这会导致紧跟在if之后的Error: Expected one of #, =>测试失败。

以下是filter语句:

代码语言:javascript
复制
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"]
    } 
}

此阻止失败,并出现以下错误:

代码语言:javascript
复制
$ /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语句会使配置测试通过,并允许过滤器解析日志。

先谢谢你...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-16 00:21:44

条件条件超出了过滤器的范围,因此类似于:

代码语言:javascript
复制
if [field] == "value" {
     grok {
          ...
     }
]

都是正确的。在您的情况下,执行第一个grok,然后测试以运行第二个,即:

代码语言:javascript
复制
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}" ]
    }  
}

这实际上是为匹配的记录运行两个regexp。由于grok仅在regexp匹配时生成字段,因此您可以这样做:

代码语言:javascript
复制
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}" ]
}

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

如果你真的想发疯,你可以使用break_on_match特性在一个grok{}中完成所有这些操作。

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29046055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档