2021-10-13 20:33:49 +00:00
|
|
|
package f2b
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fail2ban-prometheus-exporter/cfg"
|
|
|
|
"fail2ban-prometheus-exporter/socket"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Collector struct {
|
|
|
|
socketPath string
|
|
|
|
exporterVersion string
|
|
|
|
lastError error
|
|
|
|
socketConnectionErrorCount int
|
|
|
|
socketRequestErrorCount int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
|
2021-10-15 18:02:26 +00:00
|
|
|
return &Collector{
|
|
|
|
socketPath: appSettings.Fail2BanSocketPath,
|
2021-10-13 20:33:49 +00:00
|
|
|
exporterVersion: exporterVersion,
|
|
|
|
lastError: nil,
|
|
|
|
socketConnectionErrorCount: 0,
|
|
|
|
socketRequestErrorCount: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
|
2021-10-15 18:02:26 +00:00
|
|
|
ch <- metricServerUp
|
|
|
|
ch <- metricJailCount
|
|
|
|
ch <- metricJailFailedCurrent
|
|
|
|
ch <- metricJailFailedTotal
|
|
|
|
ch <- metricJailBannedCurrent
|
|
|
|
ch <- metricJailBannedTotal
|
2021-10-13 20:33:49 +00:00
|
|
|
ch <- metricErrorCount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
2021-10-15 18:02:26 +00:00
|
|
|
s, err := socket.ConnectToSocket(c.socketPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error opening socket: %v", err)
|
|
|
|
c.socketConnectionErrorCount++
|
|
|
|
} else {
|
|
|
|
defer s.Close()
|
2021-10-13 20:33:49 +00:00
|
|
|
}
|
2021-10-15 18:02:26 +00:00
|
|
|
|
|
|
|
c.collectServerUpMetric(ch, s)
|
|
|
|
if err == nil && s != nil {
|
|
|
|
c.collectJailMetrics(ch, s)
|
2021-10-13 20:33:49 +00:00
|
|
|
c.collectVersionMetric(ch, s)
|
|
|
|
}
|
|
|
|
c.collectErrorCountMetric(ch)
|
|
|
|
}
|