From 745195d56a98a1ae6bcdf1518a9190eb504e9e5e Mon Sep 17 00:00:00 2001 From: Hector Date: Mon, 30 Aug 2021 08:02:32 +0100 Subject: [PATCH] new metric with error counts Add a new metric to track the number of errors found while connecting to the fail2ban socket. --- src/exporter.go | 29 +++++++++++++++++++++++++++++ src/socket/fail2banSocket.go | 1 - 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/exporter.go b/src/exporter.go index da6468c..81d30cf 100644 --- a/src/exporter.go +++ b/src/exporter.go @@ -50,6 +50,11 @@ var ( []string{"type"}, nil, ) + metricErrorCountNew = prometheus.NewDesc( + prometheus.BuildFQName(sockNamespace, "", "errors"), + "Number of errors found since startup", + []string{"type"}, nil, + ) metricServerUp = prometheus.NewDesc( prometheus.BuildFQName(sockNamespace, "", "up"), "Check if the fail2ban server is up", @@ -87,6 +92,8 @@ type Exporter struct { socketPath string lastError error dbErrorCount int + socketConnectionErrorCount int + socketRequestErrorCount int } func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { @@ -105,6 +112,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { ch <- metricJailBannedCurrent ch <- metricJailBannedTotal } + ch <- metricErrorCountNew } func (e *Exporter) Collect(ch chan<- prometheus.Metric) { @@ -119,6 +127,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { s, err := socket.ConnectToSocket(e.socketPath) if err != nil { log.Printf("error opening socket: %v", err) + e.socketConnectionErrorCount++ } else { defer s.Close() } @@ -127,6 +136,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { e.collectJailMetrics(ch, s) } } + e.collectErrorCountMetricNew(ch) } func (e *Exporter) collectUpMetric(ch chan<- prometheus.Metric) { @@ -193,10 +203,25 @@ func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) { } } +func (e *Exporter) collectErrorCountMetricNew(ch chan<- prometheus.Metric) { + ch <- prometheus.MustNewConstMetric( + metricErrorCountNew, prometheus.CounterValue, float64(e.dbErrorCount), "db", + ) + ch <- prometheus.MustNewConstMetric( + metricErrorCountNew, prometheus.CounterValue, float64(e.socketConnectionErrorCount), "socket_conn", + ) + ch <- prometheus.MustNewConstMetric( + metricErrorCountNew, prometheus.CounterValue, float64(e.socketRequestErrorCount), "socket_req", + ) +} + func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) { var serverUp float64 = 0 if s != nil { pingSuccess, err := s.Ping() + if err != nil { + e.socketRequestErrorCount++ + } if err == nil && pingSuccess { serverUp = 1 } @@ -209,6 +234,9 @@ func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket. func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) { jails, err := s.GetJails() var count float64 = 0 + if err != nil { + e.socketRequestErrorCount++ + } if err == nil { count = float64(len(jails)) } @@ -224,6 +252,7 @@ func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fai func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) { stats, err := s.GetJailStats(jail) if err != nil { + e.socketRequestErrorCount++ log.Printf("failed to get stats for jail %s: %v", jail, err) return } diff --git a/src/socket/fail2banSocket.go b/src/socket/fail2banSocket.go index 589217c..3ff95de 100644 --- a/src/socket/fail2banSocket.go +++ b/src/socket/fail2banSocket.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/kisielk/og-rek" "github.com/nlpodyssey/gopickle/types" - "log" "net" "strings" )