如何从文本文件行解析logstash / grok中的json?

时间:2015-11-26 11:49:30

标签: elasticsearch logstash logstash-grok

我有一个看起来像这样(简化)的日志文件

日志示例

MyLine data={"firstname":"bob","lastname":"the builder"}

我想提取数据中包含的json并创建两个字段,一个用于firstname,一个用于last。但是,我得到的输出是:

{"message":"Line data={\"firstname\":\"bob\",\"lastname\":\"the builder\"}\r","@version":"1","@timestamp":"2015-11-26T11:38:56.700Z","host":"xxx","path":"C:/logstashold/bin/input.txt","MyWord":"Line","parsedJson":{"firstname":"bob","lastname":"the builder"}}

如你所见

..."parsedJson":{"firstname":"bob","lastname":"the builder"}}

这不是我需要的,我需要在kibana中为firstname和lastname创建字段,但logstash不会使用json过滤器提取字段。

LogStash配置

input {
  file {
        path => "C:/logstashold/bin/input.txt"        
       }
}

filter {     

   grok {
            match => { "message" => "%{WORD:MyWord} data=%{GREEDYDATA:request}"}        
        }   

    json{
        source => "request"
        target => "parsedJson"
        remove_field=>["request"]
    }   
}   

output {  
    file{
        path => "C:/logstashold/bin/output.txt"
    }   
}

任何帮助都非常感谢,我确定我错过了一些简单的

由于

1 个答案:

答案 0 :(得分:19)

json过滤器添加另一个名为mutate的过滤器后,添加您将从parsedJson字段中获取的两个字段。

filter {
  ...
  json {
     ...
  }
  mutate {
    add_field => {
      "firstname" => "%{[parsedJson][firstname]}"
      "lastname" => "%{[parsedJson][lastname]}"
    }
  }
}

对于上面的示例日志行,将给出:

{
       "message" => "MyLine data={\"firstname\":\"bob\",\"lastname\":\"the builder\"}",
      "@version" => "1",
    "@timestamp" => "2015-11-26T11:54:52.556Z",
          "host" => "iMac.local",
        "MyWord" => "MyLine",
    "parsedJson" => {
        "firstname" => "bob",
         "lastname" => "the builder"
    },
     "firstname" => "bob",
      "lastname" => "the builder"
}