Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to specify a directory of config files #64

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading