From e2902b8cc27b7854936539c5b6685a27fd138a40 Mon Sep 17 00:00:00 2001 From: Hector Date: Sat, 19 Feb 2022 11:21:58 +0000 Subject: [PATCH] feat: improve logging on startup Update the exporter logging on startup to include the exporter version, the path to the fail2ban socket, and whether basic-auth is enabled or not. Fix code printing error messages on invalid CLI parameters to correct line breaks and correctly print the "usage" information. --- src/cfg/cfg.go | 11 +++++------ src/collector/f2b/collector.go | 1 + src/collector/textfile/collector.go | 2 +- src/exporter.go | 7 +++++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/cfg/cfg.go b/src/cfg/cfg.go index 062d11f..2f584a3 100644 --- a/src/cfg/cfg.go +++ b/src/cfg/cfg.go @@ -1,7 +1,6 @@ package cfg import ( - "flag" "fmt" "gopkg.in/alecthomas/kingpin.v2" "os" @@ -100,25 +99,25 @@ func (settings *AppSettings) validateFlags() { var flagsValid = true if !settings.VersionMode { if settings.Fail2BanSocketPath == "" { - fmt.Println("fail2ban socket path must not be blank") + fmt.Println("error: fail2ban socket path must not be blank") flagsValid = false } if settings.MetricsPort < minServerPort || settings.MetricsPort > maxServerPort { - fmt.Printf("invalid server port, must be within %d and %d (found %d)\n", + fmt.Printf("error: invalid server port, must be within %d and %d (found %d)\n", minServerPort, maxServerPort, settings.MetricsPort) flagsValid = false } if settings.FileCollectorEnabled && settings.FileCollectorPath == "" { - fmt.Printf("file collector directory path must not be empty if collector enabled\n") + fmt.Println("error: file collector directory path must not be empty if collector enabled") flagsValid = false } if (len(settings.BasicAuthProvider.username) > 0) != (len(settings.BasicAuthProvider.password) > 0) { - fmt.Printf("to enable basic auth both the username and the password must be provided") + fmt.Println("error: to enable basic auth both the username and the password must be provided") flagsValid = false } } if !flagsValid { - flag.Usage() + kingpin.Usage() os.Exit(1) } } diff --git a/src/collector/f2b/collector.go b/src/collector/f2b/collector.go index c588013..a125adb 100644 --- a/src/collector/f2b/collector.go +++ b/src/collector/f2b/collector.go @@ -16,6 +16,7 @@ type Collector struct { } func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector { + log.Printf("reading metrics from fail2ban socket: %s", appSettings.Fail2BanSocketPath) return &Collector{ socketPath: appSettings.Fail2BanSocketPath, exporterVersion: exporterVersion, diff --git a/src/collector/textfile/collector.go b/src/collector/textfile/collector.go index 00582db..d5b3a71 100644 --- a/src/collector/textfile/collector.go +++ b/src/collector/textfile/collector.go @@ -25,7 +25,7 @@ func NewCollector(appSettings *cfg.AppSettings) *Collector { fileMap: make(map[string]*fileData), } if collector.enabled { - log.Printf("collector.textfile directory: %s", collector.folderPath) + log.Printf("reading textfile metrics from: %s", collector.folderPath) } return collector } diff --git a/src/exporter.go b/src/exporter.go index 46eb3cc..adf4518 100644 --- a/src/exporter.go +++ b/src/exporter.go @@ -55,8 +55,8 @@ func main() { printAppVersion() } else { addr := fmt.Sprintf("%s:%d", appSettings.MetricsAddress, appSettings.MetricsPort) - - log.Printf("starting fail2ban exporter at %s", addr) + log.Printf("fail2ban exporter version %s", version) + log.Printf("starting server at %s", addr) f2bCollector := f2b.NewExporter(appSettings, version) prometheus.MustRegister(f2bCollector) @@ -72,6 +72,9 @@ func main() { appSettings.BasicAuthProvider, )) log.Printf("metrics available at '%s'", metricsPath) + if appSettings.BasicAuthProvider.Enabled() { + log.Printf("basic auth enabled") + } svrErr := make(chan error) go func() {