Skip to content

Commit

Permalink
Merge pull request #64 from omnistat/main
Browse files Browse the repository at this point in the history
Added ability to specify a directory of config files
  • Loading branch information
bastischubert authored Mar 11, 2024
2 parents bbb0902 + e3e6cd7 commit f42ee2c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ usage: modbus_exporter [<flags>]
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).
Expand All @@ -89,6 +89,9 @@ 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`.

## Systemd service

You can create a systemd service if you want to run modbus exporter as a service in the background. Start by creating a modbus_exporter system account (example on Debian)
Expand Down
45 changes: 28 additions & 17 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions modbus_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand All @@ -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)
Expand Down

0 comments on commit f42ee2c

Please sign in to comment.