Skip to content

Commit 904b720

Browse files
committed
Warn if configuration file not found, fail if missing auths or modules
Fixes #1456 Signed-off-by: Brian Candler <[email protected]>
1 parent 2fc7f4f commit 904b720

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package config
1616
import (
1717
"errors"
1818
"fmt"
19+
"log/slog"
1920
"os"
2021
"path/filepath"
2122
"regexp"
@@ -25,13 +26,16 @@ import (
2526
"gopkg.in/yaml.v2"
2627
)
2728

28-
func LoadFile(paths []string, expandEnvVars bool) (*Config, error) {
29+
func LoadFile(paths []string, expandEnvVars bool, logger *slog.Logger) (*Config, error) {
2930
cfg := &Config{}
3031
for _, p := range paths {
3132
files, err := filepath.Glob(p)
3233
if err != nil {
3334
return nil, err
3435
}
36+
if len(files) == 0 && logger != nil {
37+
logger.Warn("No file found matching pattern", "file", p)
38+
}
3539
for _, f := range files {
3640
content, err := os.ReadFile(f)
3741
if err != nil {

config_test.go

Lines changed: 6 additions & 6 deletions
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([]string{"testdata/snmp-auth.yml"}, false)
25+
err := sc.ReloadConfig([]string{"testdata/snmp-auth.yml"}, false, nil)
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([]string{"testdata/snmp-with-overrides.yml"}, false)
44+
err := sc.ReloadConfig([]string{"testdata/snmp-with-overrides.yml"}, false, nil)
4545
if err != nil {
4646
t.Errorf("Error loading config %v: %v", "testdata/snmp-with-overrides.yml", err)
4747
}
@@ -56,7 +56,7 @@ func TestLoadConfigWithOverrides(t *testing.T) {
5656
func TestLoadMultipleConfigs(t *testing.T) {
5757
sc := &SafeConfig{}
5858
configs := []string{"testdata/snmp-auth.yml", "testdata/snmp-with-overrides.yml"}
59-
err := sc.ReloadConfig(configs, false)
59+
err := sc.ReloadConfig(configs, false, nil)
6060
if err != nil {
6161
t.Errorf("Error loading configs %v: %v", configs, err)
6262
}
@@ -75,7 +75,7 @@ func TestEnvSecrets(t *testing.T) {
7575
t.Setenv("ENV_PRIV_PASSWORD", "snmp_priv_password")
7676

7777
sc := &SafeConfig{}
78-
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true)
78+
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true, nil)
7979
if err != nil {
8080
t.Errorf("Error loading config %v: %v", "testdata/snmp-auth-envvars.yml", err)
8181
}
@@ -106,7 +106,7 @@ func TestEnvSecretsMissing(t *testing.T) {
106106
t.Setenv("ENV_PRIV_PASSWORD", "snmp_priv_password")
107107

108108
sc := &SafeConfig{}
109-
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true)
109+
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true, nil)
110110
if err != nil {
111111
// we check the error message pattern to determine the error
112112
if strings.Contains(err.Error(), "environment variable not found") {
@@ -120,7 +120,7 @@ func TestEnvSecretsMissing(t *testing.T) {
120120
// When SNMPv2 was specified without credentials
121121
func TestEnvSecretsNotSpecified(t *testing.T) {
122122
sc := &SafeConfig{}
123-
err := sc.ReloadConfig([]string{"testdata/snmp-auth-v2nocreds.yml"}, true)
123+
err := sc.ReloadConfig([]string{"testdata/snmp-auth-v2nocreds.yml"}, true, nil)
124124
if err != nil {
125125
t.Errorf("Error loading config %v: %v", "testdata/snmp-auth-v2nocreds.yml", err)
126126
}

main.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ type SafeConfig struct {
182182
C *config.Config
183183
}
184184

185-
func (sc *SafeConfig) ReloadConfig(configFile []string, expandEnvVars bool) (err error) {
186-
conf, err := config.LoadFile(configFile, expandEnvVars)
185+
func (sc *SafeConfig) ReloadConfig(configFile []string, expandEnvVars bool, logger *slog.Logger) (err error) {
186+
conf, err := config.LoadFile(configFile, expandEnvVars, logger)
187187
if err != nil {
188188
return err
189189
}
@@ -214,10 +214,20 @@ func main() {
214214
prometheus.MustRegister(versioncollector.NewCollector("snmp_exporter"))
215215

216216
// Bail early if the config is bad.
217-
err := sc.ReloadConfig(*configFile, *expandEnvVars)
217+
err := sc.ReloadConfig(*configFile, *expandEnvVars, logger)
218218
if err != nil {
219219
logger.Error("Error parsing config file", "err", err)
220-
logger.Error("Possible version missmatch between generator and snmp_exporter. Make sure generator and snmp_exporter are the same version.")
220+
logger.Error("Possible version mismatch between generator and snmp_exporter. Make sure generator and snmp_exporter are the same version.")
221+
logger.Error("See also: https://github.com/prometheus/snmp_exporter/blob/main/auth-split-migration.md")
222+
os.Exit(1)
223+
}
224+
if len(sc.C.Modules) == 0 {
225+
logger.Error("Configuration is missing Modules. Did you provide any configuration file?")
226+
os.Exit(1)
227+
}
228+
if len(sc.C.Auths) == 0 {
229+
logger.Error("Configuration is missing Auths.")
230+
logger.Error("Possible version mismatch between generator and snmp_exporter. Make sure generator and snmp_exporter are the same version.")
221231
logger.Error("See also: https://github.com/prometheus/snmp_exporter/blob/main/auth-split-migration.md")
222232
os.Exit(1)
223233
}
@@ -235,13 +245,13 @@ func main() {
235245
for {
236246
select {
237247
case <-hup:
238-
if err := sc.ReloadConfig(*configFile, *expandEnvVars); err != nil {
248+
if err := sc.ReloadConfig(*configFile, *expandEnvVars, logger); err != nil {
239249
logger.Error("Error reloading config", "err", err)
240250
} else {
241251
logger.Info("Loaded config file")
242252
}
243253
case rc := <-reloadCh:
244-
if err := sc.ReloadConfig(*configFile, *expandEnvVars); err != nil {
254+
if err := sc.ReloadConfig(*configFile, *expandEnvVars, logger); err != nil {
245255
logger.Error("Error reloading config", "err", err)
246256
rc <- err
247257
} else {

0 commit comments

Comments
 (0)