# Grok check if exists

**URL:** <https://discuss.elastic.co/t/grok-check-if-exists/232433>\
**Category:** Logstash\
**Created:** [May 13, 2020, 12:34pm UTC](https://discuss.elastic.co/t/grok-check-if-exists/232433 "2020-05-13T12:34:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohammad\_Mousavi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohammad_mousavi/32/56508_2.png) [@Mohammad\_Mousavi](https://discuss.elastic.co/u/Mohammad_Mousavi)\
**Post date:** [May 13, 2020, 12:34pm UTC](https://discuss.elastic.co/t/grok-check-if-exists/232433/1 "2020-05-13T12:34:51Z")

</div>

Hi, I have 2 type of nginx logs:

> x.x.x.x - - [13/May/2020:16:42:27 +0430] "GET /cache/68/fb/68fb0ff0cf190dc7fcc567e76e1e4c2b.jpg HTTP/1.1" 200 79059

> x.x.x.x - - [13/May/2020:16:40:06 +0430] "GET /dl-apk/1585150257\_vhTIEU0sjQ.apk?st=hqWJPpX\_LbM6J5qEiW0U4w&e=1589385638&product\_id=184623&package\_name=com.mypackage&user\_id=3693549 HTTP/1.1" 200 67244157

This in my grok:

`%{IPV4:clientip} - - \[%{HTTPDATE:timestamp}\] \"%{WORD:verb} %{URIPATH:request}.*(?:product_id=%{INT:product_id}).*(?:package_name=%{DATA:package_name}&).*(?:user_id=%{INT:user_id}) HTTP/%{NUMBER:httpversion}\" %{NUMBER:response} %{NUMBER:bytes_sent}`

It's valid for second log type but not for the first one.  
I guess I could make a if exist condition with `(?:)` (if product\_id exist or does not) but I don't know how.  
I see `(?:%{INT:number}|-)` means number or - character, but what if does not exists ?  
Thanx a lot

---

<div class="post-metadata">

**Author:** ![ptamba](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@ptamba](https://discuss.elastic.co/u/ptamba)\
**Post date:** [May 13, 2020, 1:12pm UTC](https://discuss.elastic.co/t/grok-check-if-exists/232433/2 "2020-05-13T13:12:36Z")

</div>

you can use array of patterns to match

further info in the [docs](https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html#plugins-filters-grok-match)

---

<div class="post-metadata">

**Author:** ![Mohammad\_Mousavi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohammad_mousavi/32/56508_2.png) [@Mohammad\_Mousavi](https://discuss.elastic.co/u/Mohammad_Mousavi)\
**Post date:** [May 14, 2020, 6:50am UTC](https://discuss.elastic.co/t/grok-check-if-exists/232433/3 "2020-05-14T06:50:14Z")

</div>

I did it as you suggested:

```
filter {
                grok { match => ["message" , "%{IPV4:clientip} - - \[%{HTTPDATE:timestamp}\] \"%{WORD:verb} %{URIPATH:request}.*(?:product_id=%{INT:product_id}).*(?:package_name=%{DATA:package_name}&).*(?:user_id=%{INT:user_id}) HTTP/%{NUMBER:httpversion}\" %{NUMBER:response} %{NUMBER:bytes_sent}"] }
                grok { match => ["message" , "%{IPV4:clientip} - - \[%{HTTPDATE:timestamp}\] \"%{WORD:verb} %{URIPATH:request} HTTP/%{NUMBER:httpversion}\" %{NUMBER:response} %{NUMBER:bytes_sent}"] }
                geoip {
                        source => "clientip"
                        target => "geoip"
                }
                mutate {                                                                                                                                 
                      convert => ["response", "integer"]
                      convert => ["bytes_sent", "integer"]
                }
                }
       }

```

but for both matches logstash add `_grokparsefailure` to tags.  
In general it works. thanx.

---

<div class="post-metadata">

**Author:** ![ptamba](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@ptamba](https://discuss.elastic.co/u/ptamba)\
**Post date:** [May 14, 2020, 8:53am UTC](https://discuss.elastic.co/t/grok-check-if-exists/232433/4 "2020-05-14T08:53:48Z")

</div>

you’re running two grok, that’s why you have parse failure. it will match first then have parse failure on the next and vice versa.

you could do

```
      grok {
        match => {
          "message" => [
            "pattern1",
            "pattern2"
          ]
        }
      }

```

however, be careful if you have double quotes in your pattern then you need you wrap the pattern in a single quote. in general, don’t mix them

---

<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:** [June 11, 2020, 8:53am UTC](https://discuss.elastic.co/t/grok-check-if-exists/232433/5 "2020-06-11T08:53:54Z")

</div>

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