fix: recover from fail2ban server restarts

Update the code collecting metrics to open a new socket connection each
time metrics are collected. This ensures that a new socket connection is
used each time and avoids errors caused by fail2ban being restarted.
This commit is contained in:
Hector 2021-08-30 07:36:15 +01:00
parent aef73df3fa
commit acb40a94bd
2 changed files with 26 additions and 16 deletions

View File

@ -84,7 +84,7 @@ var (
type Exporter struct { type Exporter struct {
db *fail2banDb.Fail2BanDB db *fail2banDb.Fail2BanDB
socket *socket.Fail2BanSocket socketPath string
lastError error lastError error
dbErrorCount int dbErrorCount int
} }
@ -97,7 +97,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- metricEnabledJails ch <- metricEnabledJails
ch <- metricErrorCount ch <- metricErrorCount
} }
if e.socket != nil { if e.socketPath != "" {
ch <- metricServerPing ch <- metricServerPing
ch <- metricJailCount ch <- metricJailCount
ch <- metricJailFailedCurrent ch <- metricJailFailedCurrent
@ -115,9 +115,15 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.collectUpMetric(ch) e.collectUpMetric(ch)
e.collectErrorCountMetric(ch) e.collectErrorCountMetric(ch)
} }
if e.socket != nil { if e.socketPath != "" {
e.collectServerPingMetric(ch) s, err := socket.ConnectToSocket(e.socketPath)
e.collectJailMetrics(ch) if err != nil {
log.Printf("error opening socket: %v", err)
} else {
defer s.Close()
e.collectServerPingMetric(ch, s)
e.collectJailMetrics(ch, s)
}
} }
} }
@ -185,8 +191,8 @@ func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) {
} }
} }
func (e *Exporter) collectServerPingMetric(ch chan<- prometheus.Metric) { func (e *Exporter) collectServerPingMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
pingSuccess := e.socket.Ping() pingSuccess := s.Ping()
var pingSuccessInt float64 = 1 var pingSuccessInt float64 = 1
if !pingSuccess { if !pingSuccess {
pingSuccessInt = 0 pingSuccessInt = 0
@ -196,8 +202,8 @@ func (e *Exporter) collectServerPingMetric(ch chan<- prometheus.Metric) {
) )
} }
func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric) { func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
jails, err := e.socket.GetJails() jails, err := s.GetJails()
var count float64 = 0 var count float64 = 0
if err == nil { if err == nil {
count = float64(len(jails)) count = float64(len(jails))
@ -207,12 +213,12 @@ func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric) {
) )
for i := range jails { for i := range jails {
e.collectJailStatsMetric(ch, jails[i]) e.collectJailStatsMetric(ch, s, jails[i])
} }
} }
func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, jail string) { func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
stats, err := e.socket.GetJailStats(jail) stats, err := s.GetJailStats(jail)
if err != nil { if err != nil {
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
@ -249,7 +255,7 @@ func main() {
exporter.db = fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath) exporter.db = fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath)
} }
if appSettings.Fail2BanSocketPath != "" { if appSettings.Fail2BanSocketPath != "" {
exporter.socket = socket.MustConnectToSocket(appSettings.Fail2BanSocketPath) exporter.socketPath = appSettings.Fail2BanSocketPath
} }
prometheus.MustRegister(exporter) prometheus.MustRegister(exporter)

View File

@ -21,15 +21,19 @@ type JailStats struct {
BannedTotal int BannedTotal int
} }
func MustConnectToSocket(path string) *Fail2BanSocket { func ConnectToSocket(path string) (*Fail2BanSocket, error) {
c, err := net.Dial("unix", path) c, err := net.Dial("unix", path)
if err != nil { if err != nil {
log.Fatalf("failed to open fail2ban socket: %v", err) return nil, err
} }
return &Fail2BanSocket{ return &Fail2BanSocket{
socket: c, socket: c,
encoder: ogórek.NewEncoder(c), encoder: ogórek.NewEncoder(c),
} }, nil
}
func (s *Fail2BanSocket) Close() error {
return s.socket.Close()
} }
func (s *Fail2BanSocket) Ping() bool { func (s *Fail2BanSocket) Ping() bool {