<?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: How to create a custom macro / function inside the search query? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109425#M28547</link>
    <description>&lt;P&gt;Update ping: today, I was able to test it on real Windows events and here is the correct first &lt;CODE&gt;stats&lt;/CODE&gt; &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats last(_time) AS last_time last(eval(round(max(Value),2 ))) AS Value by _time, host, counter
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;replace it in the provided search and it will work &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; This is needed because the field &lt;CODE&gt;Value&lt;/CODE&gt; is a multivalue field.&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
    <pubDate>Thu, 13 Nov 2014 08:17:47 GMT</pubDate>
    <dc:creator>MuS</dc:creator>
    <dc:date>2014-11-13T08:17:47Z</dc:date>
    <item>
      <title>How to create a custom macro / function inside the search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109418#M28540</link>
      <description>&lt;P&gt;In the query below, for each host, I am searching for its performance data for each value for past 5 minutes. &lt;BR /&gt;
The expected output is the following:&lt;/P&gt;

&lt;P&gt;&lt;A href="http://i.imgur.com/9VR1kyP.png"&gt;Open screenshot&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;&lt;IMG src="http://i.imgur.com/eHOy9DS.png" alt="See screenshot" /&gt;&lt;/P&gt;

&lt;P&gt;I have solved this problem using 4 joins… But that made the source code large and ugly… &lt;BR /&gt;
Is there any way I can optimize the size of the query below? &lt;BR /&gt;
Can a define a custom macro inside the query and call it several times with different parameters instead of copy-pasting the code? &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=perfmon host=lena  counter="% Processor Time" earliest=-1m  
   | fields host,counter,Value  
     | eval ValueR_1m_Ago = round(Value, 2) 
     | eval HostUpperCase = upper(host) 
   | convert ctime(_time) as Time_1m_Ago 
   | fields HostUpperCase, counter, ValueR_1m_Ago, Time_1m_Ago  

| join  HostUpperCase 
[search index=perfmon host=lena  counter="% Processor Time" earliest=-2m latest=-1m  
   | fields host,counter,Value  
     | eval ValueR_2m_Ago = round(Value, 2) 
     | eval HostUpperCase = upper(host) 
   | convert ctime(_time) as Time_2m_Ago 
   | fields HostUpperCase, ValueR_2m_Ago, Time_2m_Ago ]  

| join HostUpperCase 
[search index=perfmon host=lena  counter="% Processor Time" earliest=-3m latest=-2m  
   | fields host,counter,Value  
     | eval ValueR_3m_Ago = round(Value, 2) 
     | eval HostUpperCase = upper(host) 
   | convert ctime(_time) as Time_3m_Ago 
   | fields HostUpperCase, ValueR_3m_Ago, Time_3m_Ago ] 

| join HostUpperCase 
[search index=perfmon host=lena  counter="% Processor Time" earliest=-4m latest=-3m  
   | fields host,counter,Value  
     | eval ValueR_4m_Ago = round(Value, 2) 
     | eval HostUpperCase = upper(host) 
   | convert ctime(_time) as Time_4m_Ago 
   | fields HostUpperCase, ValueR_4m_Ago, Time_4m_Ago ]  

| join HostUpperCase 
[search index=perfmon host=lena  counter="% Processor Time" earliest=-5m latest=-4m  
   | fields host,counter,Value  
     | eval ValueR_5m_Ago = round(Value, 2) 
     | eval HostUpperCase = upper(host) 
   | convert ctime(_time) as Time_5m_Ago 
   | fields HostUpperCase, ValueR_5m_Ago, Time_5m_Ago ]  
| DEDUP HostUpperCase
| sort -ValueR_1m_Ago
| table HostUpperCase 
        ,counter 
        ,ValueR_1m_Ago 
        ,Time_1m_Ago 
        ,ValueR_2m_Ago 
        ,ValueR_3m_Ago 
        ,ValueR_4m_Ago 
        ,ValueR_5m_Ago
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 09 Nov 2014 07:25:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109418#M28540</guid>
      <dc:creator>dzhariy</dc:creator>
      <dc:date>2014-11-09T07:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom macro / function inside the search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109419#M28541</link>
      <description>&lt;P&gt;Hi dzhariy,&lt;/P&gt;

&lt;P&gt;Take a look at the &lt;CODE&gt;timewrap&lt;/CODE&gt; &lt;A href="http://apps.splunk.com/app/1645/"&gt;app&lt;/A&gt;, this will provide a new search command that will simplify such searches.&lt;/P&gt;

&lt;P&gt;hope this helps...&lt;/P&gt;

&lt;P&gt;cheers, MuS &lt;/P&gt;</description>
      <pubDate>Sun, 09 Nov 2014 09:06:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109419#M28541</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-11-09T09:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom macro / function inside the search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109420#M28542</link>
      <description>&lt;P&gt;That, very much so.&lt;/P&gt;

&lt;P&gt;For other cases that don't get solved by &lt;CODE&gt;timewrap&lt;/CODE&gt; you can define macros through Settings -&amp;gt; Advanced search -&amp;gt; Macros and even give them parameters, in this example your varying time ranges.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Nov 2014 11:26:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109420#M28542</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-11-09T11:26:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom macro / function inside the search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109421#M28543</link>
      <description>&lt;P&gt;Thank you MuS, &lt;BR /&gt;
