Splunk Search

Using a Macro to return the earliest time value

theouhuios
Motivator

Hello

I have drop-down acting like a timepicker. So when a user selects "Current Month", the $time$ (token for the drop down) is earliest=@mon latest=now(). Now I have to use this token and get the earliest value out to use it in a subsearch. I DON'T need the latest value from the token.

So I wrote a macro earliestevent(1) which does this and is set to eval-definition=true

earliest=-1m@m index=abc|stats count |append[|stats count|fields count|rename count as Time|eval Time="$time$"]|eventstats first(Time) as Time|rex field=Time "earliest\=(?P<earliest>\S+)"|dedup earliest|table earliest|eval earliest=tostring(earliest)

Now I get the @mon as the value, but when I do earliest=earliestevent("earliest=@mon latest=now()") index=_internal (Somehow I am not able to place ` around the macro and get the format) it gives me

Error in 'SearchParser': The definition of macro 'earliestevent(1)' is expected to be an eval expression that returns a string. 

Not sure what mistake I am doing. It's my first time writing an eval- definition macro. Any ideas on where I might be wrong?

Tags (4)
0 Karma

woodcock
Esteemed Legend

It is telling you that your macro must be a streaming portion of a search, not a complete search. In other words, whatever goes into a macro must be something that fits in a fuller search after a pipe ( | ) character. So your macro is nonsensical because it is not syntactically valid to do a search like this:

... | earliest= ...

I am not sure what you are trying to do exactly but this is one way to make your macro syntactically (but probably not calculatingly) valid:

append [search earliest=-1m@m index=abc|stats count |append[|stats count|fields count|rename count as Time|eval Time="$time$"]|eventstats first(Time) as Time|rex field=Time "earliest\=(?P<earliest>\S+)"|dedup earliest|table earliest|eval earliest=tostring(earliest)]

This should be syntactically valid because append is valid after a pipe. Remember that you may only call a macro after a pipe like this:

... | `earliestevent("earliest=@mon latest=now()")`
0 Karma

theouhuios
Motivator

Thanks for the explanation. I understand where I am doing wrong. But is there a way that I can take a string like "earliest=@mon latest=now()" and get the output result as whatever the earliest value is and use it like

...|join report_month [search earliest=`macro($arg$)` latest=`macro($arg$)`+1h@h index=blah ...]` ? 
0 Karma

woodcock
Esteemed Legend

You can use addinfo, like this:

... | addinfo | join report_month [search earliest=info_min_time latest=info_max_time+3600 index=blah ...]

http://docs.splunk.com/Documentation/Splunk/6.2.4/SearchReference/addinfo

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 ...