Grok匹配json字段和值

时间:2016-02-25 14:14:25

标签: json logstash grok

我使用带有bunyan的koajs将错误日志保存到我的服务器然后我使用filebeat将它们发送到我的logstash应用程序。

我的错误日志正在正确转发,但我现在想创建一个过滤器,它会在特定日志中添加标记。

{"name":"myapp","hostname":"sensu-node-dev","pid":227,"level":50,"err":{"message":"Cannot find module 'lol'","name":"Error","stack":"Error: Cannot find module 'lol'\n    at Function.Module._resolveFilename (module.js:339:15)\n    at Function.Module._load (module.js:290:25)\n    at Module.require (module.js:367:17)\n    at require (internal/module.js:16:19)\n    at Object.<anonymous> (/srv/www/dev.site/app.js:27:6)\n    at next (native)\n    at Object.<anonymous> (/srv/www/dev.site/node_modules/koa-compose/index.js:29:5)\n    at next (native)\n    at onFulfilled (/srv/www/dev.site/node_modules/co/index.js:65:19)\n    at /srv/www/dev.site/node_modules/co/index.js:54:5","code":"MODULE_NOT_FOUND"},"msg":"Cannot find module 'lol'","time":"2016-02-24T22:04:26.492Z","v":0}

现在,特定日志中有趣的部分是"err":{...}"name":"Error"位。为简单起见,我只想创建一个过滤器,在日志中检测"name":"Error"(如果存在),然后将标记add_tag => ["error"]应用于日志。

这是我的/etc/logstash/conf.d/logstash.conf文件:

input {
  beats {
   port => 5044
   type => "logs"
  }
}
filter {
  grok { 
    type => "log"
    pattern => "???" // <--- have no idea what to do here
    add_tag => ["error"]
  }
}
output {
  elasticsearch {
    hosts => "localhost:9200"
    sniffing => true
    manage_template => false
    index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
    document_type => "%{[@metadata][type]}"
  }
  http {
    http_method => "post"
    url => "<MY_URL>"
    format => "message"
    message => "{"text":"dis is workinz, you has error"}"
    tags => ["error"]
  }
}

我尝试了以下内容:

pattern => ""name":"Error""

但是出现了以下错误:

Error: Expected one of #, {, } at line 9, column 31 (byte 107) after filter {
  grok {
    match => { "message" => ""
You may be interested in the '--configtest' flag which you can
use to validate logstash's configuration before you choose
to restart a running system.

在任何地方都没有这种特定匹配类型的简单示例。

奖励:另外如何在logstash中逃脱,我无法找到关于这个主题的任何内容?

1 个答案:

答案 0 :(得分:0)

如果您只想查看邮件中是否存在字符串,请尝试以下操作:

if [message] =~ /"name":"Error"/ {
    mutate {
        add_tag { ... }
    }
}

如果你真的想将输入信息输入到字段中,请查看json编解码器或过滤器。