March 26th, 2008
The Splunk Python client library (part 1)
Splunk 3.2 introduces a publicly available Python client library that allows external developers to programmatically interact with Splunk by importing a few key modules.
The easiest way to get started with the client library is to get into Splunk’s Python environment. Locate your Splunk install directory (/opt/splunk by default), and start the python interactive shell that comes with Splunk:
# bin/splunk cmd python
This will launch the interactive Python prompt, which starts off looking like this:
Python 2.5.1 (r251:54863, Nov 18 2007, 16:13:41)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Starting a search
Import the Splunk modules:
import splunk.auth
import splunk.search as se
If you have installed Splunk with the default settings, then your hostpath is https://localhost:8089. The client library knows this default, so you can authenticate directly by providing a username and password:
key = splunk.auth.getSessionKey('admin','changeme')
The getSessionKey method automatically caches the session key in the current interactive session, so you don’t have to pass it along to subsequent methods. In a production implementation, or if you are connecting to multiple servers, you’ll need to keep track of separate session keys.
If your server is on a different hostname or port, then you need to first update the session defaults:
splunk.mergeHostPath('splunk_hostname:12000', True)
key = splunk.auth.getSessionKey('admin','changeme')
The mergeHostPath method takes host information in many different forms:
- hostname
- hostname:port
- https://hostname
- http://hostname:port
Next, start a search:
job = se.dispatch('search error')
This creates a search job handle object job and start a running search on the server for events that contain the term “error”. If you are connecting to multiple servers, then you’ll also need to provide hostPath and sessionKey parameters as well. This handle is keyed off of the search job ID that is generated by the server, and is available via:
job.id
With this ID, you can always use



