Splunk Search

How do I create a derived field using two searches?

SlothB77
Engager

I want to create a derived field using a search string like so:

(host=HostA sourcetype="SourceTypeA" counter="CounterA" avg(ValueA)) /
(host=HostA sourcetype="SourceTypeA" counter="CounterB" avg(ValueB))

This divides the average value when counter=CounterA by the average value when counter=CounterB. I get an error when I plug this search string into the Splunk dynamic search.

I tried this alternative version:

host=Host sourcetype=SourceType counter=CounterA avg(Value) by GroupByField as numerator
counter=CounterB avg(ValueB) by GroupByField as denominator |

eval quotient= numerator/denominator |
table GroupByField, quotient

but no luck.

Tags (2)
0 Karma
1 Solution

renjith_nair
SplunkTrust
SplunkTrust

@SlothB77,
Try,

host=Host sourcetype=SourceType |stats avg(eval(if(counter=CounterA,ValueA,null()))) as numerator,
                                       avg(eval(if(counter=CounterB,ValueB,null()))) as denominator 
                                       by GroupByField 
|eval quotient= numerator/denominator
Happy Splunking!

View solution in original post

renjith_nair
SplunkTrust
SplunkTrust

@SlothB77,
Try,

host=Host sourcetype=SourceType |stats avg(eval(if(counter=CounterA,ValueA,null()))) as numerator,
                                       avg(eval(if(counter=CounterB,ValueB,null()))) as denominator 
                                       by GroupByField 
|eval quotient= numerator/denominator
Happy Splunking!

SlothB77
Engager

This code worked without error when I plugged in my values and it returned exactly what I was looking for. Well done!

0 Karma

horsefez
SplunkTrust
SplunkTrust

Hi @SlothB77,

I just throw a random suggestion out there.

How about:

host=HostA sourcetype="SourceTypeA"
| stats avg(ValueA) AS average_valueA, avg(ValueB) AS average_valueB by counter
| stats values(*) as *
| ratio=average_valueA/average_valueB

Props go to @DalJeanis for help on the last two lines!

0 Karma

horsefez
SplunkTrust
SplunkTrust

Hi @SlothB77,

please provide us some sample data and an expected output with further clarification on what you are expecting to do.

Unfortunately your SPL is just a mess. And I can't figure out what you are trying to do.

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...