Reputation: 10469
I am using telegraf
as a measuring/monitoring tool in my tests. I need to edit telegraf
configurations automatically; since all tests are being executed automatically.
Currently I am using re
for configuring it; this is the process:
But I'm searching for a library, if exists, like ConfigParser
or reconfigure
to handle the configurations as an object not content.
I tried ConfigParser.ConfigParser
, ConfigParser.RawConfigParser
and ConfigParser.SafeConfigParser
; all return:
ConfigParser.ParsingError: File contains parsing errors: /etc/telegraf/telegraf.conf
reconfigure
library has specific configuration classes, each belongs to a special type of linux configs (e.g. FSTabConfig
, ResolvConfig
and some other types), but it doesn't contain a class for telegraf
configs.
Does anyone have an option in mind?
EDIT 1:
I tried configobj
library (as @KevinC suggested), but it loads nothing:
>>> import configobj
>>> c = configobj.ConfigObj('/home/zeinab/Desktop/config-modification/telegraf.conf', list_values=False)
>>> c
ConfigObj({})
Using list_values=True
returns the same results.
Upvotes: 2
Views: 865
Reputation: 11
You can use toml
Configuration file
[[inputs.ping]]
## Hosts to send ping packets to.
urls = ["example.org"]
method = "exec"
Usage
import toml
conf = (toml.load("/etc/telegraf/telegraf.conf"))
conf.get("inputs")
Output
{'ping': [{'urls': ['example.org'], 'method': 'exec'}]}
Upvotes: 1