Security

Compare results from 2 searches

RahulMisra1
Explorer

Hi, 

 

I am running a search to get count of IP';s from yesterday & last month.

index=<> source="/****" IP!="10.*" [| inputlookup ip_tracking.csv | rename MIDS AS MID | format ] earliest=-30d@d latest=now| eval ReportKey="Last30Day"|append [search index=<> source=""/****"" IP!="10.*" [| inputlookup ip_tracking.csv | rename MIDS AS MID | format ] earliest=-1d@d latest=@d | eval ReportKey="yesterday"]| eval Day=if(_time<=relative_time(now(),"-30d@d"),"yesterday","Last30Day")
| stats count(eval(Day="yesterday")) AS yesterday count(eval(Day="Last30Day")) AS Last30Day BY IP

 

 

This search is giving me all results in Month but not in yesterday. Can you help me in correcting the query

RahulMisra1_0-1718112201454.png

 

0 Karma
1 Solution

bowesmana
SplunkTrust
SplunkTrust

@RahulMisra1 It looks like you have double accounting - you are counting yesterday twice because your first search is -30 days to NOW, which includes yesterday and then you append yesterday's data, so you will effectively count yesterday twice.

You don't actually need the append as you already have the data from the first search to calculate yesterday.

One question though - is your last 30 days supposed to include yesterday - anyway this search should work

index=<> source="/****" IP!="10.*" earliest=-30d@d latest=now 
[
  | inputlookup ip_tracking.csv 
  | rename MIDS AS MID 
  | format 
] 
``` If the data time is -1d to end of day, then the data is yesterday ```
| eval Day=if(_time>=relative_time(now(),"-1d@d") AND _time<=relative_time(now(),"@d"),"yesterday","Last30Day") 
``` Now count yesterday, but then count last 30 days as ALL items, which 
    includes yesterday ```
| stats count(eval(Day="yesterday")) AS yesterday count AS Last30Day BY IP

The final count as last30Day will just count all items in the search including yesterday - if you want that to exclude yesterday BUT also include today up to now, then use your original eval statement in the count.

 

View solution in original post

0 Karma

bowesmana
SplunkTrust
SplunkTrust

@RahulMisra1 It looks like you have double accounting - you are counting yesterday twice because your first search is -30 days to NOW, which includes yesterday and then you append yesterday's data, so you will effectively count yesterday twice.

You don't actually need the append as you already have the data from the first search to calculate yesterday.

One question though - is your last 30 days supposed to include yesterday - anyway this search should work

index=<> source="/****" IP!="10.*" earliest=-30d@d latest=now 
[
  | inputlookup ip_tracking.csv 
  | rename MIDS AS MID 
  | format 
] 
``` If the data time is -1d to end of day, then the data is yesterday ```
| eval Day=if(_time>=relative_time(now(),"-1d@d") AND _time<=relative_time(now(),"@d"),"yesterday","Last30Day") 
``` Now count yesterday, but then count last 30 days as ALL items, which 
    includes yesterday ```
| stats count(eval(Day="yesterday")) AS yesterday count AS Last30Day BY IP

The final count as last30Day will just count all items in the search including yesterday - if you want that to exclude yesterday BUT also include today up to now, then use your original eval statement in the count.

 

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Double-check the calculation for the Day field.  Events less than a day old will have *greater* timestamps than older events.

eval Day=if(_time>=relative_time(now(),"-1d@d"),"yesterday","Last30Day")
---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Accelerating Observability as Code with the Splunk AI Assistant

We’ve seen in previous posts what Observability as Code (OaC) is and how it’s now essential for managing ...

Integrating Splunk Search API and Quarto to Create Reproducible Investigation ...

 Splunk is More Than Just the Web Console For Digital Forensics and Incident Response (DFIR) practitioners, ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...