From e6b7e5908112d61a209433a4c99915b98d4dbaf8 Mon Sep 17 00:00:00 2001 From: Hector <dev@hsmith.org> Date: Mon, 30 Aug 2021 07:48:18 +0100 Subject: [PATCH 1/6] set the up metric to 0 if the socket connection fails --- src/exporter.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/exporter.go b/src/exporter.go index 007fb51..54adc6d 100644 --- a/src/exporter.go +++ b/src/exporter.go @@ -50,7 +50,7 @@ var ( []string{"type"}, nil, ) - metricServerPing = prometheus.NewDesc( + metricServerUp = prometheus.NewDesc( prometheus.BuildFQName(sockNamespace, "", "up"), "Check if the fail2ban server is up", nil, nil, @@ -98,7 +98,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { ch <- metricErrorCount } if e.socketPath != "" { - ch <- metricServerPing + ch <- metricServerUp ch <- metricJailCount ch <- metricJailFailedCurrent ch <- metricJailFailedTotal @@ -121,7 +121,9 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { log.Printf("error opening socket: %v", err) } else { defer s.Close() - e.collectServerPingMetric(ch, s) + } + e.collectServerUpMetric(ch, s) + if err == nil && s != nil { e.collectJailMetrics(ch, s) } } @@ -191,14 +193,16 @@ func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) { } } -func (e *Exporter) collectServerPingMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) { - pingSuccess := s.Ping() - var pingSuccessInt float64 = 1 - if !pingSuccess { - pingSuccessInt = 0 +func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) { + var serverUp float64 = 0 + if s != nil { + pingSuccess := s.Ping() + if pingSuccess { + serverUp = 1 + } } ch <- prometheus.MustNewConstMetric( - metricServerPing, prometheus.GaugeValue, pingSuccessInt, + metricServerUp, prometheus.GaugeValue, serverUp, ) } From a764127c1437cfa3a71cdaa90cc1b866b119bd42 Mon Sep 17 00:00:00 2001 From: Hector <dev@hsmith.org> Date: Mon, 30 Aug 2021 07:52:38 +0100 Subject: [PATCH 2/6] update ping command to return error Update the ping command to return errors instead of logging them. --- src/exporter.go | 4 ++-- src/socket/fail2banSocket.go | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/exporter.go b/src/exporter.go index 54adc6d..da6468c 100644 --- a/src/exporter.go +++ b/src/exporter.go @@ -196,8 +196,8 @@ func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) { func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) { var serverUp float64 = 0 if s != nil { - pingSuccess := s.Ping() - if pingSuccess { + pingSuccess, err := s.Ping() + if err == nil && pingSuccess { serverUp = 1 } } diff --git a/src/socket/fail2banSocket.go b/src/socket/fail2banSocket.go index 493d1e3..589217c 100644 --- a/src/socket/fail2banSocket.go +++ b/src/socket/fail2banSocket.go @@ -36,21 +36,19 @@ func (s *Fail2BanSocket) Close() error { return s.socket.Close() } -func (s *Fail2BanSocket) Ping() bool { +func (s *Fail2BanSocket) Ping() (bool, error) { response, err := s.sendCommand([]string{pingCommand, "100"}) if err != nil { - log.Printf("server ping failed: %v", err) - return false + return false, newConnectionError(pingCommand, err) } if t, ok := response.(*types.Tuple); ok { if (*t)[1] == "pong" { - return true + return true, nil } - log.Printf("unexpected response data: %s", t) + return false, fmt.Errorf("unexpected response data (expecting 'pong'): %s", (*t)[1]) } - log.Printf("(%s) unexpected response format - cannot parse: %v", pingCommand, response) - return false + return false, newBadFormatError(pingCommand, response) } func (s *Fail2BanSocket) GetJails() ([]string, error) { @@ -125,6 +123,10 @@ func newBadFormatError(command string, data interface{}) error { return fmt.Errorf("(%s) unexpected response format - cannot parse: %v", command, data) } +func newConnectionError(command string, err error) error { + return fmt.Errorf("(%s) failed to send command through socket: %v", err) +} + func trimSpaceForAll(slice []string) []string { for i := range slice { slice[i] = strings.TrimSpace(slice[i]) From 745195d56a98a1ae6bcdf1518a9190eb504e9e5e Mon Sep 17 00:00:00 2001 From: Hector <dev@hsmith.org> Date: Mon, 30 Aug 2021 08:02:32 +0100 Subject: [PATCH 3/6] 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" ) From d4351418b5a4f64a5c6443457c4cfaa3fd4f8075 Mon Sep 17 00:00:00 2001 From: Hector <dev@hsmith.org> Date: Mon, 30 Aug 2021 08:03:28 +0100 Subject: [PATCH 4/6] fix compile error --- src/socket/fail2banSocket.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/socket/fail2banSocket.go b/src/socket/fail2banSocket.go index 3ff95de..4f720ad 100644 --- a/src/socket/fail2banSocket.go +++ b/src/socket/fail2banSocket.go @@ -123,7 +123,7 @@ func newBadFormatError(command string, data interface{}) error { } func newConnectionError(command string, err error) error { - return fmt.Errorf("(%s) failed to send command through socket: %v", err) + return fmt.Errorf("(%s) failed to send command through socket: %v", command, err) } func trimSpaceForAll(slice []string) []string { From 9b39eeaea2b8d36803b51d349072461b145a5091 Mon Sep 17 00:00:00 2001 From: Hector <dev@hsmith.org> Date: Mon, 30 Aug 2021 08:05:57 +0100 Subject: [PATCH 5/6] log socket request errors --- src/exporter.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/exporter.go b/src/exporter.go index 81d30cf..102cb76 100644 --- a/src/exporter.go +++ b/src/exporter.go @@ -221,6 +221,7 @@ func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket. pingSuccess, err := s.Ping() if err != nil { e.socketRequestErrorCount++ + log.Print(err) } if err == nil && pingSuccess { serverUp = 1 @@ -236,6 +237,7 @@ func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fai var count float64 = 0 if err != nil { e.socketRequestErrorCount++ + log.Print(err) } if err == nil { count = float64(len(jails)) From baf1da8c0f7373d9c33ba6f94b0c4a89ec67de3e Mon Sep 17 00:00:00 2001 From: Hector <dev@hsmith.org> Date: Mon, 30 Aug 2021 08:06:32 +0100 Subject: [PATCH 6/6] go fmt --- src/exporter.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/exporter.go b/src/exporter.go index 102cb76..d1d8bab 100644 --- a/src/exporter.go +++ b/src/exporter.go @@ -88,12 +88,12 @@ var ( ) type Exporter struct { - db *fail2banDb.Fail2BanDB - socketPath string - lastError error - dbErrorCount int + db *fail2banDb.Fail2BanDB + socketPath string + lastError error + dbErrorCount int socketConnectionErrorCount int - socketRequestErrorCount int + socketRequestErrorCount int } func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {