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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

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

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...