Getting Data In

Not able to obtain search results via Splunk REST API

Path Finder

Hello,

I am using Splunk Python SDK to connect to Splunk via REST API.

However the code I have written does not return any results:

query = 'search index = test | stats sum(bytes) as bytes by ip'
kwargs_oneshot = {"earliest_time": "-2m","latest_time": "now","enable_lookups":True,"rf":["ip"]}
job = splunk_connection_service.jobs.create(query, **kwargs_oneshot)
while not job.is_done():
    time.sleep(.2)

for result in results.ResultsReader(job.results()):
    print (result)

If I remove "stats" command from the query, then I only get "_raw" data. What should i change in my code to get the "stats" command output?

Thank you.

0 Karma

SplunkTrust
SplunkTrust

@kiril123
I'm assumed your search is giving proper output on SH.

Have you tried reader?

import splunklib.results as results

...

# Run a one-shot search and display the results using the results reader

# Set the parameters for the search:
# - Search everything in a 24-hour time range starting June 19, 12:00pm
# - Display the first 10 results
kwargs_oneshot = {"earliest_time": "2014-06-19T12:00:00.000-07:00",
                  "latest_time": "2014-06-20T12:00:00.000-07:00"}
searchquery_oneshot = "search * | head 10"

oneshotsearch_results = service.jobs.oneshot(searchquery_oneshot, **kwargs_oneshot)

# Get the results and display them using the ResultsReader
reader = results.ResultsReader(oneshotsearch_results)
for item in reader:
    print(item)

http://dev.splunk.com/view/python-sdk/SP-CAAAER5

0 Karma

Path Finder

Yes, i tried that.
When I run 'query = search index = test' i get the output in raw format.
However when I run 'query = search index = test | stats sum(bytes) as bytes by ip' I get none.

Running the search directly on the SH works fine.

0 Karma

SplunkTrust
SplunkTrust

Can you please try this search in python?

index = test | table _time _raw bytes ip

I just want to know about the fields are there or not. If we didn't find any fields then need to check extraction of fields or app context from which this search is executing.

0 Karma