Are there any splunk specific variables exposed to scripted inputs that I could use to navigate to files I distribute with my app?
For instance I want to parse a file and use values from it, but don't want to hardcode
/path/to/splunk/app/local/myscript.conf in the python file.
I'm looking for something like a
SPLUNK_HOME + '/etc/apps/' + APPLICATION + '/local/myscript.conf'
Or similar. Is there anything like this?
The environment will typically have $SPLUNK_HOME set to '/opt/splunk' or whatever your local installation path is.
import os APPNAME = 'myapp' FILENAME = 'myscript.conf' SPLUNK_HOME = os.environ['SPLUNK_HOME'] filename = os.path.join(SPLUNK_HOME,'etc','apps',APPNAME,'local',CONFIGNAME)
Filename should now contain '/opt/splunk/etc/apps/myapp/local/myscript.conf'.
I recommend you compute a path relative to the script location, i.e.,
sys.path, so that it will also correctly under distributed search map-reduce.
I wondered about that, but wasn't sure if that approach would have differing results depending on whether the end-user script was called directly or via ScriptRunner. Though IIRC, the latter would only apply to custom search scripts and not scripted inputs.
sys.path in python will have the path of the script, whether it's called via scriptrunner or as an argument to python or running directly. Note that it is different in this regard from
You can count on
SPLUNK_HOME, but that's about it, and may not be what you should be using. To get the application path, you can take the relative path from the script's directory. In python, that will be
sys.path (in contrast to
sys.path has the location of the script regardless of how it was invoked), so to get the app base, you could usually do
or to get the path to your file:
If you just want the app name, then:
assuming your script is directly in the app's
It important that you calculate your paths relative to the current script location and not
SPLUNK_HOME if you expect the script to be run via map-reduce on distributed search nodes, as the replication of the app config puts it into a different location and you can therefore not assume the app base is in