Getting Data In

Practices when reading/writing .conf files

klausJohan
Path Finder

Hi,

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

      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

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 ".../myApp/default" directory (perhaps "..../system/local", 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.

Here's the code used to write

 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()  

Therefore I resorted to use ConfigParser for reading/writing .conf files , without doing it through Splunk.

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  

This is done for both custom .conf files and the macros.conf file

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 ?

Also any feedback on reading/writing .conf files would be greatly welcomed.

0 Karma

halr9000
Motivator

You may also want to check out the new KV store feature. We are using this for config state in the latest versions of ES, Windows Infrastructure, and other apps in the future.

0 Karma

ragingwire
Path Finder

sbaryakov
Explorer

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.

Greetings
-Stefan

0 Karma

klausJohan
Path Finder

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.

Cheers

0 Karma
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...