Splunk Search

Today date and previous day data

LogUx
Motivator

I want to calculate todays date data and previous day data from the host. Please suggest SPL for this.

Labels (1)
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi @LogUx,

if in yyour search you can use only the index time fields (index, sourcetyoe, source and host) you can use "|metasearch", having someting like this:

| metasearch index=_internal earliest=-d@d latest=now
| eval date_year=strftime(_time,"%Y"), date_month=strftime(_time,"%B"),date_mday=strftime(_time,"%d")
| eval now_date_year=strftime(now(),"%Y"), now_date_month=strftime(now(),"%B"),now_date_mday=strftime(now(),"%d")
| eval day=if(date_year=now_date_year AND date_month=now_date_month AND date_mday=now_date_mday,"Today","Previous")
| stats count BY day
| delta count AS diff
| reverse
| table diff
| head 1

Ciao.

 Giuseppe

View solution in original post

inventsekar
SplunkTrust
SplunkTrust

hi @LogUx just curious - earliest and latest variables are ok or not ok?

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @inventsekar,

I tried on my Splunk and correctly worked.

Ciao.

Giuseppe

isoutamo
SplunkTrust
SplunkTrust
At least in some older versions of splunk only official way was use all time as search period with metasearch. With other periods result was or wasn’t exactly correct. I’m not sure if this UFs still valid or not. Unfortunately I couldn’t find where I have gotten this information and could evaluate it now.
0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @LogUx,

see if this example solves your need:

index=_internal
| eval day=if(date_year=strftime(now(),"%Y") AND date_month=lower(strftime(now(),"%B")) AND date_mday=strftime(now(),"%d"),"Today","Previous")
| stats count BY day

Ciao.

Giuseppe

LogUx
Motivator

@gcusello  I want the difference between pervious day count of the host and current day count of the host. I want comparison panel in my dashboard

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @LogUx,

as I said, I don't know in deep your need, but you can use my approach.

so using a search like mine you can display the count of events of today and previous day so you can compare the two numbers.

If you want more help, you should share more infos, e.g.:

  • what's you main search
  • which count you want to compare (e.g. numer of hosts).

so if you want to display the diference in numer of logging hosts between today and yesterday, you could run something like this:

index=_internal earliest=-d@d latest=now
| eval day=if(date_year=strftime(now(),"%Y") AND date_month=lower(strftime(now(),"%B")) AND date_mday=strftime(now(),"%d"),"Today","Previous")
| stats dc(host) AS hosts BY day
| delta hosts AS diff
| table diff

Ciao.

Giuseppe

Ciao.

Giuseppe

LogUx
Motivator

@gcusello  Can you suggest me another method. I mean another SPL. Because this search is very slow while executing. 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @LogUx,

if in yyour search you can use only the index time fields (index, sourcetyoe, source and host) you can use "|metasearch", having someting like this:

| metasearch index=_internal earliest=-d@d latest=now
| eval date_year=strftime(_time,"%Y"), date_month=strftime(_time,"%B"),date_mday=strftime(_time,"%d")
| eval now_date_year=strftime(now(),"%Y"), now_date_month=strftime(now(),"%B"),now_date_mday=strftime(now(),"%d")
| eval day=if(date_year=now_date_year AND date_month=now_date_month AND date_mday=now_date_mday,"Today","Previous")
| stats count BY day
| delta count AS diff
| reverse
| table diff
| head 1

Ciao.

 Giuseppe

LogUx
Motivator

@gcusello  It is working fine now . Thanks for yours extended help.

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @LogUx,

good for you, please accept the answer for the other people of Community.

Ciao and happy splunking.

Giuseppe

0 Karma

LogUx
Motivator

 Below is "no result" i am getting. I need todays day count, previous day count and the difference between todays count and previous day count. From the above query as you suggested not getting any results.

IMG_20201020_202435.jpgIMG_20201020_202435.jpg

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @LogUx,

this is because you have only today's events!

enlarge your data frame and you'll have results.

Ciao.

Giuseppe

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!

Your Feedback. Our Roadmap. Visit the PX Feedback Booth at .conf26

You use Splunk every day, come and help shape what's next.  Save Your Seat: Product-Focused Sessions at ...

Agentic SOC Triage: Investigating Splunk ES Notables with MCP Server and a Local LLM

The Problem: Too Many Alerts, Too Little Context Security operations teams running Splunk Enterprise Security ...

Painting a Clearer Picture: Creating Cross-Domain Visibility with AI Canvas

Watch Now Painting a Clearer Picture: Creating Cross-Domain Visibility with AI Canvas     Do you ever feel ...