Splunk Search

Only include certain hours/days for a long term search

sepkarimpour
Path Finder

I'm looking to run a search over a 4 week period here I find the count of results per week but I want to look for a specific time range - Sunday 11pm to Friday 11pm.

Having found an answer to a similar question to this, I'm using this search at the moment:

"base search"
| eval hour = tonumber(strftime(_time,"%H"))
| eval dow = tonumber(strftime(_time,"%w"))
| where dow!=6 AND (dow!=0 AND hour<=23) AND (dow!=5 AND hour>=23)
| timechart span=7d count

I can see the number of results is significantly lower when I run the query with these restrictions: 20m+ results with all days included, under 1m results with the WHERE command included. Would this correctly show the range I'm looking for?

0 Karma
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi sepkarimpour,
instead use eval, you could use Splunk fields date_wday and date_hours with the same logic, but it's more efficient:

your_search NOT ((date_wday=friday date_hour>23) OR date_wday=saturday OR (date_wday=sunday date_hour<24))
| timechart span=7d count

Bye.
Giuseppe

View solution in original post

cmerriman
Super Champion

try this statement instead:

|eval keep=if((date_wday="sunday" AND date_hour>=23) OR (date_wday="friday" AND date_hour<23 ) OR date_wday="monday" OR date_wday="tuesday" OR date_wday="wednesday" OR date_wday="thursday",1,0)
|search keep=1| timechart span=7d count

the date_wday and date_hour fields should be in your data, as they are derived from the event timestamp.
https://docs.splunk.com/Documentation/SplunkCloud/6.6.1/Knowledge/Usedefaultfields

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi sepkarimpour,
instead use eval, you could use Splunk fields date_wday and date_hours with the same logic, but it's more efficient:

your_search NOT ((date_wday=friday date_hour>23) OR date_wday=saturday OR (date_wday=sunday date_hour<24))
| timechart span=7d count

Bye.
Giuseppe

sepkarimpour
Path Finder

Ah, this is picking up a lot more results now. I had read before that sometimes wday and hour aren't picked up properly sometimes from the logs. This seems to work much better now with the number much closer to the original now. Thanks.

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...