<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Custom alert action is not working in Alerting</title>
    <link>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585871#M13508</link>
    <description>&lt;P&gt;Let me quote the important part:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;args&lt;/EM&gt; is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either &lt;EM&gt;shell&lt;/EM&gt; must be &lt;A title="True" href="https://docs.python.org/3/library/constants.html#True" target="_blank" rel="noopener"&gt;&lt;SPAN class=""&gt;True&lt;/SPAN&gt;&lt;/A&gt; (see below) or else the string must simply name the program to be executed without specifying any arguments.&lt;/P&gt;</description>
    <pubDate>Sun, 20 Feb 2022 19:16:05 GMT</pubDate>
    <dc:creator>PickleRick</dc:creator>
    <dc:date>2022-02-20T19:16:05Z</dc:date>
    <item>
      <title>Custom alert action is not working</title>
      <link>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585868#M13505</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Snapshot&lt;/STRONG&gt;: I had some alerts with script actions. Alerts are simply if value A exceeds value B by 10 more e.g. value A=411 and value B=400 then trigger that alert and trigger the respective script alert is pointed to. Whenever alerts were triggered, specific shell scripts were triggered. Script were simply a shell command . Whenever those command/s ran, they fed alerts to event pipeline of another tool. It worked perfectly fine as designed until we upgraded Splunk from 7.x to 8.x. After upgrade, Script actions option is still available but does not work anymore since functionality has been deprecated.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Alternative&lt;/STRONG&gt;: Suggested functionality to use is custom alert actions from Splunk. I have developed a TA using Splunk Add On builder. This allows users to create same alerts where Action section allows them to select the script of choice to attach with alert so whenever alert is triggered, script is trigged and event is initiated as before up tp event pipeline of another tool. This TA has a python script which simply call the shell scripts of choice if a condition is met for respective script.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Issue/s:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;1. When same shell command that in the script/s is ran manually from one of the Search Heads command line, it works perfectly. This is for testing to make sure it works.&lt;/P&gt;&lt;P&gt;2. When same shell command that in the script/s is ran via Splunk Add On Builder @ validation section, it gives syntax error for "-" in the command. When I escaped it with &lt;STRONG&gt;\&lt;/STRONG&gt; using "&lt;STRONG&gt;\-&lt;/STRONG&gt;", syntax error resolves and test runs fine with success message. However, event that is supposed to be injected into another tools event pipeline, that event does not come at all.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;3. When same shell command that in the script/s is ran WITHOUT "\-options" flags, it works. This is just for testing to make sure the most basic functionality is working.&lt;/P&gt;&lt;P&gt;So what can I do to make this python script run the commands we want it to run to restore functionality which we had before via scripted alerts actions? I am suspecyting issue with Python. FYI, these scripts with shell commands and python script to trigger them are all in one package under same TA. Alerts are in a different application and are globally shared as always.&lt;/P&gt;&lt;P&gt;We are on Splunk 8.x. Python is upgraded via Splunk upgrade to 3.x. I am suspecting issue with Python engine and libraries. I did some libraries check and some libraries are still @ 2.7 even after the upgrade.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;# encoding = utf-8&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;from __future__ import print_function&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;from future import standard_library&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;standard_library.install_aliases()&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;import requests&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;import sys,os&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;import json&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;import logging&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;import logging.handlers&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;import subprocess&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;from subprocess import Popen, PIPE&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;def process_event(helper, *args, **kwargs):&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;helper.set_log_level(helper.log_level)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;dropdown_list = helper.get_param("dropdown_list")&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;helper.log_info("dropdown_list={}".format(dropdown_list))&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;helper.log_info("Alert action alert_action_test started.")&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;# Basic test command. Working and making it thru the event messaging queue&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;if dropdown_list == 'abcscript.sh':&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;execute=subprocess.run("/opt/OV/bin/opcmsg application=ABC object=ABC severity=Major msg_text=TESTTESTTEST, stdout=subprocess.PIPE, text=True)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;# Working syntax wise without any errors in Add On builder but not making it thru the messaging queue as desired for custom alert actions. Note: there is an option Description below that I need to take out to make it work because there is a syntax error with it because python is not aking values with spaces. (\-option Description&amp;gt;&amp;gt;&amp;gt;&amp;gt;)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;if dropdown_list == 'abcscript.sh':&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;execute=subprocess.run("/opt/OV/bin/opcmsg application=ABC object=ABC severity=Major msg_t=TEST \-option CIHint=ABC_Abstract_Node \-option ETIHint=ABCPortal:Major \-option Description='The rate of SendSubmission failures to successes exceeded the threshold in the JVM logs' \-option category=ABC_JVM \-option subcategory=SendSubmissionRate", stdout=subprocess.PIPE, text=True)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;# There are 4 scripts in total so I have three more drop downs and 3 more commands respectively like above.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thanks in-advance!!!&lt;/P&gt;</description>
      <pubDate>Sun, 20 Feb 2022 15:42:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585868#M13505</guid>
      <dc:creator>mbasharat</dc:creator>
      <dc:date>2022-02-20T15:42:31Z</dc:date>
    </item>
    <item>
      <title>Re: Custom alert action is not working</title>
      <link>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585869#M13506</link>
      <description>&lt;P&gt;You're calling subprocess.run incorrectly. See &lt;A href="https://docs.python.org/3/library/subprocess.html#subprocess.run" target="_blank"&gt;https://docs.python.org/3/library/subprocess.html#subprocess.run&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Feb 2022 16:44:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585869#M13506</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2022-02-20T16:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: Custom alert action is not working</title>
      <link>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585870#M13507</link>
      <description>&lt;P&gt;Hi PickleRick,&lt;/P&gt;&lt;P&gt;I am having difficulty getting it so can you please tell me what is incorrect and how it needs to be?&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 20 Feb 2022 18:42:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585870#M13507</guid>
      <dc:creator>mbasharat</dc:creator>
      <dc:date>2022-02-20T18:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: Custom alert action is not working</title>
      <link>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585871#M13508</link>
      <description>&lt;P&gt;Let me quote the important part:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;args&lt;/EM&gt; is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either &lt;EM&gt;shell&lt;/EM&gt; must be &lt;A title="True" href="https://docs.python.org/3/library/constants.html#True" target="_blank" rel="noopener"&gt;&lt;SPAN class=""&gt;True&lt;/SPAN&gt;&lt;/A&gt; (see below) or else the string must simply name the program to be executed without specifying any arguments.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Feb 2022 19:16:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585871#M13508</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2022-02-20T19:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: Custom alert action is not working</title>
      <link>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585877#M13509</link>
      <description>&lt;P&gt;I have tried three variations below after reading. Still no luck.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;# Method 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;if dropdown_list == 'msb_get_new_acks_submission.sh':&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;subprocess.run(["/opt/OV/bin/opcmsg", "application=ABC", "object=ABC", "severity=Major", "msg_t=TEST", "-option", "CIHint=ABC-A2A_Abstract_Node", "-option", "ETIHint=TESTABCPortal_ABC:Major", "-option", "Description='The rate of SendSubmission failures to successes exceeded the threshold in the ABC IEP A2A JVM logs'", "-option", "category=ABC_A2A_JVM", "-option", "subcategory=SendSubmissionRate"], stdout=subprocess.PIPE, text=True)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;# Method 2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;if dropdown_list == 'msb_get_new_acks_submission.sh':&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;execute=subprocess.run('/opt/OV/bin/opcmsg application=ABC object=ABC severity=Major msg_t=TEST -option CIHint=ABC-A2A_Abstract_Node -option ETIHint=TESTABCPortal_ABC:Major -option category=ABC_A2A_JVM -option subcategory=SendSubmissionRate', shell=True, stdout=subprocess.PIPE, text=True, check=True)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;# Method 3, command is what I defined just to reduce the clutter.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if dropdown_list == 'msb_get_new_acks_submission.sh':&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; res = subprocess.call('command', shell=True)&lt;/P&gt;</description>
      <pubDate>Sun, 20 Feb 2022 22:31:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585877#M13509</guid>
      <dc:creator>mbasharat</dc:creator>
      <dc:date>2022-02-20T22:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: Custom alert action is not working</title>
      <link>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585890#M13510</link>
      <description>&lt;P&gt;OK. Your first and second form seem to be relatively OK (they should indeed spawn the subprocess unless you pointed your script to a wrong path.&lt;/P&gt;&lt;P&gt;But if this is your whole script, and you counted on subprocess.run returining the output from the process to the calling splunk daemon, it won't work becaues you set stdout=subprocess.PIPE.&lt;/P&gt;&lt;P&gt;So either print directly from&amp;nbsp; subprocess.run() (don't capture the output) - kinda ugly solution. Or capture and then print it yourself.&lt;/P&gt;&lt;P&gt;And most importantly for any such development - add some loging layer so you know what's happening inside your script!&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 07:35:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585890#M13510</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2022-02-21T07:35:32Z</dc:date>
    </item>
    <item>
      <title>Re: Custom alert action is not working</title>
      <link>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585930#M13511</link>
      <description>&lt;P&gt;Logging is enabled in the code + TA&amp;nbsp;@ DEBUG level from beginning. I do not see any errors/concerns when code executes. Scripts are sitting in the bin/scripts.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;When you say, "So either print directly from&amp;nbsp; subprocess.run() (don't capture the output) - kinda ugly solution. Or capture and then print it yourself." Can you correct the python and provide me the correct one pls? I am at the dead end after trying many things. Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 14:53:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585930#M13511</guid>
      <dc:creator>mbasharat</dc:creator>
      <dc:date>2022-02-21T14:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: Custom alert action is not working</title>
      <link>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585940#M13512</link>
      <description>&lt;P&gt;OK. Let me show you what I mean.&lt;/P&gt;&lt;P&gt;I create a file named test.py with this inside:&lt;/P&gt;&lt;PRE&gt;#!/usr/bin/env python&lt;BR /&gt;&lt;BR /&gt;import subprocess&lt;BR /&gt;&lt;BR /&gt;print ("Don't capture")&lt;BR /&gt;a=subprocess.run(("ls"))&lt;BR /&gt;print ("Done")&lt;BR /&gt;print ("Capture")&lt;BR /&gt;a=subprocess.run(("ls"),capture_output=True)&lt;BR /&gt;print ("Done")&lt;BR /&gt;print ("Captured")&lt;BR /&gt;print (a)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;If I run it, I get:&lt;/P&gt;&lt;PRE&gt;$ ./test.py &lt;BR /&gt;Don't capture&lt;BR /&gt;test.py&lt;BR /&gt;Done&lt;BR /&gt;Capture&lt;BR /&gt;Done&lt;BR /&gt;Captured&lt;BR /&gt;CompletedProcess(args='ls', returncode=0, stdout=b'test.py\n', stderr=b'')&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;As you can see - the first call to subprocess.run() "bypassed" my python script and directly produced output on the stdout. The second one did not. But in the second case, the command's output was captured into a.stdout, from where I can use it - for example print by print(a.stdout).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 16:55:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Custom-alert-action-is-not-working/m-p/585940#M13512</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2022-02-21T16:55:46Z</dc:date>
    </item>
  </channel>
</rss>

