Getting Data In

Not able to obtain search results via Splunk REST API

kiril123
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

kamlesh_vaghela
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

kiril123
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

kamlesh_vaghela
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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Unlocking Unified Insights: New Gigamon Federated Search App for Splunk

In today’s data-heavy environment, organizations are caught in a data distribution dilemma. As data volumes ...

GA: New Data Management App in Splunk Platform

Streamlining Data Management: Introducing a unified experience in Splunk Managing data at scale shouldn’t feel ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...