<?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 I use Splunk's built-in Python SDK in my own scripts? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9057#M4</link>
    <description>&lt;P&gt;According to the latest documentation, pydoc is now at:&lt;/P&gt;

&lt;P&gt;$SPLUNK_HOME/bin/splunk cmd $SPLUNK_HOME/lib/python2.6/pydoc.py -p 8080&lt;/P&gt;</description>
    <pubDate>Mon, 28 Sep 2020 09:13:12 GMT</pubDate>
    <dc:creator>esachs</dc:creator>
    <dc:date>2020-09-28T09:13:12Z</dc:date>
    <item>
      <title>Can I use Splunk's built-in Python SDK in my own scripts?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9054#M1</link>
      <description>&lt;P&gt;I have existing Python scripts that pull data from various sources.  I would like to use Splunk's built-in Python SDK layer in my own script so that I can run searches programmatically.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Nov 2009 03:17:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9054#M1</guid>
      <dc:creator>Johnvey</dc:creator>
      <dc:date>2009-11-10T03:17:36Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use Splunk's built-in Python SDK in my own scripts?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9055#M2</link>
      <description>&lt;P&gt;Yes, Splunk includes its own copy of Python along with modules that talk directly to the Splunk backend.  &lt;/P&gt;

&lt;H2&gt;Interactive Splunk Python prompt&lt;/H2&gt;

&lt;P&gt;If you have Splunk installed already, you can use the interactive Python interpreter to try out the various modules.&lt;/P&gt;

&lt;P&gt;1)  start the Python prompt&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$SPLUNK_HOME/bin/splunk cmd python
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;2)  import the required modules&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunk.auth, splunk.search
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;3)  obtain a session key&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;key = splunk.auth.getSessionKey('admin','changeme') # replace with your credentials
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The SDK will cache the session key, so you don't have to explicitly pass it while in the interactive prompt.&lt;/P&gt;

&lt;P&gt;4)  run a basic search&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;my_job = splunk.search.dispatch('search error | timechart span=1h count', namespace='search', earliest_time='-24h')
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This command will start a search job for all occurrences of the keyword &lt;CODE&gt;error&lt;/CODE&gt;, in the context of the &lt;CODE&gt;search&lt;/CODE&gt; app, and count them on a per-hour basis for data that occurred over the last 24 hours.  The handle to the job is represented by the &lt;CODE&gt;my_job&lt;/CODE&gt; object, which is a splunk.search.SearchJob class.&lt;/P&gt;

&lt;P&gt;5)  inspect the various job properties&lt;/P&gt;

&lt;P&gt;The &lt;CODE&gt;my_job&lt;/CODE&gt; object has a multitude of properties that describe the current job.  Examples are:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;gt;&amp;gt;&amp;gt; my_job.isDone 
True
&amp;gt;&amp;gt;&amp;gt; my_job.eventCount
13264
&amp;gt;&amp;gt;&amp;gt; my_job.resultCount
24
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The complete list of properties can be enumerated by printing the job object:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;gt;&amp;gt;&amp;gt; print my_job
createTime           2009-11-09T10:48:03.000-08:00
cursorTime           2009-09-09T01:40:20.000-07:00
delegate             None
doneProgress         1.0
dropCount            0
eai:acl              {'sharing': 'global', 'perms': {'read': ['admin'], 'write': ['admin']}, 'app': 'search', 'modifiable': 'true', 'can_write': 'true', 'owner': 'admin'}
earliestTime         2002-09-12T17:02:52.000-07:00
eventAvailableCount  100
eventCount           100
eventFieldCount      17
eventIsStreaming     True
eventIsTruncated     False
eventSearch          search error  | head 100
eventSorting         desc
isDone               True
isFailed             False
isFinalized          False
isPaused             False
isRealTimeSearch     False
isSaved              False
isSavedSearch        False
isZombie             False
keywords             error
label                None
latestTime           2009-09-09T01:40:21.000-07:00
messages             {}
modifiedTime         2009-11-09T10:48:03.000-08:00
priority             5
remoteSearch         litsearch error | fields keepcolorder=t * | prehead limit=100 null=false keeplast=false
reportSearch         None
request              {'search': 'search error | head 100'}
resultCount          100
resultIsStreaming    True
resultPreviewCount   100
runDuration          3.38
scanCount            2803
search               search error | head 100
searchProviders      ['decider.local-johnvey']
sid                  1257792483.115
statusBuckets        300
ttl                  600
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;6)  get the raw events&lt;/P&gt;

&lt;P&gt;You can get the raw events that the index returned by:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;gt;&amp;gt;&amp;gt; for event in my_job.events: print event
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;7)  get the transformed results&lt;/P&gt;

