<?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: How do i get the parameters passed to the script in Alert action in Alerting</title>
    <link>https://community.splunk.com/t5/Alerting/How-do-i-get-the-parameters-passed-to-the-script-in-Alert-action/m-p/187928#M3141</link>
    <description>&lt;P&gt;Are you using Python in Windows? That's not how you use os.environ in Python. You don't specify the environment variable with a leading '$'.&lt;/P&gt;

&lt;PRE&gt;
[shaskell@docker ~]$ export FOO=la
[shaskell@docker ~]$ python
Python 2.7.5 (default, Jun 24 2015, 00:41:19) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; import os
&amp;gt;&amp;gt;&amp;gt; print(os.environ['FOO']);
la
&lt;/PRE&gt;

&lt;P&gt;You'd only access the argument like &lt;B&gt;$SPLUNK_ARG_0&lt;/B&gt; if it were from a shell script in Unix or &lt;B&gt;%SPLUNK_ARG_0%&lt;/B&gt; if it were from a batch file in Windows.&lt;/P&gt;

&lt;P&gt;You can also access the arguments positionally in Python. Given the following script:&lt;/P&gt;

&lt;PRE&gt;
#!/usr/bin/env python
import sys
print(sys.argv[0])
print(sys.argv[1])
&lt;/PRE&gt;

&lt;PRE&gt;
[shaskell@docker ~]$ ./foo.py arg1
./foo.py
arg1
&lt;/PRE&gt;

&lt;P&gt;So sys.argv[0] is $SPLUNK_ARG_0 (name of the script) and so on. Also note that the 9th argument is the Splunk session key in case you want to interact with the REST API from your script.&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 09:55:08 GMT</pubDate>
    <dc:creator>shaskell_splunk</dc:creator>
    <dc:date>2020-09-29T09:55:08Z</dc:date>
    <item>
      <title>How do i get the parameters passed to the script in Alert action</title>
      <link>https://community.splunk.com/t5/Alerting/How-do-i-get-the-parameters-passed-to-the-script-in-Alert-action/m-p/187925#M3138</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I am working on the alerting module of the Splunk and would like to execute a script when alert gets triggered and from the documents I see that &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.0.2/alert/ConfiguringScriptedAlerts" target="_blank"&gt;http://docs.splunk.com/Documentation/Splunk/6.0.2/alert/ConfiguringScriptedAlerts&lt;/A&gt; &lt;/P&gt;

&lt;P&gt;when alerts gets triggered, it will pass some set of arguments to the script. Is my understanding correct? If my understanding is wrong kindly guide me with proper meaning.&lt;/P&gt;

&lt;P&gt;Assuming the above understanding is correct, to know the value of each of the parameter, I access it by using: os.environ['SPLUNK_ARG_0'] and so on in the Python scripting correct?&lt;/P&gt;

&lt;P&gt;NOTE: I am trying out this in the Windows machine, so do I need to use os.environ['$SPLUNK_ARG_0'] instead of os.environ['SPLUNK_ARG_0']?? &lt;/P&gt;

&lt;P&gt;Please let me know if I need to explicitly set anything so as to pass the arguments from the Splunk to the script.&lt;/P&gt;

&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 16:08:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/How-do-i-get-the-parameters-passed-to-the-script-in-Alert-action/m-p/187925#M3138</guid>
      <dc:creator>nithin_shubhana</dc:creator>
      <dc:date>2020-09-28T16:08:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do i get the parameters passed to the script in Alert action</title>
      <link>https://community.splunk.com/t5/Alerting/How-do-i-get-the-parameters-passed-to-the-script-in-Alert-action/m-p/187926#M3139</link>
      <description>&lt;P&gt;The arguments are always passed and there is nothing you have to do special.  Also, make sure your alert script in python starts with a hash bang that points to the python for splunk appropriate to your path on your windows installation.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Mar 2014 14:50:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/How-do-i-get-the-parameters-passed-to-the-script-in-Alert-action/m-p/187926#M3139</guid>
      <dc:creator>starcher</dc:creator>
      <dc:date>2014-03-16T14:50:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do i get the parameters passed to the script in Alert action</title>
      <link>https://community.splunk.com/t5/Alerting/How-do-i-get-the-parameters-passed-to-the-script-in-Alert-action/m-p/187927#M3140</link>
      <description>&lt;P&gt;I downvoted this post because please try batch script with some parameter as you have described. it doesn't run with splunk  while it is totaly fine on cmd.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2016 20:37:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/How-do-i-get-the-parameters-passed-to-the-script-in-Alert-action/m-p/187927#M3140</guid>
      <dc:creator>axl88</dc:creator>
      <dc:date>2016-06-08T20:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: How do i get the parameters passed to the script in Alert action</title>
      <link>https://community.splunk.com/t5/Alerting/How-do-i-get-the-parameters-passed-to-the-script-in-Alert-action/m-p/187928#M3141</link>
      <description>&lt;P&gt;Are you using Python in Windows? That's not how you use os.environ in Python. You don't specify the environment variable with a leading '$'.&lt;/P&gt;

&lt;PRE&gt;
[shaskell@docker ~]$ export FOO=la
[shaskell@docker ~]$ python
Python 2.7.5 (default, Jun 24 2015, 00:41:19) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; import os
&amp;gt;&amp;gt;&amp;gt; print(os.environ['FOO']);
la
&lt;/PRE&gt;

&lt;P&gt;You'd only access the argument like &lt;B&gt;$SPLUNK_ARG_0&lt;/B&gt; if it were from a shell script in Unix or &lt;B&gt;%SPLUNK_ARG_0%&lt;/B&gt; if it were from a batch file in Windows.&lt;/P&gt;

&lt;P&gt;You can also access the arguments positionally in Python. Given the following script:&lt;/P&gt;

&lt;PRE&gt;
#!/usr/bin/env python
import sys
print(sys.argv[0])
print(sys.argv[1])
&lt;/PRE&gt;

&lt;PRE&gt;
[shaskell@docker ~]$ ./foo.py arg1
./foo.py
arg1
&lt;/PRE&gt;

&lt;P&gt;So sys.argv[0] is $SPLUNK_ARG_0 (name of the script) and so on. Also note that the 9th argument is the Splunk session key in case you want to interact with the REST API from your script.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 09:55:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/How-do-i-get-the-parameters-passed-to-the-script-in-Alert-action/m-p/187928#M3141</guid>
      <dc:creator>shaskell_splunk</dc:creator>
      <dc:date>2020-09-29T09:55:08Z</dc:date>
    </item>
  </channel>
</rss>

