refactor: create new collector folder
Create a new `collector` folder to store the code for the different collectors. Move the existing f2b and textfile collectors to this folder. Minor refactors to the f2b collector to better match the code style of the newer textfile collector.
This commit is contained in:
parent
7cdaf1ebd1
commit
60e6365e1f
9 changed files with 121 additions and 127 deletions
83
src/collector/f2b/collector.go
Normal file
83
src/collector/f2b/collector.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package f2b
|
||||
|
||||
import (
|
||||
"fail2ban-prometheus-exporter/cfg"
|
||||
fail2banDb "fail2ban-prometheus-exporter/db"
|
||||
"fail2ban-prometheus-exporter/socket"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Collector struct {
|
||||
db *fail2banDb.Fail2BanDB
|
||||
socketPath string
|
||||
exporterVersion string
|
||||
lastError error
|
||||
dbErrorCount int
|
||||
socketConnectionErrorCount int
|
||||
socketRequestErrorCount int
|
||||
}
|
||||
|
||||
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Collector {
|
||||
colector := &Collector{
|
||||
exporterVersion: exporterVersion,
|
||||
lastError: nil,
|
||||
dbErrorCount: 0,
|
||||
socketConnectionErrorCount: 0,
|
||||
socketRequestErrorCount: 0,
|
||||
}
|
||||
if appSettings.Fail2BanDbPath != "" {
|
||||
log.Print("database-based metrics have been deprecated and will be removed in a future release")
|
||||
colector.db = fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath)
|
||||
}
|
||||
if appSettings.Fail2BanSocketPath != "" {
|
||||
colector.socketPath = appSettings.Fail2BanSocketPath
|
||||
}
|
||||
return colector
|
||||
}
|
||||
|
||||
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
|
||||
if c.db != nil {
|
||||
ch <- deprecatedMetricUp
|
||||
ch <- deprecatedMetricBadIpsPerJail
|
||||
ch <- deprecatedMetricBannedIpsPerJail
|
||||
ch <- deprecatedMetricEnabledJails
|
||||
ch <- deprecatedMetricErrorCount
|
||||
}
|
||||
if c.socketPath != "" {
|
||||
ch <- metricServerUp
|
||||
ch <- metricJailCount
|
||||
ch <- metricJailFailedCurrent
|
||||
ch <- metricJailFailedTotal
|
||||
ch <- metricJailBannedCurrent
|
||||
ch <- metricJailBannedTotal
|
||||
}
|
||||
ch <- metricErrorCount
|
||||
}
|
||||
|
||||
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
||||
if c.db != nil {
|
||||
c.collectDeprecatedBadIpsPerJailMetrics(ch)
|
||||
c.collectDeprecatedBannedIpsPerJailMetrics(ch)
|
||||
c.collectDeprecatedEnabledJailMetrics(ch)
|
||||
c.collectDeprecatedUpMetric(ch)
|
||||
c.collectDeprecatedErrorCountMetric(ch)
|
||||
}
|
||||
if c.socketPath != "" {
|
||||
s, err := socket.ConnectToSocket(c.socketPath)
|
||||
if err != nil {
|
||||
log.Printf("error opening socket: %v", err)
|
||||
c.socketConnectionErrorCount++
|
||||
} else {
|
||||
defer s.Close()
|
||||
}
|
||||
c.collectServerUpMetric(ch, s)
|
||||
if err == nil && s != nil {
|
||||
c.collectJailMetrics(ch, s)
|
||||
}
|
||||
c.collectVersionMetric(ch, s)
|
||||
} else {
|
||||
c.collectVersionMetric(ch, nil)
|
||||
}
|
||||
c.collectErrorCountMetric(ch)
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package export
|
||||
package f2b
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
@ -37,9 +37,9 @@ var (
|
|||
)
|
||||
)
|
||||
|
||||
func (e *Exporter) collectDeprecatedUpMetric(ch chan<- prometheus.Metric) {
|
||||
func (c *Collector) collectDeprecatedUpMetric(ch chan<- prometheus.Metric) {
|
||||
var upMetricValue float64 = 1
|
||||
if e.lastError != nil {
|
||||
if c.lastError != nil {
|
||||
upMetricValue = 0
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
|
@ -47,18 +47,18 @@ func (e *Exporter) collectDeprecatedUpMetric(ch chan<- prometheus.Metric) {
|
|||
)
|
||||
}
|
||||
|
||||
func (e *Exporter) collectDeprecatedErrorCountMetric(ch chan<- prometheus.Metric) {
|
||||
func (c *Collector) collectDeprecatedErrorCountMetric(ch chan<- prometheus.Metric) {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
deprecatedMetricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
|
||||
deprecatedMetricErrorCount, prometheus.CounterValue, float64(c.dbErrorCount), "db",
|
||||
)
|
||||
}
|
||||
|
||||
func (e *Exporter) collectDeprecatedBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
||||
jailNameToCountMap, err := e.db.CountBadIpsPerJail()
|
||||
e.lastError = err
|
||||
func (c *Collector) collectDeprecatedBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
||||
jailNameToCountMap, err := c.db.CountBadIpsPerJail()
|
||||
c.lastError = err
|
||||
|
||||
if err != nil {
|
||||
e.dbErrorCount++
|
||||
c.dbErrorCount++
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
|
@ -69,12 +69,12 @@ func (e *Exporter) collectDeprecatedBadIpsPerJailMetrics(ch chan<- prometheus.Me
|
|||
}
|
||||
}
|
||||
|
||||
func (e *Exporter) collectDeprecatedBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
||||
jailNameToCountMap, err := e.db.CountBannedIpsPerJail()
|
||||
e.lastError = err
|
||||
func (c *Collector) collectDeprecatedBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
||||
jailNameToCountMap, err := c.db.CountBannedIpsPerJail()
|
||||
c.lastError = err
|
||||
|
||||
if err != nil {
|
||||
e.dbErrorCount++
|
||||
c.dbErrorCount++
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
|
@ -85,12 +85,12 @@ func (e *Exporter) collectDeprecatedBannedIpsPerJailMetrics(ch chan<- prometheus
|
|||
}
|
||||
}
|
||||
|
||||
func (e *Exporter) collectDeprecatedEnabledJailMetrics(ch chan<- prometheus.Metric) {
|
||||
jailNameToEnabledMap, err := e.db.JailNameToEnabledValue()
|
||||
e.lastError = err
|
||||
func (c *Collector) collectDeprecatedEnabledJailMetrics(ch chan<- prometheus.Metric) {
|
||||
jailNameToEnabledMap, err := c.db.JailNameToEnabledValue()
|
||||
c.lastError = err
|
||||
|
||||
if err != nil {
|
||||
e.dbErrorCount++
|
||||
c.dbErrorCount++
|
||||
log.Print(err)
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package export
|
||||
package f2b
|
||||
|
||||
import (
|
||||
"fail2ban-prometheus-exporter/socket"
|
||||
|
@ -53,24 +53,24 @@ var (
|
|||
)
|
||||
)
|
||||
|
||||
func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) {
|
||||
func (c *Collector) collectErrorCountMetric(ch chan<- prometheus.Metric) {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
|
||||
metricErrorCount, prometheus.CounterValue, float64(c.dbErrorCount), "db",
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricErrorCount, prometheus.CounterValue, float64(e.socketConnectionErrorCount), "socket_conn",
|
||||
metricErrorCount, prometheus.CounterValue, float64(c.socketConnectionErrorCount), "socket_conn",
|
||||
)
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricErrorCount, prometheus.CounterValue, float64(e.socketRequestErrorCount), "socket_req",
|
||||
metricErrorCount, prometheus.CounterValue, float64(c.socketRequestErrorCount), "socket_req",
|
||||
)
|
||||
}
|
||||
|
||||
func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||
func (c *Collector) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||
var serverUp float64 = 0
|
||||
if s != nil {
|
||||
pingSuccess, err := s.Ping()
|
||||
if err != nil {
|
||||
e.socketRequestErrorCount++
|
||||
c.socketRequestErrorCount++
|
||||
log.Print(err)
|
||||
}
|
||||
if err == nil && pingSuccess {
|
||||
|
@ -82,11 +82,11 @@ func (e *Exporter) collectServerUpMetric(ch chan<- prometheus.Metric, s *socket.
|
|||
)
|
||||
}
|
||||
|
||||
func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||
func (c *Collector) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||
jails, err := s.GetJails()
|
||||
var count float64 = 0
|
||||
if err != nil {
|
||||
e.socketRequestErrorCount++
|
||||
c.socketRequestErrorCount++
|
||||
log.Print(err)
|
||||
}
|
||||
if err == nil {
|
||||
|
@ -97,14 +97,14 @@ func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric, s *socket.Fai
|
|||
)
|
||||
|
||||
for i := range jails {
|
||||
e.collectJailStatsMetric(ch, s, jails[i])
|
||||
c.collectJailStatsMetric(ch, s, jails[i])
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
||||
func (c *Collector) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket, jail string) {
|
||||
stats, err := s.GetJailStats(jail)
|
||||
if err != nil {
|
||||
e.socketRequestErrorCount++
|
||||
c.socketRequestErrorCount++
|
||||
log.Printf("failed to get stats for jail %s: %v", jail, err)
|
||||
return
|
||||
}
|
||||
|
@ -123,18 +123,18 @@ func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket
|
|||
)
|
||||
}
|
||||
|
||||
func (e *Exporter) collectVersionMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||
func (c *Collector) collectVersionMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
|
||||
var err error
|
||||
var fail2banVersion = ""
|
||||
if s != nil {
|
||||
fail2banVersion, err = s.GetServerVersion()
|
||||
if err != nil {
|
||||
e.socketRequestErrorCount++
|
||||
c.socketRequestErrorCount++
|
||||
log.Printf("failed to get fail2ban server version: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
metricVersionInfo, prometheus.GaugeValue, float64(1), e.exporterVersion, fail2banVersion,
|
||||
metricVersionInfo, prometheus.GaugeValue, float64(1), c.exporterVersion, fail2banVersion,
|
||||
)
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package export
|
||||
|
||||
import (
|
||||
"fail2ban-prometheus-exporter/cfg"
|
||||
fail2banDb "fail2ban-prometheus-exporter/db"
|
||||
"log"
|
||||
)
|
||||
|
||||
func NewExporter(appSettings *cfg.AppSettings, exporterVersion string) *Exporter {
|
||||
exporter := &Exporter{
|
||||
exporterVersion: exporterVersion,
|
||||
lastError: nil,
|
||||
dbErrorCount: 0,
|
||||
socketConnectionErrorCount: 0,
|
||||
socketRequestErrorCount: 0,
|
||||
}
|
||||
if appSettings.Fail2BanDbPath != "" {
|
||||
log.Print("database-based metrics have been deprecated and will be removed in a future release")
|
||||
exporter.db = fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath)
|
||||
}
|
||||
if appSettings.Fail2BanSocketPath != "" {
|
||||
exporter.socketPath = appSettings.Fail2BanSocketPath
|
||||
}
|
||||
return exporter
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
package export
|
||||
|
||||
import (
|
||||
fail2banDb "fail2ban-prometheus-exporter/db"
|
||||
"fail2ban-prometheus-exporter/socket"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Exporter struct {
|
||||
db *fail2banDb.Fail2BanDB
|
||||
socketPath string
|
||||
exporterVersion string
|
||||
lastError error
|
||||
dbErrorCount int
|
||||
socketConnectionErrorCount int
|
||||
socketRequestErrorCount int
|
||||
}
|
||||
|
||||
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
||||
if e.db != nil {
|
||||
ch <- deprecatedMetricUp
|
||||
ch <- deprecatedMetricBadIpsPerJail
|
||||
ch <- deprecatedMetricBannedIpsPerJail
|
||||
ch <- deprecatedMetricEnabledJails
|
||||
ch <- deprecatedMetricErrorCount
|
||||
}
|
||||
if e.socketPath != "" {
|
||||
ch <- metricServerUp
|
||||
ch <- metricJailCount
|
||||
ch <- metricJailFailedCurrent
|
||||
ch <- metricJailFailedTotal
|
||||
ch <- metricJailBannedCurrent
|
||||
ch <- metricJailBannedTotal
|
||||
}
|
||||
ch <- metricErrorCount
|
||||
}
|
||||
|
||||
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
||||
if e.db != nil {
|
||||
e.collectDeprecatedBadIpsPerJailMetrics(ch)
|
||||
e.collectDeprecatedBannedIpsPerJailMetrics(ch)
|
||||
e.collectDeprecatedEnabledJailMetrics(ch)
|
||||
e.collectDeprecatedUpMetric(ch)
|
||||
e.collectDeprecatedErrorCountMetric(ch)
|
||||
}
|
||||
if e.socketPath != "" {
|
||||
s, err := socket.ConnectToSocket(e.socketPath)
|
||||
if err != nil {
|
||||
log.Printf("error opening socket: %v", err)
|
||||
e.socketConnectionErrorCount++
|
||||
} else {
|
||||
defer s.Close()
|
||||
}
|
||||
e.collectServerUpMetric(ch, s)
|
||||
if err == nil && s != nil {
|
||||
e.collectJailMetrics(ch, s)
|
||||
}
|
||||
e.collectVersionMetric(ch, s)
|
||||
} else {
|
||||
e.collectVersionMetric(ch, nil)
|
||||
}
|
||||
e.collectErrorCountMetric(ch)
|
||||
}
|
|
@ -2,8 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"fail2ban-prometheus-exporter/cfg"
|
||||
"fail2ban-prometheus-exporter/export"
|
||||
"fail2ban-prometheus-exporter/textfile"
|
||||
"fail2ban-prometheus-exporter/collector/f2b"
|
||||
textfile2 "fail2ban-prometheus-exporter/collector/textfile"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -44,7 +44,7 @@ func rootHtmlHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func metricHandler(w http.ResponseWriter, r *http.Request, collector *textfile.Collector) {
|
||||
func metricHandler(w http.ResponseWriter, r *http.Request, collector *textfile2.Collector) {
|
||||
promhttp.Handler().ServeHTTP(w, r)
|
||||
collector.WriteTextFileMetrics(w, r)
|
||||
}
|
||||
|
@ -58,10 +58,10 @@ func main() {
|
|||
|
||||
log.Printf("starting fail2ban exporter at %s", addr)
|
||||
|
||||
exporter := export.NewExporter(appSettings, version)
|
||||
prometheus.MustRegister(exporter)
|
||||
f2bCollector := f2b.NewExporter(appSettings, version)
|
||||
prometheus.MustRegister(f2bCollector)
|
||||
|
||||
textFileCollector := textfile.NewCollector(appSettings)
|
||||
textFileCollector := textfile2.NewCollector(appSettings)
|
||||
prometheus.MustRegister(textFileCollector)
|
||||
|
||||
http.HandleFunc("/", rootHtmlHandler)
|
||||
|
|
Loading…
Reference in a new issue