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!

Best Practices: Splunk auto adjust pipeline queue

When you enable autoAdjustQueue in Splunk, maxSize should be understood as the queue size Splunk starts with ...

Laser Bananas and Edge Hubs: Exploring Operational Technology (OT) Data Through a ...

  OT is a different environment to traditional IT and can have interesting challenges when interfacing the ...

Event Series: Mastering AI Tokenomics and Splunk Agent Observability

Beyond the Black Box: Correlating AI Performance and Tokenomics with Splunk Agent Observability   As ...