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!

Event Series: Splunk Observability Metrics Cost Optimization

Balancing Scale and Spend: Gaining Control Over High-Volume Metrics in Splunk Observability Cloud As ...

Kick the Tires Before You Commit: A Hands-On Tour of the Splunk Observability Cloud ...

Evaluating an enterprise observability platform usually goes like this: fill out a form, get a free trial with ...

Deep insights, no barriers: Splunk Observability Cloud Free Edition

As software delivery cycles continue to accelerate, observability shouldn’t be a luxury — it should be a ...