<?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: Practices when reading/writing .conf files in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95659#M19932</link>
    <description>&lt;P&gt;You may also want to check out the new &lt;A href="http://dev.splunk.com/view/webframework-features/SP-CAAAEY7"&gt;KV store&lt;/A&gt; feature. We are using this for config state in the latest versions of ES, Windows Infrastructure, and other apps in the future.&lt;/P&gt;</description>
    <pubDate>Wed, 25 Feb 2015 13:34:10 GMT</pubDate>
    <dc:creator>halr9000</dc:creator>
    <dc:date>2015-02-25T13:34:10Z</dc:date>
    <item>
      <title>Practices when reading/writing .conf files</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95655#M19928</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;In the Splunk App I am working on , there is a need to specify some parameters through UI, persist them and later on read/update them. Since the "setup screen" doesn't provide me all the flexibility I need I've looked for other options. Eventually I realized that by using a custom controller and reading/writing some .conf files I can obtain what I need. However the tricky part is manipulating those files.  Reading content created no problems , with the following code&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;      import splunk.bundle as bundle


       dict = {}
        try:
            conf = bundle.getConf(self.MY_CONF_FILE)
            for stanza in conf:
                logger.info("stanza : "+stanza )
                dict[stanza] = {}
                dict[stanza].update(conf[stanza].items())
        except splunk.ResourceNotFound:
            pass
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Writing on the other hand , done though the Splunk bundle proved to offer something else then desired. The content was written in a version of MY_CONF_FILE located somewhere else then "&lt;CODE&gt;.../myApp/default&lt;/CODE&gt;" directory (perhaps &lt;CODE&gt;"..../system/local"&lt;/CODE&gt;, it's a while since I've tried it). So In my intention to persist some configuration I realized that I was reading from a file and writing to another file, therefore subsequent reads would not bring previous writes.&lt;/P&gt;

&lt;P&gt;Here's the code used to write&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; conf = bundle.getConf(self.MY_CONF_FILE)
    conf.beginBatch()
    conf['STANZA']['setting_1'] = value_1
    conf['STANZA']['setting_2'] = value_2
    conf['STANZA']['setting_3'] = value_3
    conf.commitBatch()  
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Therefore I resorted to use ConfigParser for reading/writing .conf files , without doing it through Splunk.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;def writeMacrosConfFile(self,statisticsPort):
   writeSuccesfull = True       
    try:
        parser = ConfigParser.SafeConfigParser()
        parser.read(MACROS_CONF_FILE)
        parser.set(MACRO_PORT_STANZA, DEFINITION, statisticsPort)   
        with open(MACROS_CONF_FILE, 'wb') as configfile:
            parser.write(configfile)
    except Exception as e:
        writeSuccesfull = False  
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is done for both custom .conf files and the macros.conf file&lt;/P&gt;

&lt;P&gt;The question would be if there is something wrong in manipulating the .conf files like this since I am feeling it is not according to the Splunk way ?  Would this kind of code render the Splunk app unacceptable for uploading in Splunkbase ?&lt;/P&gt;

&lt;P&gt;Also any feedback on reading/writing .conf files would be greatly welcomed.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 14:57:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95655#M19928</guid>
      <dc:creator>klausJohan</dc:creator>
      <dc:date>2020-09-28T14:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: Practices when reading/writing .conf files</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95656#M19929</link>
      <description>&lt;P&gt;I submitted the app including the custom/system conf files manipulation using ConfigParser and the result is that it got accepted. So I guess this is acceptable. Hope it will help the next person having the same dilema as I did.&lt;/P&gt;

&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2013 10:50:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95656#M19929</guid>
      <dc:creator>klausJohan</dc:creator>
      <dc:date>2013-11-04T10:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: Practices when reading/writing .conf files</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95657#M19930</link>
      <description>&lt;P&gt;You may think of preserving the case if the config that you are working with is also touched by splunk, such as server.conf or web.conf and so on(parser_obj.optionxform = str). Otherwise my experience is that it is fine to use python's ConfigParser.&lt;/P&gt;

&lt;P&gt;Greetings&lt;BR /&gt;
-Stefan&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2013 11:10:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95657#M19930</guid>
      <dc:creator>sbaryakov</dc:creator>
      <dc:date>2013-11-04T11:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: Practices when reading/writing .conf files</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95658#M19931</link>
      <description>&lt;P&gt;I was looking for the same thing. I also found this:&lt;BR /&gt;
&lt;A href="http://answers.splunk.com/answers/69438/retrieve-configuration-items-from-a-custom-python-search-command.html"&gt;http://answers.splunk.com/answers/69438/retrieve-configuration-items-from-a-custom-python-search-command.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2015 00:24:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95658#M19931</guid>
      <dc:creator>ragingwire</dc:creator>
      <dc:date>2015-02-25T00:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: Practices when reading/writing .conf files</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95659#M19932</link>
      <description>&lt;P&gt;You may also want to check out the new &lt;A href="http://dev.splunk.com/view/webframework-features/SP-CAAAEY7"&gt;KV store&lt;/A&gt; feature. We are using this for config state in the latest versions of ES, Windows Infrastructure, and other apps in the future.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Feb 2015 13:34:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Practices-when-reading-writing-conf-files/m-p/95659#M19932</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2015-02-25T13:34:10Z</dc:date>
    </item>
  </channel>
</rss>

