Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 266b98b

Browse files
committedAug 27, 2023
Support loading multiple configuration files
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 <superq@gmail.com>
1 parent a42276b commit 266b98b

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed
 

‎README.md

+5
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ by hand. If you need to change it, see
117117
The default `snmp.yml` file covers a variety of common hardware walking them
118118
using SNMP v2 GETBULK.
119119

120+
The `--config.file` parameter can be used multiple times to load more than one file.
121+
It also supports [glob filename matching](https://pkg.go.dev/path/filepath#Glob). (i.e. `snmp*.yml`)
122+
123+
Duplicate `module` or `auth` entries are treated as invalid and can not be loaded.
124+
120125
## Prometheus Configuration
121126

122127
The URL params `target`, `auth`, and `module` can be controlled through relabelling.

‎config/config.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,31 @@ package config
1616
import (
1717
"fmt"
1818
"os"
19+
"path/filepath"
1920
"regexp"
2021
"time"
2122

2223
"github.com/gosnmp/gosnmp"
2324
"gopkg.in/yaml.v2"
2425
)
2526

26-
func LoadFile(filename string) (*Config, error) {
27-
content, err := os.ReadFile(filename)
28-
if err != nil {
29-
return nil, err
30-
}
27+
func LoadFile(paths []string) (*Config, error) {
3128
cfg := &Config{}
32-
err = yaml.UnmarshalStrict(content, cfg)
33-
if err != nil {
34-
return nil, err
29+
for _, p := range paths {
30+
files, err := filepath.Glob(p)
31+
if err != nil {
32+
return nil, err
33+
}
34+
for _, f := range files {
35+
content, err := os.ReadFile(f)
36+
if err != nil {
37+
return nil, err
38+
}
39+
err = yaml.UnmarshalStrict(content, cfg)
40+
if err != nil {
41+
return nil, err
42+
}
43+
}
3544
}
3645
return cfg, nil
3746
}

‎config_test.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
func TestHideConfigSecrets(t *testing.T) {
2424
sc := &SafeConfig{}
25-
err := sc.ReloadConfig("testdata/snmp-auth.yml")
25+
err := sc.ReloadConfig([]string{"testdata/snmp-auth.yml"})
2626
if err != nil {
2727
t.Errorf("Error loading config %v: %v", "testdata/snmp-auth.yml", err)
2828
}
@@ -41,7 +41,7 @@ func TestHideConfigSecrets(t *testing.T) {
4141

4242
func TestLoadConfigWithOverrides(t *testing.T) {
4343
sc := &SafeConfig{}
44-
err := sc.ReloadConfig("testdata/snmp-with-overrides.yml")
44+
err := sc.ReloadConfig([]string{"testdata/snmp-with-overrides.yml"})
4545
if err != nil {
4646
t.Errorf("Error loading config %v: %v", "testdata/snmp-with-overrides.yml", err)
4747
}
@@ -52,3 +52,18 @@ func TestLoadConfigWithOverrides(t *testing.T) {
5252
t.Errorf("Error marshaling config: %v", err)
5353
}
5454
}
55+
56+
func TestLoadMultipleConfigs(t *testing.T) {
57+
sc := &SafeConfig{}
58+
configs := []string{"testdata/snmp-auth.yml", "testdata/snmp-with-overrides.yml"}
59+
err := sc.ReloadConfig(configs)
60+
if err != nil {
61+
t.Errorf("Error loading configs %v: %v", configs, err)
62+
}
63+
sc.RLock()
64+
_, err = yaml.Marshal(sc.C)
65+
sc.RUnlock()
66+
if err != nil {
67+
t.Errorf("Error marshaling config: %v", err)
68+
}
69+
}

‎main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141
)
4242

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

155-
func (sc *SafeConfig) ReloadConfig(configFile string) (err error) {
155+
func (sc *SafeConfig) ReloadConfig(configFile []string) (err error) {
156156
conf, err := config.LoadFile(configFile)
157157
if err != nil {
158158
return err

0 commit comments

Comments
 (0)
Please sign in to comment.