Create your own search commands by writings scripts. To build a search script, put a Python script in $SPLUNK_HOME/etc/searchscripts/. Python scripts in the searchscripts directory are available in the search language and can be used in a search. Find more examples on the Dev Wiki search script page.
For more help using Splunk's Python modules, please see the page on SKDs.
Configuration filesYou must add your search script by name to commands.conf and permissions to authorize.conf.
commands.confAdd an entry to commands.conf for your search script. This allows you to pipe your search to your custom search script.
[loglady] filename = loglady.py
Add two entries to authorize.conf.
First, add a capability for the script to be run:
[capability::run_script_loglady]
Second, add a line to any role to authorize users assigned that role to run the script:
run_script_loglady = enabled
Some things to know about passing results to and from a search command:
If your script is called myNewCommand.py, it can be used in a search as follows:
Please note:
The splunk.Intersplunk module directs events from Splunk to your Python search scripts.
The output of your script can then be fed back into Splunk as events. For example:
import sys,splunk.Intersplunk # this call populates the results variable with all the events passed into the search script: results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults() # hand the results right back to Splunk splunk.Intersplunk.outputResults(results)
Although this code snippet does not do much, it shows you how to get events pass the data back to Splunk. If you want to change some of the events, add a loop to iterate over all the events. Each event is comprised of a set of key-value pairs for every extracted field.
AuthenticateTo get your search script to authenticate into Splunk, add the following to your Python script:
results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()
authString = settings.get("authString", None)You can then pass authString to any part of your code which requires you to authenticate into Splunk.
You must also add an auth attribute to your stanza in commands.conf:
[MYSEARCHSCRIPT] filename = MYSEARCHSCRIPT.py passauth = true
tolower.py:
import sys,splunk.Intersplunk
# this call populates the results variable with all the events passed into the search script:
results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()
# create new list to pass back to Splunk
new_result_list = []
# Iterate over all the events:
for result in results:
# for all the events, you want to iterate over all the extracted fields:
new_result = {}
for key,value in result.items():
# change the result items. This example makes all the values lowercase.
value = value.lower()
new_result[key] = value
# add the changed result to the new list of results
new_result_list.append(new_result)
# hand the results right back to Splunk
splunk.Intersplunk.outputResults(new_result_list)Changing events with your own command is probably the most common use-case. You do not necessarily have to return the entire original set of events. You can return any key-value pairs back to Splunk. For example:
# This prepares the return value for the script
newresults = [ { "afterglowFilename" : "afterglow.html" } ]
splunk.Intersplunk.outputResults(newresults)This example returns only one key/value pair. This could then be combined with a field action to execute some action on this field, for example displaying the html file indicated in the value part.
Comments
No comments have been submitted.