Splunk Search

How to Summarize on distinct value?

hvdtol
Path Finder

Hello there,

I would like some help with my query.

I want to summarize 2 fields into 2 new columns

One field is unique, but the other is not
The field fhost is not unique.

I want the sum of field "cores" by unique combination of the columns    "clname" and  "fhost"

I am struggle how to do this properly and how i can use the sum unique for column "fhost"

| makeresults
| eval clname="clusterx", fhost="f-hosta", vhost="v-hosta",cores=2,cpu=1
| append [| makeresults | eval clname="clusterx", fhost="f-hosta", vhost="v-hostb" ,cores=2,cpu=1 ]
| append [| makeresults | eval clname="clusterx", fhost="f-hostb", vhost="v-hostc" ,cores=4,cpu=1 ]
| append [| makeresults | eval clname="clusterx", fhost="f-hostc", vhost="v-hostd" ,cores=6,cpu=1 ]

| eventstats sum(cpu) as total_vhost_cpus by clname

``` This is not working ```
| eventstats sum(cores) as total_fhost_cores by clname fhost

`` The output should be in table format ```

| table clname cores cpu fhost vhost  total_vhost_cpus  total_fhost_cores

Thank you in advance.

Harry

Labels (1)
Tags (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Assuming cores relates to fhosts and cpus relates to vhosts, your data has mixed where these counts are coming from, so you need to split them out. Try something like this

| makeresults
| eval clname="clusterx", fhost="f-hosta", vhost="v-hosta",cores=2,cpu=1
| append [| makeresults | eval clname="clusterx", fhost="f-hosta", vhost="v-hostb" ,cores=2,cpu=1 ]
| append [| makeresults | eval clname="clusterx", fhost="f-hostb", vhost="v-hostc" ,cores=4,cpu=1 ]
| append [| makeresults | eval clname="clusterx", fhost="f-hostc", vhost="v-hostd" ,cores=6,cpu=1 ]
| eval fhost-{fhost}=cores
| eventstats values(fhost-*) as fhost-cores-* by clname
| eval total_fhost_cores=0
| foreach fhost-cores-*
    [| eval total_fhost_cores=total_fhost_cores + '<<FIELD>>']
| fields - fhost-*
| eventstats sum(cpu) as total_vhost_cpus by clname
| table clname cores cpu fhost vhost  total_vhost_cpus  total_fhost_cores

btw, unless you are working in base 12, 2+4+6=12 not 10!

View solution in original post

0 Karma

yuanliu
SplunkTrust
SplunkTrust

It would help if you describe what "this is not working" actually means.  What is the result you get and what is the result you expect?  What is the logic between your data and your expected result?

Using your sample data and your sample stats, this is the table

clnamecorescpufhostvhosttotal_vhost_cpustotal_fhost_cores
clusterx21f-hostav-hosta44
clusterx21f-hostav-hostb44
clusterx41f-hostbv-hostc44
clusterx61f-hostcv-hostd46

Can you explain why this not what you expect?  What is the problem you are trying to solve using two eventstats command with raw events, not a stats?

0 Karma

hvdtol
Path Finder

Sorry, i was not clear. I am trying to get a sum of unique fhost by cluster.

So the outcome should be
f-hosta cores=2 f-hostb cores=4 f-hostc cores=6

2+4+6=10 in field "total_fhost_cores"

Regards, Harry

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Assuming cores relates to fhosts and cpus relates to vhosts, your data has mixed where these counts are coming from, so you need to split them out. Try something like this

| makeresults
| eval clname="clusterx", fhost="f-hosta", vhost="v-hosta",cores=2,cpu=1
| append [| makeresults | eval clname="clusterx", fhost="f-hosta", vhost="v-hostb" ,cores=2,cpu=1 ]
| append [| makeresults | eval clname="clusterx", fhost="f-hostb", vhost="v-hostc" ,cores=4,cpu=1 ]
| append [| makeresults | eval clname="clusterx", fhost="f-hostc", vhost="v-hostd" ,cores=6,cpu=1 ]
| eval fhost-{fhost}=cores
| eventstats values(fhost-*) as fhost-cores-* by clname
| eval total_fhost_cores=0
| foreach fhost-cores-*
    [| eval total_fhost_cores=total_fhost_cores + '<<FIELD>>']
| fields - fhost-*
| eventstats sum(cpu) as total_vhost_cpus by clname
| table clname cores cpu fhost vhost  total_vhost_cpus  total_fhost_cores

btw, unless you are working in base 12, 2+4+6=12 not 10!

0 Karma

hvdtol
Path Finder

Of course is should be 12.  I am ashamed.😏

But thank you very much

The | eval fhost-{fhost}=cores is a very nice solution
I did not know thos was possible.

Thank you for your help

Regards,

Harry

0 Karma
Get Updates on the Splunk Community!

Index This | I’m short for "configuration file.” What am I?

May 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with a Special ...

New Articles from Academic Learning Partners, Help Expand Lantern’s Use Case Library, ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Your Guide to SPL2 at .conf24!

So, you’re headed to .conf24? You’re in for a good time. Las Vegas weather is just *chef’s kiss* beautiful in ...