feat: export metrics with socket errors

Add new metric to collect the number of errors found when connecting to the
fail2ban server socket. Errors are split into two categories: connection
errors (e.g. socket file not found), and request errors (e.g. invalid
response received from server).
Update the `up` metric to return `0` if the socket connection fails.
Improve error logging.
This commit is contained in:
Hector
2021-08-30 07:19:11 +00:00
parent 828b67cdd9
commit 4da46f3c4a
2 changed files with 57 additions and 21 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt"
"github.com/kisielk/og-rek"
"github.com/nlpodyssey/gopickle/types"
"log"
"net"
"strings"
)
@ -36,21 +35,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 +122,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", command, err)
}
func trimSpaceForAll(slice []string) []string {
for i := range slice {
slice[i] = strings.TrimSpace(slice[i])