Splunk Search

How to calculate difference between field values in two events?

vikas_sood
Explorer

Hi,

i have 2 events with 3 fields: timestamp , servername, cpu_usage:

22-Mar-2022 00:00:00, server1 ,18

23-Mar-2022, 00:01:00 server1 , 82

22-Mar-2022 00:00:00, server2 ,78

23-Mar-2022, 00:01:00 server2 , 14

I want to calculate difference between 2nd and 1st event for each server. Can you please suggest, how this can be done?

Labels (4)
Tags (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| stats range(cpu_usage) as difference by servername
0 Karma

vikas_sood
Explorer

That works, is it possible to add + and - in difference?

Tags (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| stats first(cpu_usage) as first_value last(cpu_usage) as last_value by servername
| eval difference=last_value-first_value
| eval difference=if(difference>0,"+".difference,difference)
0 Karma
Get Updates on the Splunk Community!

Enterprise Security Content Update (ESCU) | New Releases

In the last month, the Splunk Threat Research Team (STRT) has had 2 releases of new security content via the ...

Announcing the 1st Round Champion’s Tribute Winners of the Great Resilience Quest

We are happy to announce the 20 lucky questers who are selected to be the first round of Champion's Tribute ...

We’ve Got Education Validation!

Are you feeling it? All the career-boosting benefits of up-skilling with Splunk? It’s not just a feeling, it's ...