You've already forked prometheus-fail2ban-exporter
							
							set the up metric to 0 if the socket connection fails
This commit is contained in:
		@@ -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) {
 | 
				
			||||||
	pingSuccess := s.Ping()
 | 
						var serverUp float64 = 0
 | 
				
			||||||
	var pingSuccessInt float64 = 1
 | 
						if s != nil {
 | 
				
			||||||
	if !pingSuccess {
 | 
							pingSuccess := s.Ping()
 | 
				
			||||||
		pingSuccessInt = 0
 | 
							if pingSuccess {
 | 
				
			||||||
 | 
								serverUp = 1
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	ch <- prometheus.MustNewConstMetric(
 | 
						ch <- prometheus.MustNewConstMetric(
 | 
				
			||||||
		metricServerPing, prometheus.GaugeValue, pingSuccessInt,
 | 
							metricServerUp, prometheus.GaugeValue, serverUp,
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user