&lt;P&gt;Since the search command contains &lt;CODE&gt;timechart&lt;/CODE&gt;, a transforming command, the relevant summarized data is contained in the &lt;CODE&gt;results&lt;/CODE&gt; property:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;gt;&amp;gt;&amp;gt; for result in my_job.results: print result
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In this iterator, the &lt;CODE&gt;result&lt;/CODE&gt; object houses detailed information about each result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;gt;&amp;gt;&amp;gt; result0 = my_job.results[0]
&amp;gt;&amp;gt;&amp;gt; result0.time
'2009-09-09T01:40:21-0700'
&amp;gt;&amp;gt;&amp;gt; result0.fields
{'count': 100, '_time': 2009-09-09T01:40:21-0700}
&amp;gt;&amp;gt;&amp;gt; result0['count']
100
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt;  clean up&lt;/P&gt;

&lt;P&gt;Every search job executed will retain its data for a period of time (defined by the &lt;CODE&gt;ttl&lt;/CODE&gt; dispatch property).  When you are finished with the job, mark the job for removal:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;gt;&amp;gt;&amp;gt; my_job.cancel()
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H2&gt;Scripting against the built-in Python SDK&lt;/H2&gt;

&lt;P&gt;You can write custom Python scripts that use the built-in SDK by executing them in the Splunk environment.  Assuming that you have a script called 'my_searcher.py':&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$ cd $SPLUNK_HOME/bin
$ vi my_searcher.py # create your scripts
$ splunk cmd python my_searcher.py
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;H2&gt;Installing the Python SDK on an server that doesn't have Splunk&lt;/H2&gt;

&lt;P&gt;Currently, there isn't a packaging script to easily bring the necessary Splunk Python components to a standalone machine.  However, those that are familiar with Python can manually setup such an environment.&lt;/P&gt;

&lt;P&gt;Splunk Python modules:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$SPLUNK_HOME/lib/python2.6/python-site/splunk/
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Python dependencies:&lt;/P&gt;

&lt;P&gt;1)  python 2.5+
2)  lxml: &lt;A href="http://codespeak.net/lxml/" rel="nofollow"&gt;http://codespeak.net/lxml/&lt;/A&gt;
3)  httplib2: &lt;A href="http://code.google.com/p/httplib2/" rel="nofollow"&gt;http://code.google.com/p/httplib2/&lt;/A&gt;&lt;/P&gt;

&lt;H2&gt;Generating SDK documentation&lt;/H2&gt;

&lt;P&gt;You can generate documentation on the various Splunk modules by running &lt;CODE&gt;pydoc&lt;/CODE&gt; within the Splunk environment:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$ $SPLUNK_HOME/bin/splunk cmd pydoc -p 8800
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This will start a local webserver that will serve the code documentation for Splunk.  Under the &lt;CODE&gt;site-packages&lt;/CODE&gt; header there is a link for `splunk', which contains the entire SDK tree.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Nov 2009 03:17:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9055#M2</guid>
      <dc:creator>Johnvey</dc:creator>
      <dc:date>2009-11-10T03:17:53Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use Splunk's built-in Python SDK in my own scripts?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9056#M3</link>
      <description>&lt;P&gt;Is pydoc part of all Splunk distributions? I'm running a 4.0.9 indexer on RHEL5.3 x86_64, as installed by the RPM, and get this when trying to generate the docs:&lt;/P&gt;

&lt;P&gt;$ /opt/splunk/bin/splunk cmd pydoc -p 8800&lt;BR /&gt;
couldn't run "/opt/splunk/bin/pydoc": No such file or directory&lt;/P&gt;

&lt;P&gt;Indeed, it doesn't exist:&lt;/P&gt;

&lt;P&gt;$ ls -l /opt/splunk/bin/p*&lt;BR /&gt;
parsetest    pcregextest  python       python2.6&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2010 16:49:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9056#M3</guid>
      <dc:creator>Glenn</dc:creator>
      <dc:date>2010-03-26T16:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use Splunk's built-in Python SDK in my own scripts?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9057#M4</link>
      <description>&lt;P&gt;According to the latest documentation, pydoc is now at:&lt;/P&gt;

&lt;P&gt;$SPLUNK_HOME/bin/splunk cmd $SPLUNK_HOME/lib/python2.6/pydoc.py -p 8080&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 09:13:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9057#M4</guid>
      <dc:creator>esachs</dc:creator>
      <dc:date>2020-09-28T09:13:12Z</dc:date>
    </item>
    <item>
      <title>Re: Can I use Splunk's built-in Python SDK in my own scripts?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9058#M5</link>
      <description>&lt;P&gt;There is also a new Splunk Python SDK on GitHub.  You can access it here:  &lt;A href="https://github.com/splunk/splunk-sdk-python"&gt;https://github.com/splunk/splunk-sdk-python&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Any questions - &lt;A href="mailto:psanford@splunk.com"&gt;psanford@splunk.com&lt;/A&gt; or ping us on Twitter:  @splunkdev  &lt;/P&gt;</description>
      <pubDate>Wed, 28 Sep 2011 16:59:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Can-I-use-Splunk-s-built-in-Python-SDK-in-my-own-scripts/m-p/9058#M5</guid>
      <dc:creator>psanford_splunk</dc:creator>
      <dc:date>2011-09-28T16:59:25Z</dc:date>
    </item>
  </channel>
</rss>

