Logstash optional fields in logfile
Asked Answered
F

1

32

I'm trying to parse a logfile using grok

Each line of the logfile has fields separated by commas:

13,home,ABC,Get,,Private, Public,1.2.3 ecc...

I'm using match like this: match => [ "message", "%{NUMBER:requestId},%{WORD:ServerHost},%{WORD:Service},...

My question is: Can I allow optional field? At times some of the fileds might be empty ,,

Is there a pattern that matches a string like this 2.3.5 ? ( a kind of version number )

Fruiter answered 6/5, 2015 at 17:30 Comment(1)
Apart from the grok filter the csv filter is made for parsing this type of data.Kip
G
78

At it's base, grok is based on regular expressions, so you can surround a pattern with ()? to make it optional -- for example (%{NUMBER:requestId})?,

If there isn't a grok pattern that suits your needs, you can always create a named extraction like this: (?<version>[\d\.]+) which would extract into version, a string that has any number of digits and dots in it.

Gasparo answered 6/5, 2015 at 18:48 Comment(5)
Excellent answer! I have same problem and your answer saved me hours for searching!Handbag
Yes, this worked perfectly for me too. @Gasparo I've seen you answer other questions - what good references do you recommend? I find there's a context gap between the grok docs and the underlying reg-exp references!Sacaton
Just an observation, that the optional modifier does not seem to apply for the GREEDYDATA pattern. For example: (%{GREEDYDATA:x})? %{IP:ipaddr} (%{GREEDYDATA:y})?Obediah
@ifelsemonkey Facing the same issue. Will post an answer if get any solution or workaround.Augmentative
We used the (%{DATA:x})?Gilchrist

© 2022 - 2024 — McMap. All rights reserved.