Skip to content

Commit ccf6ffe

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 5da4066 commit ccf6ffe

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-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(logger *slog.Logger, paths []string, expandEnvVars bool) (*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 {
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: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ import (
1717
"strings"
1818
"testing"
1919

20+
"github.com/prometheus/common/promslog"
2021
yaml "gopkg.in/yaml.v2"
2122
)
2223

24+
var nopLogger = promslog.NewNopLogger()
25+
2326
func TestHideConfigSecrets(t *testing.T) {
2427
sc := &SafeConfig{}
25-
err := sc.ReloadConfig([]string{"testdata/snmp-auth.yml"}, false)
28+
err := sc.ReloadConfig(nopLogger, []string{"testdata/snmp-auth.yml"}, false)
2629
if err != nil {
2730
t.Errorf("Error loading config %v: %v", "testdata/snmp-auth.yml", err)
2831
}
@@ -41,7 +44,7 @@ func TestHideConfigSecrets(t *testing.T) {
4144

4245
func TestLoadConfigWithOverrides(t *testing.T) {
4346
sc := &SafeConfig{}
44-
err := sc.ReloadConfig([]string{"testdata/snmp-with-overrides.yml"}, false)
47+
err := sc.ReloadConfig(nopLogger, []string{"testdata/snmp-with-overrides.yml"}, false)
4548
if err != nil {
4649
t.Errorf("Error loading config %v: %v", "testdata/snmp-with-overrides.yml", err)
4750
}
@@ -56,7 +59,7 @@ func TestLoadConfigWithOverrides(t *testing.T) {
5659
func TestLoadMultipleConfigs(t *testing.T) {
5760
sc := &SafeConfig{}
5861
configs := []string{"testdata/snmp-auth.yml", "testdata/snmp-with-overrides.yml"}
59-
err := sc.ReloadConfig(configs, false)
62+
err := sc.ReloadConfig(nopLogger, configs, false)
6063
if err != nil {
6164
t.Errorf("Error loading configs %v: %v", configs, err)
6265
}
@@ -75,7 +78,7 @@ func TestEnvSecrets(t *testing.T) {
7578
t.Setenv("ENV_PRIV_PASSWORD", "snmp_priv_password")
7679

7780
sc := &SafeConfig{}
78-
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true)
81+
err := sc.ReloadConfig(nopLogger, []string{"testdata/snmp-auth-envvars.yml"}, true)
7982
if err != nil {
8083
t.Errorf("Error loading config %v: %v", "testdata/snmp-auth-envvars.yml", err)
8184
}
@@ -106,7 +109,7 @@ func TestEnvSecretsMissing(t *testing.T) {
106109
t.Setenv("ENV_PRIV_PASSWORD", "snmp_priv_password")
107110

108111
sc := &SafeConfig{}
109-
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true)
112+
err := sc.ReloadConfig(nopLogger, []string{"testdata/snmp-auth-envvars.yml"}, true)
110113
if err != nil {
111114
// we check the error message pattern to determine the error
112115
if strings.Contains(err.Error(), "environment variable not found") {
@@ -120,7 +123,7 @@ func TestEnvSecretsMissing(t *testing.T) {
120123
// When SNMPv2 was specified without credentials
121124
func TestEnvSecretsNotSpecified(t *testing.T) {
122125
sc := &SafeConfig{}
123-
err := sc.ReloadConfig([]string{"testdata/snmp-auth-v2nocreds.yml"}, true)
126+
err := sc.ReloadConfig(nopLogger, []string{"testdata/snmp-auth-v2nocreds.yml"}, true)
124127
if err != nil {
125128
t.Errorf("Error loading config %v: %v", "testdata/snmp-auth-v2nocreds.yml", err)
126129
}

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(logger *slog.Logger, configFile []string, expandEnvVars bool) (err error) {
186+
conf, err := config.LoadFile(logger, configFile, expandEnvVars)
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(logger, *configFile, *expandEnvVars)
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(logger, *configFile, *expandEnvVars); 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(logger, *configFile, *expandEnvVars); err != nil {
245255
logger.Error("Error reloading config", "err", err)
246256
rc <- err
247257
} else {

0 commit comments

Comments
 (0)