Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse json in logstash /grok from a text file line?

I have a logfile which looks like this ( simplified)

Logline sample

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

I'd like to extract the json contained in data and create two fields, one for firstname, one for last. However, the ouput i get is this:

{"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"}}

As you can see

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

That's not what I need, I need to create fields for firstname and lastname in kibana, but logstash isn't extracting the fields out with the json filter.

LogStash Config

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"
    }   
}

Any help greatly appreciated, I'm sure I'm missing out something simple

Thanks

like image 381
bingbon Avatar asked Nov 26 '15 11:11

bingbon


1 Answers

After your json filter add another one called mutate in order to add the two fields that you would take from the parsedJson field.

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

For your sample log line above that would give:

{
       "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"
}
like image 187
Val Avatar answered Nov 17 '22 15:11

Val