# Grok Filter with optional fields fails

**URL:** <https://discuss.elastic.co/t/grok-filter-with-optional-fields-fails/178057>\
**Category:** Logstash\
**Created:** [April 23, 2019, 2:50pm UTC](https://discuss.elastic.co/t/grok-filter-with-optional-fields-fails/178057 "2019-04-23T14:50:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harrytewkesbury](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harrytewkesbury/32/11010_2.png) [@harrytewkesbury](https://discuss.elastic.co/u/harrytewkesbury)\
**Post date:** [April 23, 2019, 2:50pm UTC](https://discuss.elastic.co/t/grok-filter-with-optional-fields-fails/178057/1 "2019-04-23T14:50:07Z")

</div>

Hi,

For IPTables, we log lines similar to this:

`Apr 23 14:32:22 hostname1 kernel: [64719873.568549] IPTABLES-INPUT DROP :IN=br0-PROD OUT= MAC=ff:ff:ff:ff:ff:ff:52:54:00:b2:6c:4e:08:00 SRC=10.3.9.21 DST=10.3.9.255 LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=32436 PROTO=UDP SPT=137 DPT=137 LEN=58`  
`Apr 23 14:37:07 hostname2 kernel: [59804061.226182] IPTABLES-INPUT ACCEPT: IN=ens3 OUT= MAC=01:00:5e:00:00:12:52:54:00:74:ff:22:08:00 SRC=10.3.5.2 DST=224.0.0.18 LEN=40 TOS=0x00 PREC=0xC0 TTL=255 ID=27579 PROTO=112`

You will note that some lines have entries after the PROTO.

The grok filter is:

`IPTABLES ((%{SYSLOGTIMESTAMP:nf_timestamp})\s*(%{HOSTNAME:nf_host})\s*kernel\S+\s*\[.*\]?.*IPTABLES-(?<nf_direction>%{WORD}) (?<nf_result>%{WORD})?.*IN=(%{USERNAME:nf_in_interface})?.*OUT=(%{USERNAME:nf_out_interface})?.*MAC=(%{COMMONMAC:nf_dst_mac}):(%{COMMONMAC:nf_src_mac})?.*SRC=(%{IPV4:nf_src_ip}).*DST=(%{IPV4:nf_dst_ip}).*PROTO=(%{WORD:nf_protocol}).?*SPT=(%{INT:nf_src_port}?.*DPT=%{INT:nf_dst_port}?.*))`

This however fails to match the shorter lines. If I attempt to make these latter fields optional then all lines are grokked, BUT the optional fields are not taken, even for lines where these fields DO exist.

Is there a way - and I am sure there is - for these latter fields be grokked when necessary? Thanks

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [April 23, 2019, 7:21pm UTC](https://discuss.elastic.co/t/grok-filter-with-optional-fields-fails/178057/2 "2019-04-23T19:21:56Z")

</div>

You could grok against both patterns, and use the longer pattern first. The [documentation](https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html#plugins-filters-grok-match) has an example of this.

You should anchor your pattern with ^ if the timestamp begins at the start of a line. It will be more efficient.

---

<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:** [May 21, 2019, 7:27pm UTC](https://discuss.elastic.co/t/grok-filter-with-optional-fields-fails/178057/3 "2019-05-21T19:27:59Z")

</div>

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