<?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: Why does this python search script run twice? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Why-does-this-python-search-script-run-twice/m-p/169875#M48569</link>
    <description>&lt;P&gt;I do have the same issue with outputcsv that seems to stream the results. I have not yet tried it but... maybe if you run the proper search as subsearch  ( in [] ) and have the own backup command run in the main search this issue is mitigated because the subsearch is run completely first?&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jan 2016 09:28:07 GMT</pubDate>
    <dc:creator>dominiquevocat</dc:creator>
    <dc:date>2016-01-11T09:28:07Z</dc:date>
    <item>
      <title>Why does this python search script run twice?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-this-python-search-script-run-twice/m-p/169874#M48568</link>
      <description>&lt;P&gt;My background. . .   (Heavy Unix, Shell, numerous programming languages.  But new to Python and Splunk.)&lt;/P&gt;

&lt;P&gt;The intent of this script IS to archive a csv file into a separate directory with a date/time stamp for retention.  &lt;/P&gt;

&lt;P&gt;Problem is that splunk seems to run twice.  First it runs BEFORE "outputcsv" has even started creating the output csv file.   Then again, after the file has been created.   I can live with it in this script but for future python scripts, This is a problem.  I need to understand why my script gets called twice in the following search string.&lt;/P&gt;

&lt;P&gt;index=summary | outputcsv myfile | archcsv -c myfile -a temp    # Should only run one time at end.&lt;/P&gt;

&lt;P&gt;My python search script will look for "myfile.csv" in the /apps/splunk/var/run/splunk and move it to the ../temp folder.   &lt;/P&gt;

&lt;P&gt;IF there happens to be a myfile.csv in the .../var/run/splunk when the search string STARTS, it will move it FIRST, then the script will be called again when the new myfile.csv has been created.  &lt;/P&gt;

&lt;P&gt;I know that splunk is NOT unix, but I feel that the "pipe" command should NOT call the archcsv.py script until AFTER outputcsv as finished creating its myfile.csv file.  &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;local commands.conf entry&lt;BR /&gt;
[pydebug]&lt;BR /&gt;
type = python&lt;BR /&gt;
filename = pydebug.py&lt;BR /&gt;
streaming = false&lt;BR /&gt;
retainsevents = true&lt;/P&gt;

&lt;P&gt;UNIX Directory info with Comments:&lt;BR /&gt;
  [splunk]$ pwd&lt;BR /&gt;
  /apps/links/temp&lt;BR /&gt;
  [splunk]$ ls -ltr&lt;/P&gt;

&lt;P&gt;[splunk]$ ls -altr /apps/splunk/var/run/splunk/csvstuff*&lt;BR /&gt;
  -rw------- 1 splunk users 12734095 Aug  4 13:08 /apps/splunk/var/run/splunk/csvstuff.csv &lt;/P&gt;

&lt;P&gt;[splunk]$ # Now I will run the search, outputcsv and archive utility.&lt;BR /&gt;
  [splunk]$ # For some reason, it will copy the Existing csvstuff.csv and then the new one.&lt;BR /&gt;
  [splunk]$ pwd&lt;BR /&gt;
  /apps/links/temp&lt;BR /&gt;
  [splunk]$ ls -altr&lt;BR /&gt;
  total 22596&lt;BR /&gt;
  drwxr-xr-x 3 splunk users     4096 Jul 31 15:58 ..&lt;BR /&gt;
  -rw-r--r-- 1 splunk users 12734095 Aug  4 13:08 csvstuff_20140804131017.csv&lt;BR /&gt;
  -rw-r--r-- 1 splunk users 10392108 Aug  4 13:10 csvstuff_20140804131021.csv&lt;BR /&gt;
  drwxr-xr-x 2 splunk users     4096 Aug  4 13:10 . &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Python script&lt;BR /&gt;
 #!/usr/bin/python&lt;/P&gt;

&lt;P&gt;import sys, getopt, os&lt;BR /&gt;
import splunk.Intersplunk&lt;/P&gt;

&lt;P&gt;results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()&lt;/P&gt;

&lt;P&gt;def main(argv):&lt;BR /&gt;
   line = ''&lt;/P&gt;

&lt;P&gt;aarg=0&lt;BR /&gt;
   carg=0&lt;BR /&gt;
   archfold = 'subdir'&lt;BR /&gt;
   csvfile = 'default.csv'&lt;/P&gt;

&lt;P&gt;options, remainder = getopt.getopt(sys.argv[1:], 'c:a:', ['csvfile=',&lt;BR /&gt;
                                                             'archfold='])&lt;/P&gt;

&lt;P&gt;for opt, arg in options:&lt;BR /&gt;
       if opt in ('-c', '--csvfile'):&lt;BR /&gt;
          carg=1&lt;BR /&gt;
          csvfile = arg&lt;BR /&gt;
       elif opt in ('-a', '--archfold'):&lt;BR /&gt;
          aarg=1&lt;BR /&gt;
          archfold = arg&lt;/P&gt;

&lt;P&gt;sdir='/apps/splunk/var/run/splunk/'&lt;BR /&gt;
   adir='/apps/links/' + archfold + '/'&lt;BR /&gt;
   sfile=sdir + csvfile + '.csv'&lt;BR /&gt;
   afile=adir + csvfile + '_&lt;CODE&gt;date +"%Y%m%d%H%M%S"&lt;/CODE&gt;.csv'&lt;/P&gt;

&lt;P&gt;if carg == 0 or aarg == 0:&lt;BR /&gt;
      sys.exit(1)&lt;/P&gt;

&lt;P&gt;move='mv ' + sfile + ' ' + afile&lt;BR /&gt;
   line='if [ -e ' + sfile + ' ]; then ' + move + '; fi'&lt;BR /&gt;
   os.system(line)&lt;BR /&gt;
   line='chmod 644 ' + afile&lt;BR /&gt;
   os.system(line)&lt;/P&gt;

&lt;P&gt;newresults = []&lt;BR /&gt;
   oldresult = None&lt;BR /&gt;
   for result in results:&lt;BR /&gt;
      if result != oldresult:&lt;BR /&gt;
         newresults.append(result)&lt;BR /&gt;
         oldresult = result&lt;/P&gt;

&lt;P&gt;splunk.Intersplunk.outputResults(newresults)&lt;/P&gt;

&lt;P&gt;if &lt;STRONG&gt;name&lt;/STRONG&gt; == "&lt;STRONG&gt;main&lt;/STRONG&gt;":&lt;BR /&gt;
   main(sys.argv[1:])&lt;/P&gt;

&lt;P&gt;[splunk]$ # now, notice the first file above is from BEFORE I ran the search command&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 17:15:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-this-python-search-script-run-twice/m-p/169874#M48568</guid>
      <dc:creator>dwfarris</dc:creator>
      <dc:date>2020-09-28T17:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this python search script run twice?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-this-python-search-script-run-twice/m-p/169875#M48569</link>
      <description>&lt;P&gt;I do have the same issue with outputcsv that seems to stream the results. I have not yet tried it but... maybe if you run the proper search as subsearch  ( in [] ) and have the own backup command run in the main search this issue is mitigated because the subsearch is run completely first?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2016 09:28:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-this-python-search-script-run-twice/m-p/169875#M48569</guid>
      <dc:creator>dominiquevocat</dc:creator>
      <dc:date>2016-01-11T09:28:07Z</dc:date>
    </item>
  </channel>
</rss>

