2021-10-13 22:33:49 +02:00
|
|
|
package f2b
|
|
|
|
|
|
|
|
import (
|
2023-06-22 17:16:12 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2021-10-13 22:33:49 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-02-25 22:53:38 +01:00
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
|
|
|
|
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/socket"
|
2021-10-13 22:33:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Collector struct {
|
|
|
|
socketPath string
|
|
|
|
exporterVersion string
|
|
|
|
lastError error
|
|
|
|
socketConnectionErrorCount int
|
|
|
|
socketRequestErrorCount int
|
2022-06-19 09:20:45 +02:00
|
|
|
exitOnSocketConnError bool
|
2021-10-13 22:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
|
2023-06-22 17:16:12 +02:00
|
|
|
log.Printf("reading fail2ban metrics from socket file: %s", appSettings.Fail2BanSocketPath)
|
|
|
|
printFail2BanServerVersion(appSettings.Fail2BanSocketPath)
|
2021-10-15 20:02:26 +02:00
|
|
|
return &Collector{
|
|
|
|
socketPath: appSettings.Fail2BanSocketPath,
|
2021-10-13 22:33:49 +02:00
|
|
|
exporterVersion: exporterVersion,
|
|
|
|
lastError: nil,
|
|
|
|
socketConnectionErrorCount: 0,
|
|
|
|
socketRequestErrorCount: 0,
|
2022-06-19 09:20:45 +02:00
|
|
|
exitOnSocketConnError: appSettings.ExitOnSocketConnError,
|
2021-10-13 22:33:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
|
2021-10-15 20:02:26 +02:00
|
|
|
ch <- metricServerUp
|
|
|
|
ch <- metricJailCount
|
|
|
|
ch <- metricJailFailedCurrent
|
|
|
|
ch <- metricJailFailedTotal
|
|
|
|
ch <- metricJailBannedCurrent
|
|
|
|
ch <- metricJailBannedTotal
|
2021-10-13 22:33:49 +02:00
|
|
|
ch <- metricErrorCount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
2021-10-15 20:02:26 +02:00
|
|
|
s, err := socket.ConnectToSocket(c.socketPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error opening socket: %v", err)
|
|
|
|
c.socketConnectionErrorCount++
|
2022-06-19 09:20:45 +02:00
|
|
|
if c.exitOnSocketConnError {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-10-15 20:02:26 +02:00
|
|
|
} else {
|
|
|
|
defer s.Close()
|
2021-10-13 22:33:49 +02:00
|
|
|
}
|
2021-10-15 20:02:26 +02:00
|
|
|
|
|
|
|
c.collectServerUpMetric(ch, s)
|
|
|
|
if err == nil && s != nil {
|
|
|
|
c.collectJailMetrics(ch, s)
|
2021-10-13 22:33:49 +02:00
|
|
|
c.collectVersionMetric(ch, s)
|
|
|
|
}
|
|
|
|
c.collectErrorCountMetric(ch)
|
|
|
|
}
|
2023-06-22 17:16:12 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|