# Logstash grok filter not working

**URL:** https://discuss.elastic.co/t/logstash-grok-filter-not-working/60914
**Category:** Logstash
**Created:** [September 19, 2016, 4:31pm UTC](https://discuss.elastic.co/t/logstash-grok-filter-not-working/60914 "2016-09-19T16:31:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Roberto\_Iglesias](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@Roberto\_Iglesias](https://discuss.elastic.co/u/Roberto_Iglesias)
#### Post date: [September 19, 2016, 4:31pm UTC](https://discuss.elastic.co/t/logstash-grok-filter-not-working/60914/1 "2016-09-19T16:31:36Z")

</div>

Hi and thanks for reading.  
I'm trying to do my own postfix/imapd logs filter.  
This is what i made:  
`%{SYSLOGTIMESTAMP:timestamp} %{WORD:mail} %{WORD:program}/?(%{WORD:subprogram})?\['?(%{BASE10NUM:id})?\]?:? %{GREEDYDATA:message}`

And this is my grok filter

```
filter {
    	grok {
    		match => { "message" => "%{SYSLOGTIMESTAMP:timestamp} %{WORD:mail} %{WORD:program}/?(%{WORD:subprogram})?\[?(%{BASE10NUM:id})?\]?:? %{GREEDYDATA:message}" }
 }

```

At [grokdebug.herokuapp.com](https://grokdebug.herokuapp.com/) it works. But Logstash give me this:

`{:timestamp=>"2016-09-19T13:18:07.660000-0300", :message=>"fetched an invalid config", :config=>"input {\r\n\tfile {\r\n\t\tpath => \"/var/log/maillog\"\r\n\t\ttype => \"maillog\"\r\n\t\tstart_position => \"beginning\"\r\n\t\tignore_older => 0\r\n\t}\r\n}\r\n\r\nfilter {\r\n\tgrok {\r\n\t\tmatch => { \"message\" => \"%{SYSLOGTIMESTAMP:timestamp} %{WORD:mail} %{WORD:program}/?(%{WORD:subprogram})?\\[?(%{BASE10NUM:id})?\\]?:? %{GREEDYDATA:message}\" }\r\n}\r\n\r\noutput {\r\n\telasticsearch {\r\n\t\thosts => [\"localhost:9200\"]\r\n\t}\r\n}\r\n\n", :reason=>"Expected one of #, => at line 16, column 16 (byte 342) after filter {\r\n\tgrok {\r\n\t\tmatch => { \"message\" => \"%{SYSLOGTIMESTAMP:timestamp} %{WORD:mail} %{WORD:program}/?(%{WORD:subprogram})?\\[?(%{BASE10NUM:id})?\\]?:? %{GREEDYDATA:message}\" }\r\n}\r\n\r\noutput {\r\n\telasticsearch ", :level=>:error}`

Hope this helps to understand. Thanks!

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [September 19, 2016, 7:41pm UTC](https://discuss.elastic.co/t/logstash-grok-filter-not-working/60914/2 "2016-09-19T19:41:52Z")

</div>

It looks like there's a closing brace missing; you're never closing the filter block.

---

<div class="post-metadata">

### Author: ![Roberto\_Iglesias](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@Roberto\_Iglesias](https://discuss.elastic.co/u/Roberto_Iglesias)
#### Post date: [September 19, 2016, 8:01pm UTC](https://discuss.elastic.co/t/logstash-grok-filter-not-working/60914/3 "2016-09-19T20:01:03Z")

</div>

Thanks for reply. I fixed this problem just doing what you said.  
I keep going on with this. Now i have another issue (i don't want to open a new topic).  
I have this filter:  
\> filter {  
\> grok {  
\> match =\> { "message" =\> "%{SYSLOGTIMESTAMP:timestamp} %{WORD:mail} %{WORD:program}/?(%{WORD:subprogram})?[?(%{BASE10NUM:id})?]?:?} %{WORD:action}%{GREEDYDATA:action\_info}" }  
\> }  
\> if [action] == "disconnect" or [action] == "connect" {  
\> grok {  
\> match =\> { "action\_info" =\> " from %{GREEDYDATA:host}[%{IPV4:ip\_host}]" }  
\> }  
\> }  
\> }

And it is not working. Logstash add the tag "\_grokparsefailure" for each log.  
..."tags":["\_grokparsefailure"]}

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [September 19, 2016, 8:02pm UTC](https://discuss.elastic.co/t/logstash-grok-filter-not-working/60914/4 "2016-09-19T20:02:59Z")

</div>

Please show the whole event as produced by a `stdout { codec => rubydebug }` output.

---

<div class="post-metadata">

### Author: ![Roberto\_Iglesias](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@Roberto\_Iglesias](https://discuss.elastic.co/u/Roberto_Iglesias)
#### Post date: [September 19, 2016, 8:25pm UTC](https://discuss.elastic.co/t/logstash-grok-filter-not-working/60914/5 "2016-09-19T20:25:25Z")

</div>

The `stdout { codec => rubydebug }` output is too long to put here, but is extremely repetitive, so i copy a paragraph:

```
{
       "message" => "Sep 18 04:40:15 mail postfix/smtpd[1979]: connect from localhost[127.0.0.1]",
      "@version" => "1",
    "@timestamp" => "2016-09-19T20:15:53.638Z",
          "path" => "/var/log/dsaasd",
          "host" => "ELK-System",
          "type" => "registro_mail",
          "tags" => [
        [0] "_grokparsefailure"
    ]
}
```

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [September 20, 2016, 6:29am UTC](https://discuss.elastic.co/t/logstash-grok-filter-not-working/60914/6 "2016-09-20T06:29:23Z")

</div>

Multiple problems, mainly here: `[?(%{BASE10NUM:id})?]?:?}`

- The square brackets must be escaped.
- Why all the question marks everywhere?
- What's up with the closing brace at the end?

Suggestion if the bracketed pid should be optional: `(\[%{BASE10NUM:id}\])?`

When debugging things like this, always start with a short expression, in this case e.g. `%{SYSLOGTIMESTAMP:timestamp}`, then add more and more until things break.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 4:37am UTC](https://discuss.elastic.co/t/logstash-grok-filter-not-working/60914/7 "2017-07-06T04:37:52Z")

</div>


