From 8e0284d9f04fa885c67493a5af2db6df6fb291db Mon Sep 17 00:00:00 2001 From: Hector Date: Thu, 22 Jun 2023 15:16:12 +0000 Subject: [PATCH] feat: print fail2ban version on startup (!96) * Print the version reported by `fail2ban-server` when the exporter starts up * Logs an error if the connection to the server fails https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/merge_requests/96 --- collector/f2b/collector.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/collector/f2b/collector.go b/collector/f2b/collector.go index 95f9bd0..e6a8ed8 100644 --- a/collector/f2b/collector.go +++ b/collector/f2b/collector.go @@ -1,11 +1,12 @@ package f2b import ( + "log" + "os" + "github.com/prometheus/client_golang/prometheus" "gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg" "gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/socket" - "log" - "os" ) type Collector struct { @@ -18,7 +19,8 @@ type Collector struct { } func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector { - log.Printf("reading metrics from fail2ban socket: %s", appSettings.Fail2BanSocketPath) + log.Printf("reading fail2ban metrics from socket file: %s", appSettings.Fail2BanSocketPath) + printFail2BanServerVersion(appSettings.Fail2BanSocketPath) return &Collector{ socketPath: appSettings.Fail2BanSocketPath, exporterVersion: exporterVersion, @@ -58,3 +60,17 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { } c.collectErrorCountMetric(ch) } + +func printFail2BanServerVersion(socketPath string) { + s, err := socket.ConnectToSocket(socketPath) + if err != nil { + log.Printf("error connecting to socket: %v", err) + } else { + version, err := s.GetServerVersion() + if err != nil { + log.Printf("error interacting with socket: %v", err) + } else { + log.Printf("successfully connected to fail2ban socket! fail2ban version: %s", version) + } + } +}