<?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: Use an alert to trigger scripted input in Alerting</title>
    <link>https://community.splunk.com/t5/Alerting/Use-an-alert-to-trigger-scripted-input/m-p/98139#M1403</link>
    <description>&lt;P&gt;What we have found is that scripts for scripted alerts must live in $SPLUNK_HOME/bin/scripts, and they don't get passed the data directly. However, they are passed the data file of the data returned by the search in $8.&lt;/P&gt;

&lt;P&gt;So, you can write a launcher script which gets called by the alert, reads the file, and passes the appropriate values to the script you really want to run (such as the one in your app that creates additional input.)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/python

# Based on a script from &lt;A href="http://answers.splunk.com/quesrions/3019/scripted-alert-question" target="test_blank"&gt;http://answers.splunk.com/quesrions/3019/scripted-alert-question&lt;/A&gt;

import csv, gzip, sys
from subprocess import call

# Enter script location here. This will be called once per event returned by the Splunk
# search, with field1=value1 field2=value2 appended. Ignore fields starting with _
# (Ensure your scheduled search has a | fields -_* | fields x y at the end to ensure
# you get the fields you want going to your script)
scriptlocation = "/opt/splunk/etc/apps/demo/bin/demo.sh"


# The rest of this should not have to be configured
def openany(p):
    if p.endswith(".gz"):
        return gzip.open(p)
    else:
        return open(p)

event_count = int(sys.argv[1])  # number of events returned.
results_file = sys.argv[8]      # file with search results

for row in csv.DictReader(openany(results_file)):
    # Build a command line to call based on fields from splunk output
    my_command = [ scriptlocation ]
    for col in row:
        if col[0]!="_":
            my_command.append(col + '=' + row[col])
    call(my_command)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 28 Jan 2011 06:04:24 GMT</pubDate>
    <dc:creator>Jason</dc:creator>
    <dc:date>2011-01-28T06:04:24Z</dc:date>
    <item>
      <title>Use an alert to trigger scripted input</title>
      <link>https://community.splunk.com/t5/Alerting/Use-an-alert-to-trigger-scripted-input/m-p/98136#M1400</link>
      <description>&lt;P&gt;I have an input script which I would like to run based upon the results of another search.   Also, I need to send the results of the alert script to the script.    The results of the script creates another event which I want to correlate to the trigger event.   &lt;/P&gt;

&lt;P&gt;Perhaps alerts aren't the right mechanism.  Essentially, I'm running a script to gather more information on the event using data in the event as a parameter to the script.  Is there a better way to do this?&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2010 09:49:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Use-an-alert-to-trigger-scripted-input/m-p/98136#M1400</guid>
      <dc:creator>mpatnode</dc:creator>
      <dc:date>2010-12-02T09:49:49Z</dc:date>
    </item>
    <item>
      <title>Re: Use an alert to trigger scripted input</title>
      <link>https://community.splunk.com/t5/Alerting/Use-an-alert-to-trigger-scripted-input/m-p/98137#M1401</link>
      <description>&lt;P&gt;You might be able to have your script generate a file and put in into the Splunk batch directory, or send it to a network port on which Splunk is listening instead.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2010 10:21:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Use-an-alert-to-trigger-scripted-input/m-p/98137#M1401</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2010-12-02T10:21:44Z</dc:date>
    </item>
    <item>
      <title>Re: Use an alert to trigger scripted input</title>
      <link>https://community.splunk.com/t5/Alerting/Use-an-alert-to-trigger-scripted-input/m-p/98138#M1402</link>
      <description>&lt;P&gt;Another solution would be to run searches from your scripted input. If you're using python, you can use the splunk modules that ship with splunk. The trick is using passAuth = admin in your inputs.conf, then a session key is handed to your script on stdin.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunk.search

sessionKey = sys.stdin.readline()

job = splunk.search.dispatch('search foo', sessionKey=sessionKey)
splunk.search.waitForJob(job, maxtime=240)

if job.count &amp;gt; 0:
    foo = job.events[0]['foo']
else:
    foo = None
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Dec 2010 08:06:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Use-an-alert-to-trigger-scripted-input/m-p/98138#M1402</guid>
      <dc:creator>vbumgarn</dc:creator>
      <dc:date>2010-12-31T08:06:22Z</dc:date>
    </item>
    <item>
      <title>Re: Use an alert to trigger scripted input</title>
      <link>https://community.splunk.com/t5/Alerting/Use-an-alert-to-trigger-scripted-input/m-p/98139#M1403</link>
      <description>&lt;P&gt;What we have found is that scripts for scripted alerts must live in $SPLUNK_HOME/bin/scripts, and they don't get passed the data directly. However, they are passed the data file of the data returned by the search in $8.&lt;/P&gt;

&lt;P&gt;So, you can write a launcher script which gets called by the alert, reads the file, and passes the appropriate values to the script you really want to run (such as the one in your app that creates additional input.)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/python

# Based on a script from &lt;A href="http://answers.splunk.com/quesrions/3019/scripted-alert-question" target="test_blank"&gt;http://answers.splunk.com/quesrions/3019/scripted-alert-question&lt;/A&gt;

import csv, gzip, sys
from subprocess import call

# Enter script location here. This will be called once per event returned by the Splunk
# search, with field1=value1 field2=value2 appended. Ignore fields starting with _
# (Ensure your scheduled search has a | fields -_* | fields x y at the end to ensure
# you get the fields you want going to your script)
scriptlocation = "/opt/splunk/etc/apps/demo/bin/demo.sh"


# The rest of this should not have to be configured
def openany(p):
    if p.endswith(".gz"):
        return gzip.open(p)
    else:
        return open(p)

event_count = int(sys.argv[1])  # number of events returned.
results_file = sys.argv[8]      # file with search results

for row in csv.DictReader(openany(results_file)):
    # Build a command line to call based on fields from splunk output
    my_command = [ scriptlocation ]
    for col in row:
        if col[0]!="_":
            my_command.append(col + '=' + row[col])
    call(my_command)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Jan 2011 06:04:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/Use-an-alert-to-trigger-scripted-input/m-p/98139#M1403</guid>
      <dc:creator>Jason</dc:creator>
      <dc:date>2011-01-28T06:04:24Z</dc:date>
    </item>
  </channel>
</rss>