I’ve looked through the timewrap app and is definitely worth to look at. Unfortunately, in my organization, &lt;BR /&gt;
I am a regular splunk user with restricted permissions and connot install apps. I will try to ask my administrators to install the app…  Anyway, I still can experiment on my localhost splunk.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Nov 2014 21:23:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109421#M28543</guid>
      <dc:creator>dzhariy</dc:creator>
      <dc:date>2014-11-09T21:23:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom macro / function inside the search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109422#M28544</link>
      <description>&lt;P&gt;Hi dzhariy, have a look at the soon to be posted new answer here....I did some stats craziness - again - which could replace your monster join search&lt;/P&gt;</description>
      <pubDate>Tue, 11 Nov 2014 20:12:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109422#M28544</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-11-11T20:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom macro / function inside the search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109423#M28545</link>
      <description>&lt;P&gt;Hi dzhariy,&lt;/P&gt;

&lt;P&gt;looking at your search I was wondering how this could be done by using a &lt;CODE&gt;stats&lt;/CODE&gt; and some &lt;CODE&gt;eval&lt;/CODE&gt; tricks and guess what, I found a way to do this &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;Let me show you a run everywhere search (assuming you got the permission to search the &lt;CODE&gt;index=_internal&lt;/CODE&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal earliest=-5min@min sourcetype=splunkd | bucket _time span=1min | stats last(_time) AS last_time count AS per_min_count by _time, host, sourcetype 
| eval 5min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-6min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-5min@min")) , per_min_count ,"0") 
| eval 4min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-5min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-4min@min")) , per_min_count ,"0") 
| eval 3min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-4min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-3min@min")) , per_min_count ,"0")
| eval 2min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-3min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-2min@min")) , per_min_count ,"0")
| eval 1min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-2min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-1min@min")) , per_min_count ,"0")
| eval current_count = if(last_time &amp;gt; exact(relative_time(now(),"-1min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-0min@min")) , per_min_count ,"0")
| stats max(last_time) AS _time, values(host) AS host, values(sourcetype) AS sourcetype, max(current_count) AS current_count, max(1min_ago) AS 1min_ago, max(2min_ago) AS 2min_ago, max(3min_ago) AS 3min_ago, max(4min_ago) AS 4min_ago, max(5min_ago) AS 5min_ago
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This will produce a table like this, which matches your result:&lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/144i446BFBEA69921212/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;Now let me try to adapt this to your search and something like this should work:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=perfmon host=lena  counter="% Processor Time" earliest=-5min@min
| bucket _time span=1min 
| stats last(_time) AS last_time max(exact(Value)) AS Value by _time, host, counter
| eval 5min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-6min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-5min@min")) , Value ,"0") 
| eval 4min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-5min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-4min@min")) , Value ,"0") 
| eval 3min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-4min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-3min@min")) , Value ,"0")
| eval 2min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-3min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-2min@min")) , Value ,"0")
| eval 1min_ago = if(last_time &amp;gt; exact(relative_time(now(),"-2min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-1min@min")) , Value ,"0")
| eval current_count = if(last_time &amp;gt; exact(relative_time(now(),"-1min@min")) AND last_time &amp;lt;= exact(relative_time(now(),"-0min@min")) , Value ,"0")
| stats max(last_time) AS _time, values(host) AS host, values(counter) AS counter, max(current_count) AS current_count, max(1min_ago) AS 1min_ago, max(2min_ago) AS 2min_ago, max(3min_ago) AS 3min_ago, max(4min_ago) AS 4min_ago, max(5min_ago) AS 5min_ago
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The above search is untested due to missing windows events, so if there is any mistake in the search - adapt it to your needs &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;hope this helps ...&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Tue, 11 Nov 2014 20:26:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109423#M28545</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-11-11T20:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom macro / function inside the search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109424#M28546</link>
      <description>&lt;P&gt;Hi MuS, &lt;BR /&gt;
Thank you. I have executed your original query for splunkd, and looks like it is working. &lt;BR /&gt;
As you warned, the query for perfomon is not working “out of the box” &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; However, I believe, the general idea of your query is the way to go for me: I just need more time to figure out why it returns zeros.&lt;BR /&gt;&lt;BR /&gt;
I think this is the answer for my question. Thank you, MuS! &lt;/P&gt;</description>
      <pubDate>Wed, 12 Nov 2014 15:47:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109424#M28546</guid>
      <dc:creator>dzhariy</dc:creator>
      <dc:date>2014-11-12T15:47:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom macro / function inside the search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109425#M28547</link>
      <description>&lt;P&gt;Update ping: today, I was able to test it on real Windows events and here is the correct first &lt;CODE&gt;stats&lt;/CODE&gt; &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats last(_time) AS last_time last(eval(round(max(Value),2 ))) AS Value by _time, host, counter
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;replace it in the provided search and it will work &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; This is needed because the field &lt;CODE&gt;Value&lt;/CODE&gt; is a multivalue field.&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Thu, 13 Nov 2014 08:17:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109425#M28547</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-11-13T08:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a custom macro / function inside the search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109426#M28548</link>
      <description>&lt;P&gt;thank you MuS!&lt;BR /&gt;
Now query works on my sandbox environment and I have tried it on production one. &lt;BR /&gt;
The query speed was increased (according to my naked eye) in 4-5 times.&lt;BR /&gt;
THANK YOU!&lt;/P&gt;</description>
      <pubDate>Sat, 15 Nov 2014 21:13:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-create-a-custom-macro-function-inside-the-search-query/m-p/109426#M28548</guid>
      <dc:creator>dzhariy</dc:creator>
      <dc:date>2014-11-15T21:13:18Z</dc:date>
    </item>
  </channel>
</rss>

