# Grok Parse failure with fail2ban

**URL:** <https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492>\
**Category:** Logstash\
**Created:** [March 6, 2017, 11:43am UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492 "2017-03-06T11:43:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![xon](https://avatars.discourse-cdn.com/v4/letter/x/838e76/32.png) [@xon](https://discuss.elastic.co/u/xon)\
**Post date:** [March 6, 2017, 11:43am UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492/1 "2017-03-06T11:43:51Z")

</div>

I followed 2 different guides and I still havent figure out where the issue is. I want to parse fail2ban logs and I tried the following two configurations unsuccessfully!

Pattern files that I tried

> root@leeds:/etc/logstash/patterns# cat /etc/logstash/patterns/fail2ban  
> F2B\_DATE %{YEAR}-%{MONTHNUM}-%{MONTHDAY}%{HOUR}:?%{MINUTE}(?::?%{SECOND})  
> F2B\_ACTION (\w+).(?:\w+)(\s+)?:  
> F2B\_JAIL [(?\w+-?\w+?)]  
> F2B\_LEVEL (?\w+)\s+

and

> root@leeds:/etc/logstash/patterns# cat /tmp/fail2ban2  
> FAIL2BAN\_BAN %{TIMESTAMP\_ISO8601:timestamp} %{JAVACLASS:criteria}: %{LOGLEVEL:level} [%{WORD:service}] Ban %{IPV4:clientip}  
> FAIL2BAN\_UNBAN %{TIMESTAMP\_ISO8601:timestamp} %{JAVACLASS:criteria}: %{LOGLEVEL:level} [%{WORD:service}] Unban %{IPV4:clientip}  
> FAIL2BAN\_ALREADYBAN %{TIMESTAMP\_ISO8601:timestamp} %{JAVACLASS:criteria}: %{LOGLEVEL:level} [%{WORD:service}] %{IPV4:clientip} already banned

Logstash confs

> file {  
> type =\> "fail2ban"  
> path =\> "/tmp/fail2ban.log"  
> start\_position =\> "beginning"  
> document\_type =\> fail2ban  
> }  
> filter {  
> if [type] == "fail2ban" {  
> grok {  
> patterns\_dir =\> "/etc/logstash/patterns"  
> match =\> [  
> "message", "%{F2B\_DATE:date} %{F2B\_ACTION} %{WORD:level} %{F2B\_JAIL} %{WORD:action} %{IP:ip}",  
> "message", "%{F2B\_DATE:date} %{F2B\_ACTION} %{F2B\_LEVEL} %{GREEDYDATA:msg}?"  
> ]  
> }  
> }  
> }

Output is fine so I am not listing it here

For the second pattern test the relevant part is here

> if [type] == "fail2ban" {  
> grok {  
> patterns\_dir =\> ["/etc/logstash/patterns"]  
> match =\> ["message", "%{FAIL2BAN\_BAN}"]  
> }  
> }

The permissions of the /etc/logstash/pattern are set with chown to logstash user and the above filters lead to a grokparsefailure.

The log looks like this...

> 2016-01-26 05:12:20,778 fail2ban.server [1148]: INFO rollover performed on /var/log/fail2ban.log  
> 2016-01-26 05:12:20,828 fail2ban.actions [1148]: NOTICE [bruteforce3] Ban 255.255.255.255  
> 2016-01-26 05:12:21,048 fail2ban.actions [1148]: NOTICE [bruteforce3] Ban 255.255.255.255  
> 2016-01-26 05:12:21,064 fail2ban.filter [1148]: INFO Log rotation detected for /var/log/fail2ban.log  
> 2016-01-26 05:12:21,064 fail2ban.filter [1148]: INFO Log rotation detected for /var/log/fail2ban.log  
> 2016-01-26 05:12:21,065 fail2ban.filter [1148]: INFO Log rotation detected for /var/log/fail2ban.log  
> 2016-01-26 05:12:21,266 fail2ban.actions [1148]: NOTICE [bruteforce3] Ban 255.255.255.255

Any help?

---

<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:** [March 6, 2017, 12:03pm UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492/2 "2017-03-06T12:03:10Z")

</div>

It doesn't look like you're taking the pid (or whatever it is) into account, i.e. `[1148]` in your example.

---

<div class="post-metadata">

**Author:** ![xon](https://avatars.discourse-cdn.com/v4/letter/x/838e76/32.png) [@xon](https://discuss.elastic.co/u/xon)\
**Post date:** [March 6, 2017, 12:04pm UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492/3 "2017-03-06T12:04:16Z")

</div>

Is there a way to skip it? Is this why im getting the grok failure?

---

<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:** [March 6, 2017, 12:05pm UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492/4 "2017-03-06T12:05:31Z")

</div>

You don't have to keep the pid in a field but you have to include it in your expression, e.g. with `\[%{INT}\]`.

---

<div class="post-metadata">

**Author:** ![xon](https://avatars.discourse-cdn.com/v4/letter/x/838e76/32.png) [@xon](https://discuss.elastic.co/u/xon)\
**Post date:** [March 6, 2017, 12:09pm UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492/5 "2017-03-06T12:09:59Z")

</div>

Didnt work 😕

---

<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:** [March 6, 2017, 12:11pm UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492/6 "2017-03-06T12:11:23Z")

</div>

Then you have to debug more systematically. Start with the simplest possible expression, `%{TIMESTAMP_ISO8601:timestamp}`. Does it work? If yes, add the next part of the expression. Continue until things stop working.

---

<div class="post-metadata">

**Author:** ![zokratez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zokratez/32/5974_2.png) [@zokratez](https://discuss.elastic.co/u/zokratez)\
**Post date:** [March 15, 2017, 9:13pm UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492/7 "2017-03-15T21:13:47Z")

</div>

You can use the grok debugger tool...

[https://grokdebug.herokuapp.com/](https://grokdebug.herokuapp.com/)

---

<div class="post-metadata">

**Author:** ![zokratez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zokratez/32/5974_2.png) [@zokratez](https://discuss.elastic.co/u/zokratez)\
**Post date:** [March 15, 2017, 9:30pm UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492/8 "2017-03-15T21:30:00Z")

</div>

%{TIMESTAMP\_ISO8601:timestamp} %{JAVACLASS:criteria} **[%{NUMBER:pid}]:** %{LOGLEVEL:level} [%{ **NOTSPACE** :service}] Found %{IPV4:clientip}

Explanation:  
1)Take attention to the spaces...  
2)No match for pid was present (That's where the 2 points go.)  
3)Service is not a word (Word is alphanumerical and underscores). Here is a dash, so we use NOTSPACE

---

<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:** [April 12, 2017, 9:30pm UTC](https://discuss.elastic.co/t/grok-parse-failure-with-fail2ban/77492/9 "2017-04-12T21:30:00Z")

</div>

Closed on April 12, 2017, 9:30pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
