Logstash Grok pattern with double quotes

c-val picture c-val · Feb 24, 2016 · Viewed 19.3k times · Source

I am parsing proxy logs with Logstash and its Grok filter. The logs contain quoted strings :

1438120705 [.....] "SEF-EDP8" - "C"
"/GPM/1023/5745-7/456V/"

With the Grok Debugger the following pattern works like a charm :

%{NUMBER:ts} [......] (-|"%{USERNAME:token1}") (-|%{DATA:token2}) (-|"%{WORD:token3}") (-|"%{DATA:token4}")

This does not work with Logstash's Grok because of the double quotes in the grok pattern. Logstash error log :

Error: Expected one of #, {, } at line 9, column 204 (byte 374) after
filter {
    grok {
        match => { "message" => "%{NUMBER:ts} [......] ("

So I use the QuotedString grok pattern instead :

%{NUMBER:ts} [......] (-|%{QS:token1}) (-|%{DATA:token2}) (-|%{QS:token3}) (-|%{QS:token4})

This works with the Grok Debugger as well, but quotes are extracted with quoted strings. It doesn't work with Logstash either :

token1 : ""SEF-EDP8"" token2 : null token3 : ""C"" token4 :
""/GPM/1023/5745-7/456V/""

How can I make it work with Logstash? How can I remove these unwanted extra double quotes?

Answer

NileshP picture NileshP · Jul 5, 2016

If you escape " with backslash then it works fine.

%{NUMBER:ts} [......] (-|"%{USERNAME:token1}") (-|%{DATA:token2}) (-|"%{WORD:token3}") (-|"%{DATA:token4}")

Your new string will look like

%{NUMBER:ts} [......] (-|\"%{USERNAME:token1}\") (-|%{DATA:token2}) (-|\"%{WORD:token3}") (-|\"%{DATA:token4}\")