<?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: Can a custom search script invoke an eval/where expression? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67400#M16852</link>
    <description>&lt;P&gt;You'd need to build the evaluation logic into your Python script.&lt;/P&gt;

&lt;P&gt;Parameters like this are going to be passed into your Python script as command-line arguments -- you can get them from &lt;CODE&gt;sys.argv&lt;/CODE&gt; and do whatever you want from there.&lt;/P&gt;</description>
    <pubDate>Sat, 09 Oct 2010 00:26:56 GMT</pubDate>
    <dc:creator>southeringtonp</dc:creator>
    <dc:date>2010-10-09T00:26:56Z</dc:date>
    <item>
      <title>Can a custom search script invoke an eval/where expression?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67398#M16850</link>
      <description>&lt;P&gt;I would like to be able to create a custom search command that uses an eval-expression as an  argument.  (Similar to how you can say &lt;CODE&gt;... | transaction startswith=(eval-expression) ...&lt;/CODE&gt;)&lt;/P&gt;

&lt;P&gt;Does anyone know if it's possible to invoke an eval-expression from within the python custom search command?  Obviously this can be done for built-in commands like &lt;CODE&gt;transaction&lt;/CODE&gt;, but that doesn't seem to be written in python.&lt;/P&gt;

&lt;P&gt;If this can't be done, any ideas as a functional workaround?&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2010 00:22:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67398#M16850</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-10-09T00:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: Can a custom search script invoke an eval/where expression?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67399#M16851</link>
      <description>&lt;P&gt;if it is not supported, it would be a great enhancement!&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2010 00:25:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67399#M16851</guid>
      <dc:creator>ziegfried</dc:creator>
      <dc:date>2010-10-09T00:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: Can a custom search script invoke an eval/where expression?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67400#M16852</link>
      <description>&lt;P&gt;You'd need to build the evaluation logic into your Python script.&lt;/P&gt;

&lt;P&gt;Parameters like this are going to be passed into your Python script as command-line arguments -- you can get them from &lt;CODE&gt;sys.argv&lt;/CODE&gt; and do whatever you want from there.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2010 00:26:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67400#M16852</guid>
      <dc:creator>southeringtonp</dc:creator>
      <dc:date>2010-10-09T00:26:56Z</dc:date>
    </item>
    <item>
      <title>Re: Can a custom search script invoke an eval/where expression?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67401#M16853</link>
      <description>&lt;P&gt;Sorry, but I think your missing the point.  I know I can do whatever I want within python, but the point is that I don't want to go &lt;EM&gt;build&lt;/EM&gt; by own eval-engine.  I simply want to leverage the one Splunk already built.  (The &lt;CODE&gt;eval&lt;/CODE&gt; command already has over 40 functions and that list is growing each release.  I certainly don't want to go build my own mini expression language and all those functions. And then try to keep that in sync with every new splunk release.)&lt;/P&gt;</description>
      <pubDate>Sat, 09 Oct 2010 00:49:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67401#M16853</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-10-09T00:49:37Z</dc:date>
    </item>
    <item>
      <title>Re: Can a custom search script invoke an eval/where expression?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67402#M16854</link>
      <description>&lt;P&gt;Here is one hacked up way to do this.  BTW, I'm going to let this unanswered for now, in the hopes that someone come up with a better (less-hackish) answer.  Also, the ability to directly issue an eval/where expression from within a custom search command could be extended to many more situations than this limited hack. &lt;/P&gt;

&lt;P&gt;After some playing around, I came up with an approach that works by using a combination of &lt;CODE&gt;getinfo&lt;/CODE&gt; and a dynamic &lt;CODE&gt;preop&lt;/CODE&gt; command.  This approach leverages the fact that the &lt;CODE&gt;getinfo&lt;/CODE&gt; mechanism calls the script twice, and passes in the arguments to the script both times.  The first &lt;CODE&gt;__GETINFO__&lt;/CODE&gt; call dynamically builds a pre-streaming operation based on the argument passed to the script.  This causes the pre-operation command to execute an &lt;CODE&gt;eval&lt;/CODE&gt; command before my custom search command is run; and so the temporary field is accessible from my search command when it's called in &lt;CODE&gt;__EXECUTE__&lt;/CODE&gt; mode.&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;Below is a demo version of my script.  If the "filter" eval expression evaluates to true, then function &lt;CODE&gt;f1&lt;/CODE&gt; is used to handle the event, and if it evaluates to false then function &lt;CODE&gt;f2&lt;/CODE&gt; is used instead.  In this simple example, &lt;CODE&gt;f1()&lt;/CODE&gt; adds two fields, and &lt;CODE&gt;f2()&lt;/CODE&gt; multiples two fields.  If no "filter" is given, then &lt;CODE&gt;f2()&lt;/CODE&gt; is always called.&lt;/P&gt;

&lt;P&gt;The script is called like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | mycmd filter="date_hour&amp;gt;12" | table a b sum product
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;mycmd.py:&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunk.Intersplunk

(isgetinfo, sys.argv) = splunk.Intersplunk.isGetInfo(sys.argv)
args, kwargs = splunk.Intersplunk.getKeywordsAndOptions()

# Process args
eval_filter = kvargs.get("filter", None)

if isgetinfo:
    # streaming, generating, retevs, reqsop, preop
    if eval_filter:
        splunk.Intersplunk.outputInfo(False, False, False, True, "addinfo | eval _filter=if(%s,1,0)" % eval_filter)
    else:
        splunk.Intersplunk.outputInfo(False, False, False, True, "addinfo")

def f1(r):
    # Add field 'a' and 'b'
    r["sum"] = int(r["a"]) + int(r["b"])
    return r

def f2(r):
    # Multiply field 'a' and 'b' 
    r["product"] = int(r["a"]) * int(r["b"])
    return r

if __name__ == '__main__':
    (results, dummyresults, settings) = splunk.Intersplunk.getOrganizedResults()
    if len(results) &amp;gt;= 1:
        search_id = results[0]["info_sid"]
    else:
        search_id = None
    logger.info("SID:  %r", search_id)
    output = [] 
    try:
        for result in results:
            if "_filter" in result:
                if result["_filter") == "1":
                    result = f1(result)
                else:
                    result = f2(result)
                del result["_filter"]
            else:
                result = f2(result)
            output.append(result)
        splunk.Intersplunk.outputResults(output)
    except Exception, e:
        splunk.Intersplunk.generateErrorResults("Unhandled exception:  %s" % (e,))
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Oct 2010 03:05:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67402#M16854</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-10-09T03:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: Can a custom search script invoke an eval/where expression?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67403#M16855</link>
      <description>&lt;P&gt;Hi Lowell, is this script working in 4.2.x? I'm trying to run it but it returns 'error 1' although I fixed your typo and change a|b field to existing ones that match my search&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2011 10:54:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-a-custom-search-script-invoke-an-eval-where-expression/m-p/67403#M16855</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2011-11-14T10:54:40Z</dc:date>
    </item>
  </channel>
</rss>

