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.
This commit is contained in:
Hector 2022-02-19 11:21:58 +00:00
parent ec10999814
commit e2902b8cc2
4 changed files with 12 additions and 9 deletions

View file

@ -1,7 +1,6 @@
package cfg package cfg
import ( import (
"flag"
"fmt" "fmt"
"gopkg.in/alecthomas/kingpin.v2" "gopkg.in/alecthomas/kingpin.v2"
"os" "os"
@ -100,25 +99,25 @@ func (settings *AppSettings) validateFlags() {
var flagsValid = true var flagsValid = true
if !settings.VersionMode { if !settings.VersionMode {
if settings.Fail2BanSocketPath == "" { if settings.Fail2BanSocketPath == "" {
fmt.Println("fail2ban socket path must not be blank") fmt.Println("error: fail2ban socket path must not be blank")
flagsValid = false flagsValid = false
} }
if settings.MetricsPort < minServerPort || settings.MetricsPort > maxServerPort { 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) minServerPort, maxServerPort, settings.MetricsPort)
flagsValid = false flagsValid = false
} }
if settings.FileCollectorEnabled && settings.FileCollectorPath == "" { 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 flagsValid = false
} }
if (len(settings.BasicAuthProvider.username) > 0) != (len(settings.BasicAuthProvider.password) > 0) { 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 flagsValid = false
} }
} }
if !flagsValid { if !flagsValid {
flag.Usage() kingpin.Usage()
os.Exit(1) os.Exit(1)
} }
} }

View file

@ -16,6 +16,7 @@ type Collector struct {
} }
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector { func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
log.Printf("reading metrics from fail2ban socket: %s", appSettings.Fail2BanSocketPath)
return &Collector{ return &Collector{
socketPath: appSettings.Fail2BanSocketPath, socketPath: appSettings.Fail2BanSocketPath,
exporterVersion: exporterVersion, exporterVersion: exporterVersion,

View file

@ -25,7 +25,7 @@ func NewCollector(appSettings *cfg.AppSettings) *Collector {
fileMap: make(map[string]*fileData), fileMap: make(map[string]*fileData),
} }
if collector.enabled { if collector.enabled {
log.Printf("collector.textfile directory: %s", collector.folderPath) log.Printf("reading textfile metrics from: %s", collector.folderPath)
} }
return collector return collector
} }

View file

@ -55,8 +55,8 @@ func main() {
printAppVersion() printAppVersion()
} else { } else {
addr := fmt.Sprintf("%s:%d", appSettings.MetricsAddress, appSettings.MetricsPort) addr := fmt.Sprintf("%s:%d", appSettings.MetricsAddress, appSettings.MetricsPort)
log.Printf("fail2ban exporter version %s", version)
log.Printf("starting fail2ban exporter at %s", addr) log.Printf("starting server at %s", addr)
f2bCollector := f2b.NewExporter(appSettings, version) f2bCollector := f2b.NewExporter(appSettings, version)
prometheus.MustRegister(f2bCollector) prometheus.MustRegister(f2bCollector)
@ -72,6 +72,9 @@ func main() {
appSettings.BasicAuthProvider, appSettings.BasicAuthProvider,
)) ))
log.Printf("metrics available at '%s'", metricsPath) log.Printf("metrics available at '%s'", metricsPath)
if appSettings.BasicAuthProvider.Enabled() {
log.Printf("basic auth enabled")
}
svrErr := make(chan error) svrErr := make(chan error)
go func() { go func() {