<?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: What are some examples of saved search via python API with dispatch params in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442684#M8020</link>
    <description>&lt;P&gt;Hi @mushkevych,&lt;/P&gt;

&lt;P&gt;You're on the right page. Simply use this link if you want to modify earliest and latest : &lt;BR /&gt;
&lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAEK2#viewpropssaved"&gt;http://dev.splunk.com/view/python-sdk/SP-CAAAEK2#viewpropssaved&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Code is already on the page. This is the snippet you need :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    # Retrieve the new search
    mysavedsearch = service.saved_searches["Test Search"]

    # Specify a description for the search
    # Enable the saved search to run on schedule
    # Run the search on Saturdays at 4:15am
    # Search everything in a 24-hour time range starting June 19, 12:00pm
    kwargs = {"description": "This is a test search",
            "is_scheduled": True,
            "cron_schedule": "15 4 * * 6",
            "earliest_time": "2014-06-19T12:00:00.000-07:00",
            "latest_time": "2014-06-20T12:00:00.000-07:00"}

    # Update the server and refresh the local copy of the object
    mysavedsearch.update(**kwargs).refresh()

    # Print the properties of the saved search
    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;Cheers,&lt;BR /&gt;
David&lt;/P&gt;</description>
    <pubDate>Sat, 10 Aug 2019 07:01:36 GMT</pubDate>
    <dc:creator>DavidHourani</dc:creator>
    <dc:date>2019-08-10T07:01:36Z</dc:date>
    <item>
      <title>What are some examples of saved search via python API with dispatch params</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442683#M8019</link>
      <description>&lt;P&gt;I am looking for an example of dispatching a saved search job with custom &lt;STRONG&gt;latest&lt;/STRONG&gt; and &lt;STRONG&gt;earliest&lt;/STRONG&gt; boundaries.&lt;/P&gt;
&lt;P&gt;A bit of history: my python program finds a Saved Search by its name and instantiates a job via &lt;STRONG&gt;.dispatch()&lt;/STRONG&gt; command [1].&lt;BR /&gt;The &lt;STRONG&gt;.dispatch()&lt;/STRONG&gt; method supports two ways of transferring parameters – via *&lt;EM&gt;args.* *&lt;/EM&gt; and *&lt;EM&gt;dispatch.* *&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;It seems as *&lt;EM&gt;args.* *&lt;/EM&gt; would require modification of the saved search query itself; &lt;BR /&gt;Following *&lt;EM&gt;dispatch.* *&lt;/EM&gt; parameters, however, look promising:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;dispatch.latest_time&lt;/LI&gt;
&lt;LI&gt;dispatch.earliest_time&lt;/LI&gt;
&lt;LI&gt;dispatch.time_format&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Does anybody using those in their Python programs?&lt;/P&gt;
&lt;P&gt;[1] &lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAEK2#runsaved" target="_blank"&gt;http://dev.splunk.com/view/python-sdk/SP-CAAAEK2#runsaved&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 18:40:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442683#M8019</guid>
      <dc:creator>mushkevych</dc:creator>
      <dc:date>2020-06-07T18:40:58Z</dc:date>
    </item>
    <item>
      <title>Re: What are some examples of saved search via python API with dispatch params</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442684#M8020</link>
      <description>&lt;P&gt;Hi @mushkevych,&lt;/P&gt;

&lt;P&gt;You're on the right page. Simply use this link if you want to modify earliest and latest : &lt;BR /&gt;
&lt;A href="http://dev.splunk.com/view/python-sdk/SP-CAAAEK2#viewpropssaved"&gt;http://dev.splunk.com/view/python-sdk/SP-CAAAEK2#viewpropssaved&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Code is already on the page. This is the snippet you need :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    # Retrieve the new search
    mysavedsearch = service.saved_searches["Test Search"]

    # Specify a description for the search
    # Enable the saved search to run on schedule
    # Run the search on Saturdays at 4:15am
    # Search everything in a 24-hour time range starting June 19, 12:00pm
    kwargs = {"description": "This is a test search",
            "is_scheduled": True,
            "cron_schedule": "15 4 * * 6",
            "earliest_time": "2014-06-19T12:00:00.000-07:00",
            "latest_time": "2014-06-20T12:00:00.000-07:00"}

    # Update the server and refresh the local copy of the object
    mysavedsearch.update(**kwargs).refresh()

    # Print the properties of the saved search
    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;Cheers,&lt;BR /&gt;
