Splunk Search

How to find events that haven't happened in a given amount of time?

atebysandwich
Path Finder

I'm looking over vulnerability scan data and have the _time field formatted as 

 

| eval Last_Scanned = strftime(time, "%F")

 

How can I created a search to show hosts(events) that have not been scanned within two weeks of the current date?

Labels (3)
0 Karma
1 Solution

rut
Path Finder

Grouping by host and then filtering using relative_time should work. This only leaves you the date and host though, so maybe you'll want to add some fields to the stats command.

| stats max(_time) as Last_Scanned by host
| where Last_Scanned<relative_time(now(), "-2w")

 

View solution in original post

0 Karma

PickleRick
SplunkTrust
SplunkTrust

It depends whether you can find those hosts by expanding your time range. If you can, just find max(_time) by host and check if it falls within needed range. If you can't, you must have a static list of hosts to compare events in your index with. You can't find something if it's not there.

0 Karma

rut
Path Finder

Grouping by host and then filtering using relative_time should work. This only leaves you the date and host though, so maybe you'll want to add some fields to the stats command.

| stats max(_time) as Last_Scanned by host
| where Last_Scanned<relative_time(now(), "-2w")

 

0 Karma

atebysandwich
Path Finder

I ad to do some tweaking to make some of it work. when I did

| stats max(Last_Scanned) by IP

I got all the IPs and their last scan time. However, when I did the second line, no results were found. 

It should be noted that earlier in the search _time was specified as time 

Tags (1)
0 Karma

PickleRick
SplunkTrust
SplunkTrust

You can't do max() on non-numerical field. When you did your strftime() you lost the ability to calculate/compare timestamps.

0 Karma

rut
Path Finder

OK, so are you adding the lines on _time or your formatted time? In your original question you added the following line:

 

| eval Last_Scanned = strftime(time, "%F")

 

%F = Equivalent to %Y-%m-%d (the ISO 8601 date format).

The command max and the comparison with relative_time are expecting a timestamp, not formatted time. So you can either use the original timestamp or use strptime to transform it back.

See the following docs for more information.

strptime:

https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/DateandTimeFunctions#strptime.28...

relative_time:

https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/DateandTimeFunctions#relative_ti...

Formats:

https://docs.splunk.com/Documentation/Splunk/9.0.4/SearchReference/Commontimeformatvariables#Time_va...

0 Karma

atebysandwich
Path Finder

I removed strftime and moved a lookup after the searches you mentioned and it worked. 

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...

Data Persistence in the OpenTelemetry Collector

This blog post is part of an ongoing series on OpenTelemetry. What happens if the OpenTelemetry collector ...

Thanks for the Memories! Splunk University, .conf25, and our Community

Thank you to everyone in the Splunk Community who joined us for .conf25, which kicked off with our iconic ...