set the up metric to 0 if the socket connection fails

This commit is contained in:
Hector 2021-08-30 07:48:18 +01:00
parent 828b67cdd9
commit e6b7e59081

View File

@ -50,7 +50,7 @@ var (
[]string{"type"}, nil, []string{"type"}, nil,
) )
metricServerPing = 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",
nil, nil, nil, nil,
@ -98,7 +98,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- metricErrorCount ch <- metricErrorCount
} }
if e.socketPath != "" { if e.socketPath != "" {
ch <- metricServerPing ch <- metricServerUp
ch <- metricJailCount ch <- metricJailCount
ch <- metricJailFailedCurrent ch <- metricJailFailedCurrent
ch <- metricJailFailedTotal ch <- metricJailFailedTotal
@ -121,7 +121,9 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
log.Printf("error opening socket: %v", err) log.Printf("error opening socket: %v", err)
} else { } else {
defer s.Close() defer s.Close()
e.collectServerPingMetric(ch, s) }
e.collectServerUpMetric(ch, s)
if err == nil && s != nil {
e.collectJailMetrics(ch, s) 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) { func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
var serverUp float64 = 0
if s != nil {
pingSuccess := s.Ping() pingSuccess := s.Ping()
var pingSuccessInt float64 = 1 if pingSuccess {
if !pingSuccess { serverUp = 1
pingSuccessInt = 0 }
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricServerPing, prometheus.GaugeValue, pingSuccessInt, metricServerUp, prometheus.GaugeValue, serverUp,
) )
} }