<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using a Macro to return the earliest time value in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Using-a-Macro-to-return-the-earliest-time-value/m-p/119793#M32152</link>
    <description>&lt;P&gt;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 &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...|join report_month [search earliest=`macro($arg$)` latest=`macro($arg$)`+1h@h index=blah ...]` ? 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 15 Jul 2015 12:09:34 GMT</pubDate>
    <dc:creator>theouhuios</dc:creator>
    <dc:date>2015-07-15T12:09:34Z</dc:date>
    <item>
      <title>Using a Macro to return the earliest time value</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-a-Macro-to-return-the-earliest-time-value/m-p/119791#M32150</link>
      <description>&lt;P&gt;Hello &lt;/P&gt;

&lt;P&gt;I have drop-down acting like a timepicker. So when a user selects "Current Month", the &lt;CODE&gt;$time$&lt;/CODE&gt; (token for the drop down) is &lt;CODE&gt;earliest=@mon latest=now()&lt;/CODE&gt;. 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.&lt;/P&gt;

&lt;P&gt;So I wrote a macro earliestevent(1) which does this and is set to eval-definition=true&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;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&amp;lt;earliest&amp;gt;\S+)"|dedup earliest|table earliest|eval earliest=tostring(earliest)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now I get the &lt;CODE&gt;@mon&lt;/CODE&gt; as the value, but when I do &lt;CODE&gt;earliest=earliestevent("earliest=@mon latest=now()") index=_internal&lt;/CODE&gt; (Somehow I am not able to place ` around the macro and get the format) it gives  me &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Error in 'SearchParser': The definition of macro 'earliestevent(1)' is expected to be an eval expression that returns a string. 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;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?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2015 19:54:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-a-Macro-to-return-the-earliest-time-value/m-p/119791#M32150</guid>
      <dc:creator>theouhuios</dc:creator>
      <dc:date>2015-07-14T19:54:38Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Macro to return the earliest time value</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-a-Macro-to-return-the-earliest-time-value/m-p/119792#M32151</link>
      <description>&lt;P&gt;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 ( &lt;CODE&gt;|&lt;/CODE&gt; ) character.  So your macro is nonsensical because it is not syntactically valid to do a search like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | earliest= ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;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:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;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&amp;lt;earliest&amp;gt;\S+)"|dedup earliest|table earliest|eval earliest=tostring(earliest)]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This should be syntactically valid because &lt;CODE&gt;append&lt;/CODE&gt; is valid after a pipe.  Remember that you may only call a macro after a pipe like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | `earliestevent("earliest=@mon latest=now()")`
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Jul 2015 20:05:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-a-Macro-to-return-the-earliest-time-value/m-p/119792#M32151</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2015-07-14T20:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Macro to return the earliest time value</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-a-Macro-to-return-the-earliest-time-value/m-p/119793#M32152</link>
      <description>&lt;P&gt;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 &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...|join report_month [search earliest=`macro($arg$)` latest=`macro($arg$)`+1h@h index=blah ...]` ? 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jul 2015 12:09:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-a-Macro-to-return-the-earliest-time-value/m-p/119793#M32152</guid>
      <dc:creator>theouhuios</dc:creator>
      <dc:date>2015-07-15T12:09:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Macro to return the earliest time value</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-a-Macro-to-return-the-earliest-time-value/m-p/119794#M32153</link>
      <description>&lt;P&gt;You can use &lt;CODE&gt;addinfo&lt;/CODE&gt;, like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | addinfo | join report_month [search earliest=info_min_time latest=info_max_time+3600 index=blah ...]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.4/SearchReference/addinfo"&gt;http://docs.splunk.com/Documentation/Splunk/6.2.4/SearchReference/addinfo&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2015 16:15:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-a-Macro-to-return-the-earliest-time-value/m-p/119794#M32153</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2015-07-15T16:15:26Z</dc:date>
    </item>
  </channel>
</rss>

