Skip to content

Commit

Permalink
Support loading multiple configuration files
Browse files Browse the repository at this point in the history
Support loading multiple configuration files by by allowing
`--config.file` to be repeateable. As well as supporting glob file
matching for `config.d` style setups.
* Conflicting auth or module keys are treated as errors.

Fixes: #628

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Aug 27, 2023
1 parent a42276b commit a4c4a38
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ by hand. If you need to change it, see
The default `snmp.yml` file covers a variety of common hardware walking them
using SNMP v2 GETBULK.

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). (i.e. `snmp*.yml`)

Duplicate `module` or `auth` entries are treated as invalid and can not be loaded.

## Prometheus Configuration

The URL params `target`, `auth`, and `module` can be controlled through relabelling.
Expand Down
25 changes: 17 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,31 @@ package config
import (
"fmt"
"os"
"path/filepath"
"regexp"
"time"

"github.com/gosnmp/gosnmp"
"gopkg.in/yaml.v2"
)

func LoadFile(filename string) (*Config, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
func LoadFile(paths []string) (*Config, error) {
cfg := &Config{}
err = yaml.UnmarshalStrict(content, cfg)
if err != nil {
return nil, err
for _, p := range paths {
files, err := filepath.Glob(p)
if err != nil {
return nil, err
}
for _, f := range files {
content, err := os.ReadFile(f)
if err != nil {
return nil, err
}
err = yaml.UnmarshalStrict(content, cfg)
if err != nil {
return nil, err
}
}
}
return cfg, nil
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
)

var (
configFile = kingpin.Flag("config.file", "Path to configuration file.").Default("snmp.yml").String()
configFile = kingpin.Flag("config.file", "Path to configuration file.").Default("snmp.yml").Strings()
dryRun = kingpin.Flag("dry-run", "Only verify configuration is valid and exit.").Default("false").Bool()
concurrency = kingpin.Flag("snmp.module-concurrency", "The number of modules to fetch concurrently per scrape").Default("1").Int()
metricsPath = kingpin.Flag(
Expand Down Expand Up @@ -152,7 +152,7 @@ type SafeConfig struct {
C *config.Config
}

func (sc *SafeConfig) ReloadConfig(configFile string) (err error) {
func (sc *SafeConfig) ReloadConfig(configFile []string) (err error) {
conf, err := config.LoadFile(configFile)
if err != nil {
return err
Expand Down

0 comments on commit a4c4a38

Please sign in to comment.