Splunk Search

How to search the count of an event for the last sixty minutes, and the count of the same event for the same hour yesterday?

govindsinghrawa
Path Finder

How to get the count of an event (say logins) in last sixty minutes and the count of same event for same hour yesterday? Result should be as:

Today hh:mm:ss Count
Yesterday hh:mm:ss Count

0 Karma
1 Solution

twinspop
Influencer
your search earliest=-60m@m latest=@m| stats min(_time) as _time count as Count | eval Day="Today" | fields Day, _time, Count | append [ search your search earliest=-1d@m-60m latest=-1d@m | stats min(_time) as _time count as Count | eval Day="Yesterday" | fields Day, _time, Count ]

View solution in original post

gokadroid
Motivator

Lets assume that the event you have can be uniquely identified by yourBaseSearch, so your base search should return you unique events for whatever you are counting, then search twice and append them.

  1. One search to return today's count for an hour ago
    yourBaseSearch earliest=-60m latest=now()
    |fields anyFieldOfyoursToEnsureCountingOfEvents
    | timechart span=1m count 
    | eval _time=_time-now()%3600 
    | timechart span=1h sum(count) as count 
    | tail 3 | tail 2 
    | eval _time=_time+now()%3600
    |tail 1 
    
  2. SubSearch to return yesterday's count by shifting earliest and latest by 25 and 24 hours(in minutes to be accurate till minutes):


    search yourBaseSearchAgain earliest=-1500m latest=-1440m
    | timechart span=1m count
    | eval _time=_time-now()%3600
    | timechart span=1h sum(count) as count
    | tail 3 | tail 2
    | eval _time=_time+now()%3600
    |tail 1

  3. Append search 1 and 2

search1Above
|append [search2Above]
|sort +_time

NOTE: _time=_time-now()%3600 is given just to push the time in display to return the time correctly to represent since when the count is being taken.

govindsinghrawa
Path Finder

tested it as an answer post . seems to be working .

0 Karma

inventsekar
SplunkTrust
SplunkTrust

we can use date_hour and solve this specific timeframe issue.
Try this one -

index=main sourcetype=yourSourcetype earliest=-2d latest=now (date_hour > 1 OR date_hour < 2) | chart count(Failure) by host

Instead of chart, you use
|stats count AS Count

The date_hour, earliest /latest, combinations can be fine tuned.

0 Karma

twinspop
Influencer

I don't ever trust date_hour. Lots of past discussion on this. Search the archives.

0 Karma

govindsinghrawa
Path Finder

will it not just return for 1st and 2nd hour and not for 60 minutes ago from now. I will try though but seems not to be complete. Thanks a lot for helping out though

0 Karma

twinspop
Influencer
your search earliest=-60m@m latest=@m| stats min(_time) as _time count as Count | eval Day="Today" | fields Day, _time, Count | append [ search your search earliest=-1d@m-60m latest=-1d@m | stats min(_time) as _time count as Count | eval Day="Yesterday" | fields Day, _time, Count ]

govindsinghrawa
Path Finder

seems to work, thanks

0 Karma

sundareshr
Legend

Try this

base search earliest=-1d@d | eval when=if(_time>relative_time(now(), "@d"), "Today", "Yesterday") | eval t=strftime(_time, "%H") | chart count over t by when
0 Karma

govindsinghrawa
Path Finder

getting data for all hours and not just one hour of today and yesterday same hour

0 Karma
Get Updates on the Splunk Community!

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...