Splunk Search

Comparing the results in different time range?

smanojkumar
Contributor

Hi Splunkers!

   Good day!

   I need a search which extracts the count of serial_number of different time range and i should calculate the difference, if its greater than 5000, it should trigger an alert,

 

index="inventory" origin="Inventory:ITSM" earliest=-6h latest=now()
| fields serial_number
| stats count(serial_number) as total_assets
| search [ search index="inventory" origin="Inventory:ITSM" earliest=-12h latest=-6h
| fields serial_number
| stats count(serial_number) as total_assets_prev]
| eval diff=total_assets_prev-total_assets
| where diff>5000
| eval message="Hello Team, the assets in ITSM origin is less than 65000 and its actual value is "
| table message, total_assets_prev, total_assets

 

This query is not working.

 

Thanks in advance!

Manoj Kumar S

 

Labels (1)
0 Karma

isoutamo
SplunkTrust
SplunkTrust

Hi

You could use relative_time function to divide your events like this

index="inventory" origin="Inventory:ITSM" earliest=-12h latest=now()
| eval period = if (_time < relative_time(now(),"-6h"), "previous", "current")
| stats count(search_id) as total_si by period
| eval {period}=total_si
| fields current previous
| stats sum(*) as *
| eval diff = previous - current
| where diff > 5000
| eval message="Hello Team, the assets in ITSM origin is less than 65000 and its actual value is "
| table message, previous current

There are also other ways to do it.

r. Ismo 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
index="inventory" origin="Inventory:ITSM" earliest=-12h latest=now()
| fields serial_number
| stats count(eval(_time<relative_time(now(),"-6h"))) as total_assets_prev count(eval(_time>relative_time(now(),"-6h"))) as total_assets
| eval diff=total_assets_prev-total_assets
| where diff>5000
| eval message="Hello Team, the assets in ITSM origin is less than 65000 and its actual value is "
| table message, total_assets_prev, total_assets
0 Karma

smanojkumar
Contributor

Hi @ITWhisperer ,

   Thanks for your response!

    I'm getting total_assets_prev as 0,

    (My requirement is calculate the count of serial_number of time range -12 hr to -6hr and count of serial_number for the time range -6hr to now and calculate the difference)

Thanks in advance!

Manoj Kumar S

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

 

Remove this line

| fields serial_number

 

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...

Skip the Awkward Silence: Have a .conf-ersation at .conf26

Picture this. You arrive at .conf26 already having your socializing and networking plans mapped out. No ...

Rethinking Zero Trust: From Product Purchases to Logical Control Evidence

Implementing Zero Trust (ZT) across complex environments often falters at the very beginning due to a ...