Splunk Search

How do I compare two time slices?

asturt
Explorer

I have a search that I want to run twice, but for different time slices. The result of the two slices will then be compared to get a measure of the difference. My current code has the same search twice but with different earliest and latest values, associated using appendcols. What I want to know is if there is a way to write the search once (instead of twice) and reuse that code; something like a common table expression in SQL?

0 Karma
1 Solution

woodcock
Esteemed Legend

You can give the same base search 2 different earliest/latest specifiers like this:

index=YouShouldAlwaysSpecifyYourIndex AND sourcetype=AndSourcetypeToo
((earliest=@d latest=@d+1h) OR (earliest=-1d@d latest=-1d@d+1h))
| eval which=if(_time<relative_time(now(), "@d"), "yesterday", "today")
| stats count by which

View solution in original post

woodcock
Esteemed Legend

You can give the same base search 2 different earliest/latest specifiers like this:

index=YouShouldAlwaysSpecifyYourIndex AND sourcetype=AndSourcetypeToo
((earliest=@d latest=@d+1h) OR (earliest=-1d@d latest=-1d@d+1h))
| eval which=if(_time<relative_time(now(), "@d"), "yesterday", "today")
| stats count by which

skoelpin
SplunkTrust
SplunkTrust

You should use relative_time against each time slice. This will give you epoch time and you can compare against that

| eval today=relative_time(now(), "0d@d")
| eval yesterday=relative_time(today, "-1d@d")

http://docs.splunk.com/Documentation/Splunk/7.1.3/SearchReference/DateandTimeFunctions

0 Karma

DalJeanis
Legend

Appendcols is almost never the right answer. NO, there is no CTE in Splunk...you can achieve some of that with macros, but you don't need to for this use case.

Here's the pseudocode you need -

your search that gets ALL the records for both (or all)  time periods 
| fields  ...list the fields you need...
| eval timeframe= case(this is the first time frame, "flag1", 
                       this is the second time frame, "flag2", 
                       ...for however many timeframes you want)
| where isnotnull(timeframe)

| now do all your processing and setup for the records

| now do summary processing and presentation

1) Note that any records which don't match one of your desired timeframes get dropped.

2) If you are going to do a timechart, then you have two options. First, if appropriate, you can use the timewrap facility, or second, you can add time to all the earlier timeframes to make them align exactly with the current timeframe. Either method works.

0 Karma

asturt
Explorer

Thank you. That's an elegant method for restricting the search to a single scan of the data.

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!

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...

Skip the Awkward Silence: Have a .conf-ersation at .conf26

Picture this. You arrive at .conf26 already having your socializing and networking plans mapped out. No ...

Rethinking Zero Trust: From Product Purchases to Logical Control Evidence

Implementing Zero Trust (ZT) across complex environments often falters at the very beginning due to a ...