logstash icon indicating copy to clipboard operation
logstash copied to clipboard

Trouble parsing json with plain text file

Open sdumetz opened this issue 12 years ago • 11 comments

I have a nginx log file that I want to monitor using logstash. Log format is quite classic : one line with timestamp, verb, etc...

my grok pattern seems OK (debug work well)

I have this error :

{:timestamp=>"2014-10-24T12:40:06.035000+0200", 
:message=>"Trouble parsing json", 
:source=>"message", 
:raw=>"hostname.net 192.168.1.21 [2014-10-24T12:14:00+02:00] \"GET /favicon.ico HTTP/1.1\" 500 594 \"-\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36\" 0.012 -", 
:exception=>#<JSON::ParserError: unexpected token at 'hostname.net 192.168.1.21 [2014-10-24T12:14:00+02:00] "GET /favicon.ico HTTP/1.1" 500 594 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" 0.012 -'>, :level=>:warn
}

Obviously, message is no JSON. But as my codec is set to "plain", why does logstash try to parse it?

sdumetz avatar Oct 24 '14 10:10 sdumetz

Hi,

Could you pastebin/gist your complete config?

thanks!

electrical avatar Oct 24 '14 10:10 electrical

It's quite short I can quote it here :

input {
  file {
    path => "/var/log/nginx/access.log"
    codec => "line"
    type => "nginx_access"
  }
}
filter {
  if [type] == 'nginx_access'{
    grok {
      match => [
        "message", "%{IPORHOST:http_host} %{IPORHOST:clientip} \[%{TIMESTAMP_ISO8601:timestamp}\] \"(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})\" %{NUMBER:response} (?:%{NUMBER:bytes}|-) %{QS:referrer} %{QS:agent} %{NUMBER:request_time:float} %{NUMBER:upstream_time:float}"
      ]
    }
    date {
      match => [ "timestamp", "ISO8601" ]
    }
    geoip {
      source => "clientip"
    }
  }
}

output {
  elasticsearch { host => localhost }
  stdout { codec => rubydebug }
}

sdumetz avatar Oct 24 '14 11:10 sdumetz

Hi,

Could you try the 'plain' codec? That one is default in the file input and should work just fine with your data.

electrical avatar Oct 24 '14 12:10 electrical

Sorry, I didn't pay attention when pasting. The error I got was with the "plain" codec. I made some tests with the "line" codec and I just get no output at all.

sdumetz avatar Oct 24 '14 12:10 sdumetz

Ah okay,

That is very weird. The plain codec should work with all normal files. I'll try to reproduce it locally and get back to you.

Which LS version are you running btw?

electrical avatar Oct 24 '14 12:10 electrical

Output of logstash --version : logstash 1.4.2-modified

It's installed via the package repository on debian wheezy.

If it can help you : one of the file's line

hostname.net 192.168.1.31 [2014-10-24T14:02:30+02:00] "GET /favicon.ico HTTP/1.1" 500 594 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" 0.003 -

As far as I know, if this one works, every other I know of will work too.

sdumetz avatar Oct 24 '14 12:10 sdumetz

Cool,

Will try to do some testing on my side and i'll get back to you.

electrical avatar Oct 24 '14 12:10 electrical

Hi,

did you have time to test this? I'm quite confused by the way logstash behaves and starting to wonder if it could be an installation problem : I had a twitter input set up just for testing purpose and it was having the exact same issue.

Another problem is that it doesn't seem to happen every time. I've had a bunch of logs that passed through with no error. It can't be because of a different log format because my tested app have a limited amount of possible log lines that looks exactly the same.

Could the logstash installation be wrong in any way that would cause this problem to happen?

sdumetz avatar Nov 26 '14 06:11 sdumetz

I don't know if you ever got this working but I was having the same problem and I tried changing the coded to json (codec => json {}) and that solved the issue for me.

gershonsherman avatar Jan 12 '15 10:01 gershonsherman

Sorry for taking so long to answer.

@klepthys did you resolve your issue? From my local test setup the plain codec should work out of the box with regular file. If this is still a problem feel free to reopen this ticket.

ph avatar Jan 30 '15 15:01 ph

It's still a problem but I can't figure out any clue of why it doesn't work. I'll leave it closed and reopen it when I have time to investigate

sdumetz avatar Feb 04 '15 10:02 sdumetz