<?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: Sendemail (Splunk CLI) always sends email whether results are available or not... in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Sendemail-Splunk-CLI-always-sends-email-whether-results-are/m-p/43615#M10258</link>
    <description>&lt;P&gt;Do it like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rename COMMENT1of3 AS "Splunk sendemail ALWAYS sends email, even when no results found; we address this with 2 settings:"
| rename COMMENT2of3 AS "First, we put 'null()' in 'to' header when no results; this causes 'sendemail' to error."
| rename COMMENT3of3 AS "Last, we use 'graceful=true' so that the search does not log any error for that."
| eval valueForToHeader=if(isnotnull(someFieldNameInYourResults), "YourGoodEmailGoesHere@YourCompany.com", null())
| sendemail
   to=$result.valueForToHeader$
   graceful=true
   ...
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 25 Jul 2018 21:14:19 GMT</pubDate>
    <dc:creator>woodcock</dc:creator>
    <dc:date>2018-07-25T21:14:19Z</dc:date>
    <item>
      <title>Sendemail (Splunk CLI) always sends email whether results are available or not...</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sendemail-Splunk-CLI-always-sends-email-whether-results-are/m-p/43612#M10255</link>
      <description>&lt;P&gt;I'm running the following search from Splunk CLI:&lt;/P&gt;

&lt;P&gt;./splunk search 'index=test | search _raw!="scoobydoo" | sendemail to="elvis@splunk.com,john@splunk.com" subject=myresults server=mail.splunk.com' -auth etc:pass&lt;/P&gt;

&lt;P&gt;The behavior I see is that an email is always sent whether or not results are returned by the search.  &lt;/P&gt;

&lt;P&gt;Is there some way to tell Splunk to only send email when there are results?&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2010 00:40:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sendemail-Splunk-CLI-always-sends-email-whether-results-are/m-p/43612#M10255</guid>
      <dc:creator>the_wolverine</dc:creator>
      <dc:date>2010-09-04T00:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: Sendemail (Splunk CLI) always sends email whether results are available or not...</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sendemail-Splunk-CLI-always-sends-email-whether-results-are/m-p/43613#M10256</link>
      <description>&lt;P&gt;No, Splunk doesn't provide per-result set branching logic in the search language.&lt;/P&gt;

&lt;P&gt;I would script this using the Python SDK:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import time
import splunk
import splunk.auth   as au
import splunk.search as se

splunk.mergeHostPath('localhost:4001', True)
key = au.getSessionKey('admin', 'changeme')

d = se.dispatch('search index=_internal | head 10')

while not d.isDone:
    time.sleep(1)

if d.resultCount &amp;gt; 0:
    d.setFetchOption(search='sendemail to=...@splunk.com from=...@splunk.com server=ip1.splunk.com subject=myresults sendresults=true')
    r = d.results[0]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can then run this via: &lt;CODE&gt;splunk cmd python &amp;lt;scriptname&amp;gt;.py&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;A shell script may be even easier.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2010 11:29:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sendemail-Splunk-CLI-always-sends-email-whether-results-are/m-p/43613#M10256</guid>
      <dc:creator>Stephen_Sorkin</dc:creator>
      <dc:date>2010-09-04T11:29:16Z</dc:date>
    </item>
    <item>
      <title>Re: Sendemail (Splunk CLI) always sends email whether results are available or not...</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sendemail-Splunk-CLI-always-sends-email-whether-results-are/m-p/43614#M10257</link>
      <description>&lt;P&gt;You could consider running using the Splunk scheduler, and using Splunk's conditional script triggering rather than running the search at the CLI.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Sep 2010 05:48:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sendemail-Splunk-CLI-always-sends-email-whether-results-are/m-p/43614#M10257</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2010-09-06T05:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Sendemail (Splunk CLI) always sends email whether results are available or not...</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sendemail-Splunk-CLI-always-sends-email-whether-results-are/m-p/43615#M10258</link>
      <description>&lt;P&gt;Do it like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rename COMMENT1of3 AS "Splunk sendemail ALWAYS sends email, even when no results found; we address this with 2 settings:"
| rename COMMENT2of3 AS "First, we put 'null()' in 'to' header when no results; this causes 'sendemail' to error."
| rename COMMENT3of3 AS "Last, we use 'graceful=true' so that the search does not log any error for that."
| eval valueForToHeader=if(isnotnull(someFieldNameInYourResults), "YourGoodEmailGoesHere@YourCompany.com", null())
| sendemail
   to=$result.valueForToHeader$
   graceful=true
   ...
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Jul 2018 21:14:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sendemail-Splunk-CLI-always-sends-email-whether-results-are/m-p/43615#M10258</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2018-07-25T21:14:19Z</dc:date>
    </item>
  </channel>
</rss>

