From a764127c1437cfa3a71cdaa90cc1b866b119bd42 Mon Sep 17 00:00:00 2001 From: Hector Date: Mon, 30 Aug 2021 07:52:38 +0100 Subject: [PATCH] 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])