Markus Pesch b8cb19ea7b
Some checks failed
Golang Tests / unittest (stable, ubuntu-latest-amd64) (push) Failing after 5s
Markdown linter / markdown-lint (push) Failing after 2s
Golang Tests / unittest (stable, ubuntu-latest-arm64) (push) Failing after 15s
Golang CI lint / golangci (stable, ubuntu-latest-amd64) (push) Successful in 2m1s
Golang CI lint / golangci (stable, ubuntu-latest-arm64) (push) Successful in 2m51s
fix(Makefile): adapt Makefile and golangci lint
2025-06-01 13:23:40 +02:00

77 lines
2.1 KiB
Go

package f2b
import (
"log"
"os"
"git.cryptic.systems/volker.raschek/prometheus-fail2ban-exporter/cfg"
"git.cryptic.systems/volker.raschek/prometheus-fail2ban-exporter/socket"
"github.com/prometheus/client_golang/prometheus"
)
type Collector struct {
socketPath string
exporterVersion string
lastError error
socketConnectionErrorCount int
socketRequestErrorCount int
exitOnSocketConnError bool
}
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
log.Printf("reading fail2ban metrics from socket file: %s", appSettings.Fail2BanSocketPath)
printFail2BanServerVersion(appSettings.Fail2BanSocketPath)
return &Collector{
socketPath: appSettings.Fail2BanSocketPath,
exporterVersion: exporterVersion,
lastError: nil,
socketConnectionErrorCount: 0,
socketRequestErrorCount: 0,
exitOnSocketConnError: appSettings.ExitOnSocketConnError,
}
}
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- metricServerUp
ch <- metricJailCount
ch <- metricJailFailedCurrent
ch <- metricJailFailedTotal
ch <- metricJailBannedCurrent
ch <- metricJailBannedTotal
ch <- metricErrorCount
}
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
s, err := socket.ConnectToSocket(c.socketPath)
if err != nil {
log.Printf("error opening socket: %v", err)
c.socketConnectionErrorCount++
if c.exitOnSocketConnError {
os.Exit(1)
}
} else {
defer func() { _ = s.Close() }()
}
c.collectServerUpMetric(ch, s)
if err == nil && s != nil {
c.collectJailMetrics(ch, s)
c.collectVersionMetric(ch, s)
}
c.collectErrorCountMetric(ch)
}
func printFail2BanServerVersion(socketPath string) {
s, err := socket.ConnectToSocket(socketPath)
if err != nil {
log.Printf("error connecting to socket: %v", err)
} else {
version, err := s.GetServerVersion()
if err != nil {
log.Printf("error interacting with socket: %v", err)
} else {
log.Printf("successfully connected to fail2ban socket! fail2ban version: %s", version)
}
}
}