Splunk Search

How to extract and aggregate by extracted fields

ashriram
Engager

Hi 

I have the data that looks like this
user, ip, (metrics kv pairs)

---- sample results for search -- 
user=user1,ip=10.10.10.10,key1=10,key2=30
user=user2,ip=10.10.10.10,key1=5,key3=30
user=user1,ip=10.10.10.12,key2=10,key3=30,key4=2,key5=14,key6=4
user=user1,ip=10.10.10.10,key5=22
-------------

How do I pull out the metrics - key1- key6 and aggregate by the metrics ?
say if i wanted a pie chart with all totals of all the keys for a given IP/ user (say IP and username are dashboard input tokens)

Labels (1)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@ashriram 

Can you please try this?

YOUR_SEARCH
| rex field=_raw "user=(?<user>[^,]*),ip=(?<ip>[^,]*),(?<keys>[^\n]*)"
| rex field=keys "key\d=(?<k>[^\n|,]*)" max_match=0
| stats sum(k) as key_sum by ip user

 

you can changes as per your requirement like

| stats sum(k) as key_sum by ip

 

| stats sum(k) as key_sum by user

 

My Sample Search :

| makeresults | eval raw="user=user1,ip=10.10.10.10,key1=10,key2=30|user=user2,ip=10.10.10.10,key1=5,key3=30|user=user1,ip=10.10.10.12,key2=10,key3=30,key4=2,key5=14,key6=4|user=user1,ip=10.10.10.10,key5=22", raw=split(raw,"|")|mvexpand raw|rename raw as _raw
| rex field=_raw "user=(?<user>[^,]*),ip=(?<ip>[^,]*),(?<keys>[^\n]*)"
| rex field=keys "key\d=(?<k>[^\n|,]*)" max_match=0
| stats sum(k) as key_sum by ip user

 

Thanks
KV
▄︻̷̿┻̿═━一

If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

View solution in original post

kamlesh_vaghela
SplunkTrust
SplunkTrust

@ashriram 

Can you please try this?

YOUR_SEARCH
| rex field=_raw "user=(?<user>[^,]*),ip=(?<ip>[^,]*),(?<keys>[^\n]*)"
| rex field=keys "key\d=(?<k>[^\n|,]*)" max_match=0
| stats sum(k) as key_sum by ip user

 

you can changes as per your requirement like

| stats sum(k) as key_sum by ip

 

| stats sum(k) as key_sum by user

 

My Sample Search :

| makeresults | eval raw="user=user1,ip=10.10.10.10,key1=10,key2=30|user=user2,ip=10.10.10.10,key1=5,key3=30|user=user1,ip=10.10.10.12,key2=10,key3=30,key4=2,key5=14,key6=4|user=user1,ip=10.10.10.10,key5=22", raw=split(raw,"|")|mvexpand raw|rename raw as _raw
| rex field=_raw "user=(?<user>[^,]*),ip=(?<ip>[^,]*),(?<keys>[^\n]*)"
| rex field=keys "key\d=(?<k>[^\n|,]*)" max_match=0
| stats sum(k) as key_sum by ip user

 

Thanks
KV
▄︻̷̿┻̿═━一

If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

ashriram
Engager

This helped me solve my use case

Had to make a few tweaks, because my keys were Not in the format `key{number}`

`max_match=0` is what really solved it for me

this is how my solution looked: 

SEARCH
| rex field=_raw "user=(?<user>[^,]*),ip=(?<ip>[^,]*),(?<metrics>[^\"]+)""
| rex field=metrics "(?<metric>\w+)=(?<m_count>\d+),?" max_match=0 
| stats sum(m_count) as total_counts by metric, ip user

 

thanks a lot @kamlesh_vaghela 

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

Cool.

Glad to help you. Please accept the answer to close this question.

KV

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| extract
| stats sum(*) as * by ip user
| eval userip=user.":".ip
| fields - ip user
| table userip *
0 Karma
Get Updates on the Splunk Community!

Automatic Discovery Part 1: What is Automatic Discovery in Splunk Observability Cloud ...

If you’ve ever deployed a new database cluster, spun up a caching layer, or added a load balancer, you know it ...

Real-Time Fraud Detection: How Splunk Dashboards Protect Financial Institutions

Financial fraud isn't slowing down. If anything, it's getting more sophisticated. Account takeovers, credit ...

Splunk + ThousandEyes: Correlate frontend, app, and network data to troubleshoot ...

 Are you tired of troubleshooting delays caused by siloed frontend, application, and network data? We've got a ...