David&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2019 07:01:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442684#M8020</guid>
      <dc:creator>DavidHourani</dc:creator>
      <dc:date>2019-08-10T07:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: What are some examples of saved search via python API with dispatch params</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442685#M8021</link>
      <description>&lt;P&gt;@DavidHourani &lt;/P&gt;

&lt;P&gt;Thank you for reply. &lt;BR /&gt;
My main concern with this approach is that it updates the server side instance of the saved search.&lt;BR /&gt;
I would like to keep the saved search on the server side "as-is", and call it with custom parameters.&lt;/P&gt;

&lt;P&gt;Moreover, i am now wondering if Splunk supports execution of multiple concurrent saved searches with the same name.&lt;/P&gt;

&lt;P&gt;Dan&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 16:02:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442685#M8021</guid>
      <dc:creator>mushkevych</dc:creator>
      <dc:date>2019-08-19T16:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: What are some examples of saved search via python API with dispatch params</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442686#M8022</link>
      <description>&lt;P&gt;Hi @mushkevych, in that case you're question is easier than I thought. If you don't want to modify the saved search you can use the &lt;CODE&gt;savedsearch&lt;/CODE&gt; command : &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Savedsearch"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Savedsearch&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Note : &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;-If you specify All Time in the time range picker, the savedsearch command uses the time range that was saved with the saved search.
-If you specify any other time in the time range picker, the time range that you specify overrides the time range that was saved with the saved search.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So you simply have to call the &lt;CODE&gt;savedsearch&lt;/CODE&gt;command from your script and that will allow you to use your existing search. Does that answer your question ? &lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 16:23:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442686#M8022</guid>
      <dc:creator>DavidHourani</dc:creator>
      <dc:date>2019-08-19T16:23:40Z</dc:date>
    </item>
    <item>
      <title>Re: What are some examples of saved search via python API with dispatch params</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442687#M8023</link>
      <description>&lt;P&gt;Below is the approach I used to dispatch/run a &lt;EM&gt;saved search&lt;/EM&gt; job by its name with a custom &lt;STRONG&gt;latest&lt;/STRONG&gt; and &lt;STRONG&gt;earliest&lt;/STRONG&gt; boundaries. &lt;BR /&gt;
NO changes are required to the &lt;EM&gt;saved search&lt;/EM&gt; code/configuration/etc.&lt;BR /&gt;
In the case below, the date-time is represented in epoch, so the format is set to "%s":&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import time
import splunklib.client as client
import splunklib.results as results

def _run_job(job: client.Job):
    # small delay to sync server and client
    time.sleep(2)

    # Wait for the job to finish--poll for completion and display stats
    is_done = False
    while not is_done:
        job.refresh()
        time.sleep(10.0)
        is_done = job.is_done()

    output = list()
    rr = results.ResultsReader(job.results())
    for result in rr:
        if isinstance(result, results.Message):
            # Diagnostic messages may be returned in the results
            print('Diagnostic message {0}: {1}'.format(result.type, result.message))
        elif isinstance(result, dict):
            # Normal events are returned as dicts
            output.append(result)
    return output


def get(name):
    connection_kwargs = {
        'host': 'your_host_ip',
        'username': 'your username',
        'password': 'your password',
    }

    service = client.connect(**connection_kwargs)
    return service.saved_searches[name, 'YOUR_APP_NAMESPACE']


def run(name, **kwargs):
    saved_search = get(name)
    job = saved_search.dispatch(**kwargs)
    print('Dispatched Splunk Search Job &amp;lt;{0}&amp;gt; with params {1}'.format(name, kwargs))
    return _run_job(job)


def main():
    kwargs = {
        'dispatch.latest_time': end_epoch,
        'dispatch.earliest_time': start_epoch,
        'dispatch.time_format': '%s',
    }
    result = run('YOUR_SEARCH_NAME', **kwargs)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Dec 2019 18:20:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/What-are-some-examples-of-saved-search-via-python-API-with/m-p/442687#M8023</guid>
      <dc:creator>mushkevych</dc:creator>
      <dc:date>2019-12-06T18:20:49Z</dc:date>
    </item>
  </channel>
</rss>

