<?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: scripted input python variables in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44047#M532</link>
    <description>&lt;P&gt;The environment will typically have $SPLUNK_HOME set to '/opt/splunk' or whatever your local installation path is.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import os

APPNAME     = 'myapp'
FILENAME    = 'myscript.conf'
SPLUNK_HOME = os.environ['SPLUNK_HOME']

filename = os.path.join(SPLUNK_HOME,'etc','apps',APPNAME,'local',CONFIGNAME)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Filename should now contain '/opt/splunk/etc/apps/myapp/local/myscript.conf'.&lt;/P&gt;</description>
    <pubDate>Sat, 04 Sep 2010 23:20:11 GMT</pubDate>
    <dc:creator>southeringtonp</dc:creator>
    <dc:date>2010-09-04T23:20:11Z</dc:date>
    <item>
      <title>scripted input python variables</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44046#M531</link>
      <description>&lt;P&gt;Are there any splunk specific variables exposed to scripted inputs that I could use to navigate to files I distribute with my app?&lt;/P&gt;

&lt;P&gt;For instance I want to parse a file and use values from it, but don't want to hardcode &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;P&gt;/path/to/splunk/app/local/myscript.conf in the python file.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I'm looking for something like a&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
  &lt;P&gt;SPLUNK_HOME + '/etc/apps/' + APPLICATION + '/local/myscript.conf'&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Or similar. Is there anything like this?&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2010 15:16:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44046#M531</guid>
      <dc:creator>caphrim007</dc:creator>
      <dc:date>2010-09-04T15:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: scripted input python variables</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44047#M532</link>
      <description>&lt;P&gt;The environment will typically have $SPLUNK_HOME set to '/opt/splunk' or whatever your local installation path is.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import os

APPNAME     = 'myapp'
FILENAME    = 'myscript.conf'
SPLUNK_HOME = os.environ['SPLUNK_HOME']

filename = os.path.join(SPLUNK_HOME,'etc','apps',APPNAME,'local',CONFIGNAME)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Filename should now contain '/opt/splunk/etc/apps/myapp/local/myscript.conf'.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2010 23:20:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44047#M532</guid>
      <dc:creator>southeringtonp</dc:creator>
      <dc:date>2010-09-04T23:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: scripted input python variables</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44048#M533</link>
      <description>&lt;P&gt;I recommend you compute a path relative to the script location, i.e., &lt;CODE&gt;sys.path[0]&lt;/CODE&gt;, so that it will also correctly under distributed search map-reduce.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2010 23:27:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44048#M533</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2010-09-04T23:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: scripted input python variables</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44049#M534</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2010 23:46:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44049#M534</guid>
      <dc:creator>southeringtonp</dc:creator>
      <dc:date>2010-09-04T23:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: scripted input python variables</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44050#M535</link>
      <description>&lt;P&gt;You can count on &lt;CODE&gt;SPLUNK_HOME&lt;/CODE&gt;, 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 &lt;CODE&gt;sys.path[0]&lt;/CODE&gt; (in contrast to &lt;CODE&gt;sys.argv[0]&lt;/CODE&gt;, &lt;CODE&gt;sys.path[0]&lt;/CODE&gt; has the location of the script regardless of how it was invoked), so to get the app base, you could usually do&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;os.path.join(sys.path[0],"..")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;or to get the path to your file:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;os.path.join(sys.path[0],"..","local","myscript.conf")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you just want the app name, then:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;os.path.basename(os.path.dirname(os.path.normpath(sys.path[0])))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;assuming your script is directly in the app's &lt;CODE&gt;bin&lt;/CODE&gt; directory.&lt;/P&gt;

&lt;P&gt;It important that you calculate your paths relative to the current script location and not &lt;CODE&gt;SPLUNK_HOME&lt;/CODE&gt; 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 &lt;EM&gt;not&lt;/EM&gt; assume the app base is in &lt;CODE&gt;$SPLUNK_HOME/etc/apps&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2010 23:48:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44050#M535</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2010-09-04T23:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: scripted input python variables</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44051#M536</link>
      <description>&lt;P&gt;&lt;CODE&gt;sys.path[0]&lt;/CODE&gt; 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 &lt;CODE&gt;sys.argv[0]&lt;/CODE&gt;.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2010 23:53:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44051#M536</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2010-09-04T23:53:51Z</dc:date>
    </item>
    <item>
      <title>Re: scripted input python variables</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44052#M537</link>
      <description>&lt;P&gt;Great. I'll go with this to C.Y.A. Not sure how it could be used in a dist environment, but better safe than sorry.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Sep 2010 01:09:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/scripted-input-python-variables/m-p/44052#M537</guid>
      <dc:creator>caphrim007</dc:creator>
      <dc:date>2010-09-05T01:09:21Z</dc:date>
    </item>
  </channel>
</rss>

