Getting Data In

How to trigger script with arguments from Splunk search?

koshyk
Super Champion

I was able to make splunk send alert to my abc_pythonscript correctly after configuring commands.conf

| makeresults | eval myfield="some_raw_message" | table myfield| script abc_pythonscript myfield

But when I look into the arguments coming to my script, is myfield rather than the value within it. I was expecting the value of "some_raw_message" to be passed to my script.

Is it a mistake I'm doing or any better ways to do this?

1 Solution

woodcock
Esteemed Legend

Like this:

 | makeresults | eval myfield="some_raw_message" |map search="| makeresults | abc_pythonscript $myfield$"

View solution in original post

highsplunker
Contributor

This worked for me:

step 1. in search box of web interface
| makeresults | jonsnow dest="bbb", source="bla-bla-bla"

step 2. for "jonsnow" custom command to work
write this in commands.conf
[jonsnow]
filename = jonsnow.py

and this to local.meta of the same Splunk app

[commands/jonsnow]
access = read : [ * ], write : [ admin, power ]
export = system
owner = myusername

step 3. the script itself (jonsnow.py)
import splunk.Intersplunk
import subprocess
keywords, argvals = splunk.Intersplunk.getKeywordsAndOptions()
xxx = argvals['xxx']
yyy = argvals['yyy']
subprocess.call(["sh", "./jonsnow.sh", xxx, yyy])

0 Karma

vadivel_parames
Explorer

Hi Koshyk,

I have a similar requirement. Would be helpful if you could share the script how you are passing the splunk alert values into a python script.
Thanks!

woodcock
Esteemed Legend

You should really post a new question, but perhaps my (unaccepted) answer here helps?
https://answers.splunk.com/answers/41949/passing-search-results-to-external-python-script.html

0 Karma

koshyk
Super Champion

hi mate
1. create a python script (say my_python_script)
2. Ensure it has permissions to read from the app context
3. Run your base query and output results as you wish for your python script
4. and as answer given above pass those into a "map" command

0 Karma

woodcock
Esteemed Legend

Like this:

 | makeresults | eval myfield="some_raw_message" |map search="| makeresults | abc_pythonscript $myfield$"

highsplunker
Contributor

OK, worked too, thanks.
| makeresults | eval myfield="some_raw_message" |map search="| makeresults | abc_pythonscript $myfield$"

0 Karma

highsplunker
Contributor

Could you please share abc_pythonscript ?
See my answer below (worked).

0 Karma

koshyk
Super Champion

great thanks. worked like a charm

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 ...