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.
After some playing around, I came up with an approach that works by using a combination of getinfo and a dynamic preop command. This approach leverages the fact that the getinfo mechanism calls the script twice, and passes in the arguments to the script both times. The first __GETINFO__ call dynamically builds a pre-streaming operation based on the argument passed to the script. This causes the pre-operation command to execute an eval command before my custom search command is run; and so the temporary field is accessible from my search command when it's called in __EXECUTE__ mode.
Below is a demo version of my script. If the "filter" eval expression evaluates to true, then function f1 is used to handle the event, and if it evaluates to false then function f2 is used instead. In this simple example, f1() adds two fields, and f2() multiples two fields. If no "filter" is given, then f2() is always called.
The script is called like this:
... | mycmd filter="date_hour>12" | table a b sum product
mycmd.py:
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) >= 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,))
... View more