Splunk Dev

How do I translate a GUI search query into a python API query?

lfast12
Explorer

newbie question...
I'd like to know the appropriate Python API syntax for the following GUI query:

source="/var/log/my-test.log" index="testing" sourcetype="_json"

I get a good set of results running it in the GUI but I'm always getting 0 results from the API. I'm working off the API docs with the following base code:

kwargs_blockingsearch = {"exec_mode": "blocking"}
searchquery_blocking = "search * | head 10"
job = jobs.create(searchquery_blocking, **kwargs_blockingsearch)
so...
what's the correct syntax for replacing "search *|head 10" with my query?

Labels (1)
Tags (2)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@lfast12

Just replace it with your search.

searchquery_blocking = 'search source="/var/log/my-test.log" index="testing" sourcetype="_json" '

OR

searchquery_blocking = "search source=\"/var/log/my-test.log\" index=\"testing\" sourcetype=\"_json\" '

Check section To create a blocking search and display properties of the job in below link.

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

lfast12
Explorer

Thanks Kamlesh,
That didn't work but it put me on the right track. Here's what finally worked:

kwargs_blockingsearch = {"exec_mode": "blocking"}
searchquery_blocking = 'search source="/var/log/splunk-test.log" index="testing" sourcetype="_json"'
job = jobs.create(searchquery_blocking, **kwargs_blockingsearch)

The only difference is prefixing your suggestion with 'search ...'

Simple enough but when there are too many choices you can easily go down the wrong path. Thanks again.

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@lfast12

Yes. I missed adding the search command. Updated answer. When we execute any search from Python or CURL we need to add search initially which is not required in GUI. Please upvote and accept this answer if it helps you.

Thanks

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...