<?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: Scripted Inputs: how do I persist state between invocations of script? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Scripted-Inputs-how-do-I-persist-state-between-invocations-of/m-p/231250#M45026</link>
    <description>&lt;P&gt;You can also run a search with curl or other http tools to get the &lt;CODE&gt;sourcetype=mysource | stats first(ID) by sourcetype&lt;/CODE&gt;, then use the results of this search in your script so that Splunk is your "database" / "config file".&lt;/P&gt;

&lt;P&gt;If you do the filesystem file as suggested above, I recommend something like   appName/bin/.deltaFile&lt;/P&gt;

&lt;P&gt;the . in front of the filename will make it hidden on linux systems.  Heres some unoptimized code for creating/reading/updating a delta file which contains the last date of execution,&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def getDeltaDate(datapath):
    try:
        if os.path.exists(datapath + '/.delta_date'):
            #open delta file and return date from file
            with open(datapath + '/.delta_date','r') as deltafile:
                for lastrundate in deltafile:
                    return lastrundate
                deltafile.close()
            #write new date to file, overwriting the original
            with open(datapath + '/.delta_date','w') as deltafile:
                deltafile.write(str(date.today()))
            deltafile.close()
        else:
            #write new date to file, original shouldnt exist at this point but will overwrite if so
            with open(datapath + '/.delta_date','w') as deltafile:
                deltafile.write(str(date.today()))
            deltafile.close()
            firstDate = "1970-01-01"
            return firstDate
    except OSError as e:
        logger.critical('Function: getDeltaDate failed due to the following error(s): ' + str(e))
        print('Function: getDeltaDate failed due to the following error(s): ' + str(e))
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 Jan 2016 13:39:03 GMT</pubDate>
    <dc:creator>jkat54</dc:creator>
    <dc:date>2016-01-13T13:39:03Z</dc:date>
    <item>
      <title>Scripted Inputs: how do I persist state between invocations of script?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Scripted-Inputs-how-do-I-persist-state-between-invocations-of/m-p/231247#M45023</link>
      <description>&lt;P&gt;I have a script that pulls events from my REST API for Splunk to index. My script runs on schedule.&lt;/P&gt;

&lt;P&gt;I want to only pull new events, to prevent duplication and unnecessary traffic. My events have incrementing IDs. &lt;/P&gt;

&lt;P&gt;To pull new events I need my script to remember what was the ID of the last pulled event, i.e. my script needs to persist state between runs.  If Splunk instance restarts, I too wouldn't like to bring all the events from the beginning.&lt;/P&gt;

&lt;P&gt;What are my options here? I would like not to read last ID by issuing query to Splunk.&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 12:29:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Scripted-Inputs-how-do-I-persist-state-between-invocations-of/m-p/231247#M45023</guid>
      <dc:creator>maratc</dc:creator>
      <dc:date>2016-01-13T12:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Inputs: how do I persist state between invocations of script?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Scripted-Inputs-how-do-I-persist-state-between-invocations-of/m-p/231248#M45024</link>
      <description>&lt;P&gt;Wouldn't a local file where you store that sort of information work for you?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 12:53:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Scripted-Inputs-how-do-I-persist-state-between-invocations-of/m-p/231248#M45024</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-01-13T12:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Inputs: how do I persist state between invocations of script?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Scripted-Inputs-how-do-I-persist-state-between-invocations-of/m-p/231249#M45025</link>
      <description>&lt;P&gt;I guess it would; is my script permitted to write to files, and is that a reasonable expectation that the files will stay there between restarts?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 13:02:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Scripted-Inputs-how-do-I-persist-state-between-invocations-of/m-p/231249#M45025</guid>
      <dc:creator>maratc</dc:creator>
      <dc:date>2016-01-13T13:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: Scripted Inputs: how do I persist state between invocations of script?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Scripted-Inputs-how-do-I-persist-state-between-invocations-of/m-p/231250#M45026</link>
      <description>&lt;P&gt;You can also run a search with curl or other http tools to get the &lt;CODE&gt;sourcetype=mysource | stats first(ID) by sourcetype&lt;/CODE&gt;, then use the results of this search in your script so that Splunk is your "database" / "config file".&lt;/P&gt;

&lt;P&gt;If you do the filesystem file as suggested above, I recommend something like   appName/bin/.deltaFile&lt;/P&gt;

&lt;P&gt;the . in front of the filename will make it hidden on linux systems.  Heres some unoptimized code for creating/reading/updating a delta file which contains the last date of execution,&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def getDeltaDate(datapath):
    try:
        if os.path.exists(datapath + '/.delta_date'):
            #open delta file and return date from file
            with open(datapath + '/.delta_date','r') as deltafile:
                for lastrundate in deltafile:
                    return lastrundate
                deltafile.close()
            #write new date to file, overwriting the original
            with open(datapath + '/.delta_date','w') as deltafile:
                deltafile.write(str(date.today()))
            deltafile.close()
        else:
            #write new date to file, original shouldnt exist at this point but will overwrite if so
            with open(datapath + '/.delta_date','w') as deltafile:
                deltafile.write(str(date.today()))
            deltafile.close()
            firstDate = "1970-01-01"
            return firstDate
    except OSError as e:
        logger.critical('Function: getDeltaDate failed due to the following error(s): ' + str(e))
        print('Function: getDeltaDate failed due to the following error(s): ' + str(e))
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2016 13:39:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Scripted-Inputs-how-do-I-persist-state-between-invocations-of/m-p/231250#M45026</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2016-01-13T13:39:03Z</dc:date>
    </item>
  </channel>
</rss>

