Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

import of old splunk code

  • Loading branch information...
commit 4b1b226d925c7147eadf81ccb68ce6b162be496d 0 parents
@JustinAzoff authored
Showing with 66 additions and 0 deletions.
  1. +66 −0 splunky/splunky.py
66 splunky/splunky.py
@@ -0,0 +1,66 @@
+import lxml.etree as et
+import httplib2
+import urllib
+import simplejson
+import time
+
+NS_MAP = {
+ 'atom': 'http://www.w3.org/2005/Atom',
+ 'splunk': 'http://dev.splunk.com/ns/rest'
+}
+
+class Server:
+ def __init__(self, host, port=8089):
+ self.host = host
+ self.port = port
+
+ self.h = httplib2.Http()
+
+ def do(self, method, endpoint, data=None):
+ d = data and urllib.urlencode(data) or ''
+ url = "https://%s:%s/services/%s" % (self.host, self.port, endpoint)
+
+ resp, content = self.h.request(url, method, body=d)
+ return content
+
+ def post_xml(self, endpoint, data):
+ d = self.do('POST', endpoint, data)
+ return et.fromstring(d)
+
+ def get_json(self, endpoint, data):
+ d = data and urllib.urlencode(data) or ''
+ d = self.do('GET', endpoint + '?' + d)
+ return simplejson.loads(d)
+
+ def get_xml(self, endpoint, data=None):
+ d = data and urllib.urlencode(data) or ''
+ d = self.do('GET', endpoint + '?' + d)
+ return et.fromstring(d)
+
+ def search(self, q, **kwargs):
+ data={'search': 'search ' + q}
+ data.update(kwargs)
+ data = self.post_xml("search/jobs", data)
+ return data.findtext("sid")
+
+ def results(self, sid, **kwargs):
+ d = {'output_mode': 'json'}
+ d.update(kwargs)
+ return self.get_json("search/jobs/%s/results" % sid, d)
+
+ def status(self, sid):
+ x = self.get_xml("search/jobs/%s" % sid)
+ r = {}
+ for k in x.findall('{%(atom)s}content/{%(splunk)s}dict/{%(splunk)s}key' % NS_MAP):
+ name = k.get("name")
+ r[name] = k.text
+ return r
+
+ def search_sync(self, q, **kwargs):
+ sid = self.search(q, **kwargs)
+ done = False
+ while not done:
+ s = self.status(sid)
+ done = int(s['isDone'])
+ time.sleep(0.2)
+ return self.results(sid, **kwargs)
Please sign in to comment.
Something went wrong with that request. Please try again.