Alerting

How to debug Splunk alert scripts?

markb_1984
New Member

I have a script which takes all the 8 splunk args, extracts the csv containing the event details and then does some transformation before sending an alert to an IM.

To improve its reusability, I split out various parts of the script into separate shell scripts all located within a subdirectory of the main script splunk executes. Having completed is refactoring, I have successfully executed the main script passing in dummy (but sensible) values for all the expected args. Splunk is however is not successfully executing this main script.

I have followed most of the suggestions in the following wiki to no avail: http://wiki.splunk.com/Community:TroubleshootingAlertScripts

Increasing the logging to error in runshellscript.py does then reveal an error entry (in python.log) for my script when the alert fires. The error however simply outputs all the args that are passed down to the shell script. No other error is reported in this log.

Is there anywhere else I can look to see errors that might be emanating from splunk executing my script?

0 Karma

sjscott
Explorer

The easiest way to debug your scripts is to put a launcher in front of it. The idea is to redirect stdout and stderr to a file when the script runs. Thus allowing you to see what's going on. Below is what I use on RedHat for launching a python script.

#!/bin/bash
#
#  We need to lauch the python script from Bash so we can use the python version within the OS.  This
#  allows us to install the python modules we need from the package manager of the OS.   The python
#  that comes with Splunk most likely won't have the modules you require.  Thus we use the one from
#  the OS.
#
# Debug
# Use the following line to debug.  send stdout stderr to the file specified below
/usr/bin/python /opt/splunk/bin/scripts/<YOURSCRIPT> "$@" >>/var/tmp/file.txt 2>&1

# production
#/usr/bin/python /opt/splunk/bin/scripts/<YOURSCRIPT> "$@"

The "$@" takes the args that were passed to the launcher and re-passes them to the real script. The secondary reason I use a launcher is I don't want to use the version of Python that comes built into Splunk.

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!

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

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...