From 09dadd948be39fd5008ce1e7d917cd1128d2dbf4 Mon Sep 17 00:00:00 2001 From: James Shaw Date: Wed, 14 Feb 2024 21:48:42 +0000 Subject: [PATCH] Added ability to specify multiple config files --- README.md | 4 +++- config/load.go | 45 ++++++++++++++++++++++++++++----------------- modbus_exporter.go | 4 ++-- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7517ea1..05f087f 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ usage: modbus_exporter [] Flags: -h, --[no-]help Show context-sensitive help (also try --help-long and --help-man). - --config.file="modbus.yml" + --config.file=modbus.yml ... Sets the configuration file. --[no-]web.systemd-socket Use systemd socket activation listeners instead of port listeners (Linux only). @@ -89,6 +89,8 @@ Visit http://localhost:9602/metrics to get the metrics of the exporter itself. Check out [`modbus.yml`](/modbus.yml) for more details on the configuration file format. +The `--config.file` parameter can be used multiple times to load more than one file. +It also supports [glob filename matching](https://pkg.go.dev/path/filepath#Glob), e.g. `modbus_*.yml`. ## TODO diff --git a/config/load.go b/config/load.go index e18da14..ea848c1 100755 --- a/config/load.go +++ b/config/load.go @@ -15,27 +15,38 @@ package config import ( "os" + "path/filepath" yaml "gopkg.in/yaml.v2" ) -// LoadConfig unmarshals the targets configuration file. -func LoadConfig(pathToTargets string) (Config, error) { - ls := Config{} - yamlFile, err := os.ReadFile(pathToTargets) - if err != nil { - return Config{}, err - - } - - err = yaml.Unmarshal(yamlFile, &ls) - if err != nil { - return Config{}, err - } - - if err := ls.validate(); err != nil { - return Config{}, err +// LoadConfig unmarshals the targets configuration file(s). +func LoadConfig(pathToTargets []string) (Config, error) { + fullConfig := Config{} + for _, p := range pathToTargets { + files, err := filepath.Glob(p) + if err != nil { + return Config{}, err + } + for _, f := range files { + ls := Config{} + yamlFile, err := os.ReadFile(f) + if err != nil { + return Config{}, err + } + + err = yaml.Unmarshal(yamlFile, &ls) + if err != nil { + return Config{}, err + } + + if err := ls.validate(); err != nil { + return Config{}, err + } + + fullConfig.Modules = append(fullConfig.Modules, ls.Modules...) + } } - return ls, nil + return fullConfig, nil } diff --git a/modbus_exporter.go b/modbus_exporter.go index 7a35f6d..e24d733 100644 --- a/modbus_exporter.go +++ b/modbus_exporter.go @@ -41,7 +41,7 @@ func main() { configFile = kingpin.Flag( "config.file", "Sets the configuration file.", - ).Default("modbus.yml").String() + ).Default("modbus.yml").Strings() toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":9602") ) @@ -59,7 +59,7 @@ func main() { telemetryRegistry.MustRegister(collectors.NewGoCollector()) telemetryRegistry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})) - level.Info(logger).Log("msg", "Loading configuration file", "config_file", *configFile) + level.Info(logger).Log("msg", "Loading configuration file(s)", "config_file", strings.Join(*configFile, ", ")) config, err := config.LoadConfig(*configFile) if err != nil { level.Error(logger).Log("msg", "Error loading config", "err", err)