Splunk Search

How to merge and group multiple key-value pairs, count the values, and table the results?

mkrauss1
Explorer

I try hard to group multiple key/values from a single record, then count the values and print them in a table.
Say i have this:

EC1=1 ES1=-100 EC2=1 ES2=-150
EC1=1 ES1=-100 EC2=1 ES2=-150
EC1=2 ES1=-100 EC2=2 ES2=-150
EC1=2 ES1=-100 EC2=3 ES2=-150
EC1=1 ES1=-200 EC2=1 ES2=-250
EC1=2 ES1=-200 EC2=2 ES2=-250
EC1=2 ES1=-200 EC2=2 ES2=-250
EC1=2 ES1=-200 EC2=3 ES2=-250

Means:

ES1/2 are status codes

EC2/1 are counter values

Now i'd like to group them into summaries, ES (ErrorStatus) and EC (Error Count).
Trying to get:

Status Count
-100&nbsp&nbsp&nbsp&nbsp&nbsp6
-150&nbsp&nbsp&nbsp&nbsp&nbsp7
-200&nbsp&nbsp&nbsp&nbsp&nbsp6
-250&nbsp&nbsp&nbsp&nbsp&nbsp7

I'd try to start with the stats and count function

| stats count as Total count(ES1) as ESS1 count sum(EC1) as ECC1 count(ES2) as ESS2 count sum(EC2) as ECC2 by ES1,ES2

But that doesnt take me any further, any ideas?

1 Solution

somesoni2
Revered Legend

Try this

 you base search | rex max_match=0 "EC\d+=(?<EC>[^ ]+)" | rex max_match=0 "ES\d+=(?<Status>[^ ]+)" | table EC Status | eval temp=mvzip(EC,Status ,"#") | table temp | mvexpand temp | rex field=temp "(?<EC>.*)#(?<Status>.*)" | fields - temp | stats sum(EC) as Count by Status

View solution in original post

somesoni2
Revered Legend

Try this

 you base search | rex max_match=0 "EC\d+=(?<EC>[^ ]+)" | rex max_match=0 "ES\d+=(?<Status>[^ ]+)" | table EC Status | eval temp=mvzip(EC,Status ,"#") | table temp | mvexpand temp | rex field=temp "(?<EC>.*)#(?<Status>.*)" | fields - temp | stats sum(EC) as Count by Status

mkrauss1
Explorer

Wow, that's a tough query, thanks for this!

0 Karma

ShaneNewman
Motivator

The easiest way to do this is with a subsearch:

index=your_index sourcetype=your_sourcetype | stats sum(EC1) as count by ES1 | rename ES1 AS Status | append [ search index=your_index sourcetype=your_sourcetype | stats sum(EC2) as count by ES2 | rename ES2 AS Status]
0 Karma

mkrauss1
Explorer

Thanks for this. ES2=-150 and -250 is missing, how i can i group them too?

0 Karma

ShaneNewman
Motivator

That is strange, they are showing up in my search... Do you see ES2 values being extracted on the right hand side under interesting fields?

0 Karma
Get Updates on the Splunk Community!

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...

Updated Team Landing Page in Splunk Observability

We’re making some changes to the team landing page in Splunk Observability, based on your feedback. The ...

New! Splunk Observability Search Enhancements for Splunk APM Services/Traces and ...

Regardless of where you are in Splunk Observability, you can search for relevant APM targets including service ...