new metric with error counts

Add a new metric to track the number of errors found while connecting to
the fail2ban socket.
This commit is contained in:
Hector 2021-08-30 08:02:32 +01:00
parent a764127c14
commit 745195d56a
2 changed files with 29 additions and 1 deletions

View File

@ -50,6 +50,11 @@ var (
[]string{"type"}, nil, []string{"type"}, nil,
) )
metricErrorCountNew = prometheus.NewDesc(
prometheus.BuildFQName(sockNamespace, "", "errors"),
"Number of errors found since startup",
[]string{"type"}, nil,
)
metricServerUp = prometheus.NewDesc( metricServerUp = prometheus.NewDesc(
prometheus.BuildFQName(sockNamespace, "", "up"), prometheus.BuildFQName(sockNamespace, "", "up"),
"Check if the fail2ban server is up", "Check if the fail2ban server is up",
@ -87,6 +92,8 @@ type Exporter struct {
socketPath string socketPath string
lastError error lastError error
dbErrorCount int dbErrorCount int
socketConnectionErrorCount int
socketRequestErrorCount int
} }
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
@ -105,6 +112,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- metricJailBannedCurrent ch <- metricJailBannedCurrent
ch <- metricJailBannedTotal ch <- metricJailBannedTotal
} }
ch <- metricErrorCountNew
} }
func (e *Exporter) Collect(ch chan<- prometheus.Metric) { 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) s, err := socket.ConnectToSocket(e.socketPath)
if err != nil { if err != nil {
log.Printf("error opening socket: %v", err) log.Printf("error opening socket: %v", err)
e.socketConnectionErrorCount++
} else { } else {
defer s.Close() defer s.Close()
} }
@ -127,6 +136,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.collectJailMetrics(ch, s) e.collectJailMetrics(ch, s)
} }
} }
e.collectErrorCountMetricNew(ch)
} }
func (e *Exporter) collectUpMetric(ch chan<- prometheus.Metric) { 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) { func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
var serverUp float64 = 0 var serverUp float64 = 0
if s != nil { if s != nil {
pingSuccess, err := s.Ping() pingSuccess, err := s.Ping()
if err != nil {
e.socketRequestErrorCount++
}
if err == nil && pingSuccess { if err == nil && pingSuccess {
serverUp = 1 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) { func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
jails, err := s.GetJails() jails, err := s.GetJails()
var count float64 = 0 var count float64 = 0
if err != nil {
e.socketRequestErrorCount++
}
if err == nil { if err == nil {
count = float64(len(jails)) 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) { func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
stats, err := s.GetJailStats(jail) stats, err := s.GetJailStats(jail)
if err != nil { if err != nil {
e.socketRequestErrorCount++
log.Printf("failed to get stats for jail %s: %v", jail, err) log.Printf("failed to get stats for jail %s: %v", jail, err)
return return
} }

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"github.com/kisielk/og-rek" "github.com/kisielk/og-rek"
"github.com/nlpodyssey/gopickle/types" "github.com/nlpodyssey/gopickle/types"
"log"
"net" "net"
"strings" "strings"
) )