# How to improve below Logstash grok filters?

**URL:** <https://discuss.elastic.co/t/how-to-improve-below-logstash-grok-filters/295212>\
**Category:** Logstash\
**Created:** [January 24, 2022, 10:17am UTC](https://discuss.elastic.co/t/how-to-improve-below-logstash-grok-filters/295212 "2022-01-24T10:17:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alytkowski](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@alytkowski](https://discuss.elastic.co/u/alytkowski)\
**Post date:** [January 24, 2022, 10:17am UTC](https://discuss.elastic.co/t/how-to-improve-below-logstash-grok-filters/295212/1 "2022-01-24T10:17:34Z")

</div>

Hi there. I have a problem with Logstash consuming most of the CPU resources all the time and events not being parsed in real time. Through Stack Monitoring in Kibana, I found that for one set of filters for a certain source, the performance is low, hence causing the problem.  
The filters have been written for Tibco BW 5.13 logs. Can anyone advise what can I do with below filters, what to change, what to avoid etc. to improve performance?

```auto
    grok {
      match => {
        "message" => [
          "(?<TMSTMP>%{YEAR} %{MONTH} %{MONTHDAY} %{TIME} %{WORD:TMZ} %{INT:TMZ})%{SPACE}BW.%{NOTSPACE:APPLICATION}%{SPACE}%{NOTSPACE:USER}%{SPACE}\[%{NOTSPACE:BW_USER}]%{SPACE}%{NOTSPACE}%{SPACE}\[%{DATESTAMP}]%{SPACE}%{NOTSPACE}%{SPACE}\[%{LOGLEVEL:LOG_LEVEL}]\[%{NOTSPACE:BW_PROCESS}]%{SPACE}\[%{JAVAFILE:PROCESS_ID}]\[%{JAVAFILE:EAR_NAME}]\[%{HOSTNAME}]%{GREEDYDATA:MSG}",
          "(?<TMSTMP>%{YEAR} %{MONTH} %{MONTHDAY} %{TIME} %{WORD:TMZ} %{INT:TMZ})%{SPACE}BW.%{NOTSPACE:APPLICATION}%{SPACE}%{NOTSPACE:USER}%{SPACE}\[%{NOTSPACE:BW_USER}]%{SPACE}%{NOTSPACE}%{SPACE}\[%{DATESTAMP}]%{SPACE}%{NOTSPACE}%{SPACE}\[%{LOGLEVEL:LOG_LEVEL}]\[%{NOTSPACE:BW_PROCESS}%{SPACE}%{NOTSPACE:BW_PROCESS}]%{SPACE}\[%{JAVAFILE:PROCESS_ID}]\[%{JAVAFILE:EAR_NAME}]\[%{HOSTNAME}]%{GREEDYDATA:MSG}",
          "(?<TMSTMP>%{YEAR} %{MONTH} %{MONTHDAY} %{TIME} %{WORD:TMZ} %{INT:TMZ})%{SPACE}BW.%{NOTSPACE:APPLICATION}%{SPACE}%{NOTSPACE:USER}%{SPACE}\[%{NOTSPACE:BW_USER}]%{SPACE}%{NOTSPACE}%{SPACE}%{NOTSPACE:JOB_ID}%{SPACE}\[%{NOTSPACE:BW_PROCESS}]:%{SPACE}\[%{DATA}]%{SPACE}\[%{NOTSPACE:JOB}]%{SPACE}\[%{LOGLEVEL:LOG_LEVEL}]%{GREEDYDATA:MSG}",
          "(?<TMSTMP>%{YEAR} %{MONTH} %{MONTHDAY} %{TIME} %{WORD:TMZ} %{INT:TMZ})%{SPACE}BW.%{NOTSPACE:APPLICATION}%{SPACE}%{NOTSPACE:USER}%{SPACE}\[%{NOTSPACE:BW_USER}]%{SPACE}%{NOTSPACE}%{SPACE}%{NOTSPACE:JOB}%{SPACE}\[%{DATA}]:%{SPACE}\[%{DATESTAMP}]%{SPACE}\[%{NOTSPACE}]%{SPACE}\[%{LOGLEVEL:LOG_LEVEL}]%{NOTSPACE:BW_PROCESS}%{SPACE}\[%{NOTSPACE}]%{GREEDYDATA:MSG}",
          "(?<TMSTMP>%{YEAR} %{MONTH} %{MONTHDAY} %{TIME} %{WORD:TMZ} %{INT:TMZ})%{SPACE}BW.%{NOTSPACE:APPLICATION}%{SPACE}%{LOGLEVEL:LOG_LEVEL}%{SPACE}\[%{USERNAME:BWUSER}]%{SPACE}%{GREEDYDATA:MSG}",
          "(?<TMSTMP>%{YEAR} %{MONTH} %{MONTHDAY} %{TIME} %{WORD:TMZ} %{INT:TMZ})%{SPACE}BW.%{NOTSPACE:APPLICATION}%{SPACE}%{NOTSPACE}%{SPACE}%{NOTSPACE:BWUSER}%{SPACE}%{NOTSPACE}%{SPACE}%{NOTSPACE:JOB}%{SPACE}%{NOTSPACE}:%{SPACE}%{SYSLOG5424SD:LOG_TMSTMP}%{SPACE}%{NOTSPACE}%{SPACE}\[%{LOGLEVEL:LOG_LEVEL}]\[%{NOTSPACE:BW_PROCESS}]%{SPACE}\[%{JAVAFILE:PROCESS_ID}]\[%{JAVAFILE:EAR_NAME}]\[%{HOSTNAME}]%{GREEDYDATA:MSG}",
          "(?<TMSTMP>%{YEAR} %{MONTH} %{MONTHDAY} %{TIME} %{WORD:TMZ} %{INT:TMZ}) %{GREEDYDATA:MSG}",
          "%{GREEDYDATA:MSG_NOT_FILTERED}"
        ]

```

---

<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:** [January 24, 2022, 3:09pm UTC](https://discuss.elastic.co/t/how-to-improve-below-logstash-grok-filters/295212/2 "2022-01-24T15:09:54Z")

</div>

If your messages start with TMSTMP then anchor them using ^. This makes a failure to match much cheaper. Read [this](https://www.elastic.co/blog/do-you-grok-grok) blog post.

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [January 24, 2022, 3:27pm UTC](https://discuss.elastic.co/t/how-to-improve-below-logstash-grok-filters/295212/3 "2022-01-24T15:27:51Z")

</div>

Besides the anchoring, putting the most matched patternss first will help to reduce the CPU usage.

You could also see if you can replace your grok with a couple of dissect filters and conditionals, this can reduce the CPU usage even more.

---

<div class="post-metadata">

**Author:** ![alytkowski](https://avatars.discourse-cdn.com/v4/letter/a/cc9497/32.png) [@alytkowski](https://discuss.elastic.co/u/alytkowski)\
**Post date:** [January 24, 2022, 5:55pm UTC](https://discuss.elastic.co/t/how-to-improve-below-logstash-grok-filters/295212/4 "2022-01-24T17:55:59Z")

</div>

I've experimented with anchors before and while the performance was the same, it also caused many logs not to much without any other change to the filters so I decided not to use them. The blog also shows tiered matching strategy, I'm going to try that. Thanks for your reply!

---

<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:** [February 21, 2022, 5:56pm UTC](https://discuss.elastic.co/t/how-to-improve-below-logstash-grok-filters/295212/5 "2022-02-21T17:56:25Z")

</div>

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