<?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 to create a python script that generates an alert if the number of events returned from a search is 0? in Alerting</title>
    <link>https://community.splunk.com/t5/Alerting/How-to-create-a-python-script-that-generates-an-alert-if-the/m-p/133826#M2133</link>
    <description>&lt;P&gt;How are you calling this script?  Is it through scripted input?  I have similar requirement.  Please help. &lt;/P&gt;</description>
    <pubDate>Sun, 04 Jun 2017 08:57:12 GMT</pubDate>
    <dc:creator>k_harini</dc:creator>
    <dc:date>2017-06-04T08:57:12Z</dc:date>
    <item>
      <title>How to create a python script that generates an alert if the number of events returned from a search is 0?</title>
      <link>https://community.splunk.com/t5/Alerting/How-to-create-a-python-script-that-generates-an-alert-if-the/m-p/133824#M2131</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;

&lt;P&gt;I'm new to splunk sdk so, forgive me if my question is obvious.&lt;BR /&gt;
I'm trying to create a python script that runs a search job every minutes and give an alert if the number of events is 0.&lt;/P&gt;

&lt;P&gt;I'm starting with the connection: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunklib.client as client

HOST = "localhost"
PORT = 8089
USERNAME = "admin"
PASSWORD = "changeme"

//Create a Service instance and log in 
service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)

myquery = "*"
mysearchname = "hello"

//Check if this already exist
//mysavedsearch = service.saved_searches.create(mysearchname, myquery)
mysavedsearch = service.saved_searches["hello"]

kwargs = {"description": "This is a test search",
        "is_scheduled": True,
        "cron_schedule": "*/5 * * * *  ",}

mysavedsearch.update(**kwargs).refresh()

print "Description:         ", mysavedsearch["description"]
print "Is scheduled:        ", mysavedsearch["is_scheduled"]
print "Cron schedule:       ", mysavedsearch["cron_schedule"]
print "Next scheduled time: ", mysavedsearch["next_scheduled_time"]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Ok but how do I set the parameters of the alert? &lt;BR /&gt;
Could someone confirm if this is correct?&lt;/P&gt;

&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
      <pubDate>Mon, 09 Feb 2015 15:23:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/How-to-create-a-python-script-that-generates-an-alert-if-the/m-p/133824#M2131</guid>
      <dc:creator>Federica_92</dc:creator>
      <dc:date>2015-02-09T15:23:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a python script that generates an alert if the number of events returned from a search is 0?</title>
      <link>https://community.splunk.com/t5/Alerting/How-to-create-a-python-script-that-generates-an-alert-if-the/m-p/133825#M2132</link>
      <description>&lt;P&gt;In the end, this is my code and work &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunklib.client as client


HOST = "localhost"
PORT = 8089
USERNAME = "admin"
PASSWORD = "changeme"

# Create a Service instance and log in 
service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)

# Variables
myquery = "index=main | transaction dc(host) by host"
mysearchname = "h1"
a=0

# Create the search
savedsearches = service.saved_searches
for savedsearch in savedsearches:
    if (savedsearch.name=="h1"): a=1
if a==0: mysavedsearch = service.saved_searches.create(mysearchname, myquery)

# Edit the search
mysavedsearch = service.saved_searches["h1"]
kwargs = {"description": "This is a search",
        "is_scheduled": True,
        "cron_schedule": "*/1 * * * *  ",
    "alert.track":1,
    "alert_comparator":"greater than",
    "alert_type":"number of events",
    "alert_threshold":0,
    "alert.severity":5,
        }

mysavedsearch.update(**kwargs).refresh()

print "Description:         ", mysavedsearch["description"]
print "Is scheduled:        ", mysavedsearch["is_scheduled"]
print "Cron schedule:       ", mysavedsearch["cron_schedule"]
print "Next scheduled time: ", mysavedsearch["next_scheduled_time"]
print "Alert track      ", mysavedsearch["alert.track"]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Feb 2015 12:34:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/How-to-create-a-python-script-that-generates-an-alert-if-the/m-p/133825#M2132</guid>
      <dc:creator>Federica_92</dc:creator>
      <dc:date>2015-02-10T12:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a python script that generates an alert if the number of events returned from a search is 0?</title>
      <link>https://community.splunk.com/t5/Alerting/How-to-create-a-python-script-that-generates-an-alert-if-the/m-p/133826#M2133</link>
      <description>&lt;P&gt;How are you calling this script?  Is it through scripted input?  I have similar requirement.  Please help. &lt;/P&gt;</description>
      <pubDate>Sun, 04 Jun 2017 08:57:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Alerting/How-to-create-a-python-script-that-generates-an-alert-if-the/m-p/133826#M2133</guid>
      <dc:creator>k_harini</dc:creator>
      <dc:date>2017-06-04T08:57:12Z</dc:date>
    </item>
  </channel>
</rss>

