# Using Grok with KV filter

**URL:** <https://discuss.elastic.co/t/using-grok-with-kv-filter/170136>\
**Category:** Logstash\
**Created:** [February 27, 2019, 10:11am UTC](https://discuss.elastic.co/t/using-grok-with-kv-filter/170136 "2019-02-27T10:11:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![spadhi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spadhi/32/45258_2.png) [@spadhi](https://discuss.elastic.co/u/spadhi)\
**Post date:** [February 27, 2019, 10:11am UTC](https://discuss.elastic.co/t/using-grok-with-kv-filter/170136/1 "2019-02-27T10:11:55Z")

</div>

Hi, I am pretty new to ELK stack. Currently I am trying to parse my application log using grok pattern. But since my logs are not structured i may have to write too many grok conditions, which will not scale well.  
Sample log:  
2019-02-25 10:22:27,832 LL="INFO" field1="field value" field2="field2val" MTHD="POST" O="ipadd" PAYLOAD="{"errorResponse":{"status":403,"message":"some message.","reason":"some reason"}}"  
using KV filter I cant parse the above log, as the payload is not parsable using KV. (Correct me if I am wrong).  
So I am planning to use both grok and KV, using grok I will get the time and get the payload, and remaining part will use KV filter?  
Can you please help me here?  
I am trying the below, but I am unable to use KV filter

```
grok {
  		match => { "message" => "%{TIMESTAMP_ISO8601:date}\s+%{GREEDYDATA:msgbody}"}
}

 grok { 	
	 	match => {
 		   "msgbody" => ["(?m).*%{GREEDYDATA:KV}\s+PAYLOAD=%{GREEDYDATA:PAYLOAD}"]
	  	}
   }
   
   kv {
       source => "KV"
       field_split => " "
       value_split => "="
       remove_char_key => "<>\[\],"
       remove_char_value => "<>\[\],"
       trim_key => "<>\[\],"
       trim_value => "<>\[\],"
       include_brackets => false

   }
```

---

<div class="post-metadata">

**Author:** ![NerdSec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nerdsec/32/22056_2.png) [@NerdSec](https://discuss.elastic.co/u/NerdSec)\
**Post date:** [February 27, 2019, 11:06am UTC](https://discuss.elastic.co/t/using-grok-with-kv-filter/170136/2 "2019-02-27T11:06:35Z")

</div>

Hi Satya,

Your KV is not using the field parsed by the grok filter. Here is the grok filter output:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/0/b0cb69e17c5c109046fa3d80b60a42985a1de498.png)

Either rename the field in GROK to **KV** :

```auto
%{TIMESTAMP_ISO8601:date}\s+%{GREEDYDATA:KV}

```

Or you change the source field option in KV to **msgbody** :

```auto
kv {
       source => "msgbody"
       field_split => " "
       value_split => "="
   }

```

---

<div class="post-metadata">

**Author:** ![spadhi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spadhi/32/45258_2.png) [@spadhi](https://discuss.elastic.co/u/spadhi)\
**Post date:** [February 27, 2019, 11:15am UTC](https://discuss.elastic.co/t/using-grok-with-kv-filter/170136/3 "2019-02-27T11:15:32Z")

</div>

Thanks Nachiket for a quick reply.  
But as I mentioned there is a PAYLOAD field which is a json. I want to save the payload as is in Elastic, if i parse the payload using KV, then it does not work..

---

<div class="post-metadata">

**Author:** ![NerdSec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nerdsec/32/22056_2.png) [@NerdSec](https://discuss.elastic.co/u/NerdSec)\
**Post date:** [February 27, 2019, 11:19am UTC](https://discuss.elastic.co/t/using-grok-with-kv-filter/170136/4 "2019-02-27T11:19:08Z")

</div>

Then use the json filter for the PAYLOAD field.

```auto
 json {
    source => "PAYLOAD"
    target => "payload_parsed"
  }

```

---

<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:** [February 27, 2019, 11:20am UTC](https://discuss.elastic.co/t/using-grok-with-kv-filter/170136/5 "2019-02-27T11:20:23Z")

</div>

Change your second grok to

```
"msgbody" => ["%{GREEDYDATA:KV}\s+PAYLOAD=%{GREEDYDATA:PAYLOAD}"]
```

---

<div class="post-metadata">

**Author:** ![spadhi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spadhi/32/45258_2.png) [@spadhi](https://discuss.elastic.co/u/spadhi)\
**Post date:** [February 27, 2019, 11:23am UTC](https://discuss.elastic.co/t/using-grok-with-kv-filter/170136/6 "2019-02-27T11:23:34Z")

</div>

> [@Badger](#):
>
> GREEDYDATA:KV

Awesome...this is what I was looking for, I have added .\* before %{GREEDYDATA:KV}, thats a mistake.  
Thanks a lot Badger

---

<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:** [March 27, 2019, 11:33am UTC](https://discuss.elastic.co/t/using-grok-with-kv-filter/170136/7 "2019-03-27T11:33:27Z")

</div>

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