From 76c9dd93f4eb8c6a5dee72a38f7249461575ffd2 Mon Sep 17 00:00:00 2001
From: Hector <dev@hsmith.org>
Date: Mon, 30 Aug 2021 16:50:07 +0100
Subject: [PATCH 1/3] flag all fields related to db-based metrics as deprecated

---
 src/cfg/cfg.go  |  2 +-
 src/exporter.go | 11 ++++++-----
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/cfg/cfg.go b/src/cfg/cfg.go
index 0731fdb..1b4ce49 100644
--- a/src/cfg/cfg.go
+++ b/src/cfg/cfg.go
@@ -22,7 +22,7 @@ func Parse() *AppSettings {
 	appSettings := &AppSettings{}
 	flag.BoolVar(&appSettings.VersionMode, "version", false, "show version info and exit")
 	flag.IntVar(&appSettings.MetricsPort, "port", 9191, "port to use for the metrics server")
-	flag.StringVar(&appSettings.Fail2BanDbPath, "db", "", "path to the fail2ban sqlite database")
+	flag.StringVar(&appSettings.Fail2BanDbPath, "db", "", "path to the fail2ban sqlite database (deprecated)")
 	flag.StringVar(&appSettings.Fail2BanSocketPath, "socket", "", "path to the fail2ban server socket")
 
 	flag.Parse()
diff --git a/src/exporter.go b/src/exporter.go
index d1d8bab..a013b73 100644
--- a/src/exporter.go
+++ b/src/exporter.go
@@ -26,27 +26,27 @@ var (
 
 	metricUp = prometheus.NewDesc(
 		prometheus.BuildFQName(namespace, "", "up"),
-		"Was the last fail2ban query successful.",
+		"(Deprecated) Was the last fail2ban query successful.",
 		nil, nil,
 	)
 	metricBannedIpsPerJail = prometheus.NewDesc(
 		prometheus.BuildFQName(namespace, "", "banned_ips"),
-		"Number of banned IPs stored in the database (per jail).",
+		"(Deprecated) Number of banned IPs stored in the database (per jail).",
 		[]string{"jail"}, nil,
 	)
 	metricBadIpsPerJail = prometheus.NewDesc(
 		prometheus.BuildFQName(namespace, "", "bad_ips"),
-		"Number of bad IPs stored in the database (per jail).",
+		"(Deprecated) Number of bad IPs stored in the database (per jail).",
 		[]string{"jail"}, nil,
 	)
 	metricEnabledJails = prometheus.NewDesc(
 		prometheus.BuildFQName(namespace, "", "enabled_jails"),
-		"Enabled jails.",
+		"(Deprecated) Enabled jails.",
 		[]string{"jail"}, nil,
 	)
 	metricErrorCount = prometheus.NewDesc(
 		prometheus.BuildFQName(namespace, "", "errors"),
-		"Number of errors found since startup.",
+		"(Deprecated) Number of errors found since startup.",
 		[]string{"type"}, nil,
 	)
 
@@ -287,6 +287,7 @@ func main() {
 
 		exporter := &Exporter{}
 		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 != "" {

From 2ff6d64007e8b9d6ede603f98175c7fc25d3418c Mon Sep 17 00:00:00 2001
From: Hector <dev@hsmith.org>
Date: Mon, 30 Aug 2021 16:53:56 +0100
Subject: [PATCH 2/3] rename deprecated variables and functions

---
 src/exporter.go | 64 ++++++++++++++++++++++++-------------------------
 1 file changed, 32 insertions(+), 32 deletions(-)

diff --git a/src/exporter.go b/src/exporter.go
index a013b73..41f3619 100644
--- a/src/exporter.go
+++ b/src/exporter.go
@@ -14,8 +14,8 @@ import (
 )
 
 const (
-	namespace     = "fail2ban"
-	sockNamespace = "f2b"
+	deprecatedNamespace = "fail2ban"
+	namespace           = "f2b"
 )
 
 var (
@@ -24,64 +24,64 @@ var (
 	date    = "unknown"
 	builtBy = "unknown"
 
-	metricUp = prometheus.NewDesc(
-		prometheus.BuildFQName(namespace, "", "up"),
+	deprecatedMetricUp = prometheus.NewDesc(
+		prometheus.BuildFQName(deprecatedNamespace, "", "up"),
 		"(Deprecated) Was the last fail2ban query successful.",
 		nil, nil,
 	)
-	metricBannedIpsPerJail = prometheus.NewDesc(
-		prometheus.BuildFQName(namespace, "", "banned_ips"),
+	deprecatedMetricBannedIpsPerJail = prometheus.NewDesc(
+		prometheus.BuildFQName(deprecatedNamespace, "", "banned_ips"),
 		"(Deprecated) Number of banned IPs stored in the database (per jail).",
 		[]string{"jail"}, nil,
 	)
-	metricBadIpsPerJail = prometheus.NewDesc(
-		prometheus.BuildFQName(namespace, "", "bad_ips"),
+	deprecatedMetricBadIpsPerJail = prometheus.NewDesc(
+		prometheus.BuildFQName(deprecatedNamespace, "", "bad_ips"),
 		"(Deprecated) Number of bad IPs stored in the database (per jail).",
 		[]string{"jail"}, nil,
 	)
-	metricEnabledJails = prometheus.NewDesc(
-		prometheus.BuildFQName(namespace, "", "enabled_jails"),
+	deprecatedMetricEnabledJails = prometheus.NewDesc(
+		prometheus.BuildFQName(deprecatedNamespace, "", "enabled_jails"),
 		"(Deprecated) Enabled jails.",
 		[]string{"jail"}, nil,
 	)
-	metricErrorCount = prometheus.NewDesc(
-		prometheus.BuildFQName(namespace, "", "errors"),
+	deprecatedMetricErrorCount = prometheus.NewDesc(
+		prometheus.BuildFQName(deprecatedNamespace, "", "errors"),
 		"(Deprecated) Number of errors found since startup.",
 		[]string{"type"}, nil,
 	)
 
-	metricErrorCountNew = prometheus.NewDesc(
-		prometheus.BuildFQName(sockNamespace, "", "errors"),
+	metricErrorCount = prometheus.NewDesc(
+		prometheus.BuildFQName(namespace, "", "errors"),
 		"Number of errors found since startup",
 		[]string{"type"}, nil,
 	)
 	metricServerUp = prometheus.NewDesc(
-		prometheus.BuildFQName(sockNamespace, "", "up"),
+		prometheus.BuildFQName(namespace, "", "up"),
 		"Check if the fail2ban server is up",
 		nil, nil,
 	)
 	metricJailCount = prometheus.NewDesc(
-		prometheus.BuildFQName(sockNamespace, "", "jail_count"),
+		prometheus.BuildFQName(namespace, "", "jail_count"),
 		"Number of defined jails",
 		nil, nil,
 	)
 	metricJailFailedCurrent = prometheus.NewDesc(
-		prometheus.BuildFQName(sockNamespace, "", "jail_failed_current"),
+		prometheus.BuildFQName(namespace, "", "jail_failed_current"),
 		"Number of current failures on this jail's filter",
 		[]string{"jail"}, nil,
 	)
 	metricJailFailedTotal = prometheus.NewDesc(
-		prometheus.BuildFQName(sockNamespace, "", "jail_failed_total"),
+		prometheus.BuildFQName(namespace, "", "jail_failed_total"),
 		"Number of total failures on this jail's filter",
 		[]string{"jail"}, nil,
 	)
 	metricJailBannedCurrent = prometheus.NewDesc(
-		prometheus.BuildFQName(sockNamespace, "", "jail_banned_current"),
+		prometheus.BuildFQName(namespace, "", "jail_banned_current"),
 		"Number of IPs currently banned in this jail",
 		[]string{"jail"}, nil,
 	)
 	metricJailBannedTotal = prometheus.NewDesc(
-		prometheus.BuildFQName(sockNamespace, "", "jail_banned_total"),
+		prometheus.BuildFQName(namespace, "", "jail_banned_total"),
 		"Total number of IPs banned by this jail (includes expired bans)",
 		[]string{"jail"}, nil,
 	)
@@ -98,11 +98,11 @@ type Exporter struct {
 
 func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
 	if e.db != nil {
-		ch <- metricUp
-		ch <- metricBadIpsPerJail
-		ch <- metricBannedIpsPerJail
-		ch <- metricEnabledJails
-		ch <- metricErrorCount
+		ch <- deprecatedMetricUp
+		ch <- deprecatedMetricBadIpsPerJail
+		ch <- deprecatedMetricBannedIpsPerJail
+		ch <- deprecatedMetricEnabledJails
+		ch <- deprecatedMetricErrorCount
 	}
 	if e.socketPath != "" {
 		ch <- metricServerUp
@@ -112,7 +112,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
 		ch <- metricJailBannedCurrent
 		ch <- metricJailBannedTotal
 	}
-	ch <- metricErrorCountNew
+	ch <- metricErrorCount
 }
 
 func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
@@ -145,13 +145,13 @@ func (e *Exporter) collectUpMetric(ch chan<- prometheus.Metric) {
 		upMetricValue = 0
 	}
 	ch <- prometheus.MustNewConstMetric(
-		metricUp, prometheus.GaugeValue, upMetricValue,
+		deprecatedMetricUp, prometheus.GaugeValue, upMetricValue,
 	)
 }
 
 func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) {
 	ch <- prometheus.MustNewConstMetric(
-		metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
+		deprecatedMetricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
 	)
 }
 
@@ -166,7 +166,7 @@ func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
 
 	for jailName, count := range jailNameToCountMap {
 		ch <- prometheus.MustNewConstMetric(
-			metricBadIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
+			deprecatedMetricBadIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
 		)
 	}
 }
@@ -182,7 +182,7 @@ func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
 
 	for jailName, count := range jailNameToCountMap {
 		ch <- prometheus.MustNewConstMetric(
-			metricBannedIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
+			deprecatedMetricBannedIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
 		)
 	}
 }
@@ -198,14 +198,14 @@ func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) {
 
 	for jailName, count := range jailNameToEnabledMap {
 		ch <- prometheus.MustNewConstMetric(
-			metricEnabledJails, prometheus.GaugeValue, float64(count), jailName,
+			deprecatedMetricEnabledJails, prometheus.GaugeValue, float64(count), jailName,
 		)
 	}
 }
 
 func (e *Exporter) collectErrorCountMetricNew(ch chan<- prometheus.Metric) {
 	ch <- prometheus.MustNewConstMetric(
-		metricErrorCountNew, prometheus.CounterValue, float64(e.dbErrorCount), "db",
+		metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
 	)
 	ch <- prometheus.MustNewConstMetric(
 		metricErrorCountNew, prometheus.CounterValue, float64(e.socketConnectionErrorCount), "socket_conn",

From e60d822fa2bd0680d922ae5abc16e10c226c30f9 Mon Sep 17 00:00:00 2001
From: Hector <dev@hsmith.org>
Date: Mon, 30 Aug 2021 16:56:09 +0100
Subject: [PATCH 3/3] rename deprecated collector functions

---
 src/exporter.go | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/exporter.go b/src/exporter.go
index 41f3619..5ca325f 100644
--- a/src/exporter.go
+++ b/src/exporter.go
@@ -117,11 +117,11 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
 
 func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
 	if e.db != nil {
-		e.collectBadIpsPerJailMetrics(ch)
-		e.collectBannedIpsPerJailMetrics(ch)
-		e.collectEnabledJailMetrics(ch)
-		e.collectUpMetric(ch)
-		e.collectErrorCountMetric(ch)
+		e.collectDeprecatedBadIpsPerJailMetrics(ch)
+		e.collectDeprecatedBannedIpsPerJailMetrics(ch)
+		e.collectDeprecatedEnabledJailMetrics(ch)
+		e.collectDeprecatedUpMetric(ch)
+		e.collectDeprecatedErrorCountMetric(ch)
 	}
 	if e.socketPath != "" {
 		s, err := socket.ConnectToSocket(e.socketPath)
@@ -136,10 +136,10 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
 			e.collectJailMetrics(ch, s)
 		}
 	}
-	e.collectErrorCountMetricNew(ch)
+	e.collectErrorCountMetric(ch)
 }
 
-func (e *Exporter) collectUpMetric(ch chan<- prometheus.Metric) {
+func (e *Exporter) collectDeprecatedUpMetric(ch chan<- prometheus.Metric) {
 	var upMetricValue float64 = 1
 	if e.lastError != nil {
 		upMetricValue = 0
@@ -149,13 +149,13 @@ func (e *Exporter) collectUpMetric(ch chan<- prometheus.Metric) {
 	)
 }
 
-func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) {
+func (e *Exporter) collectDeprecatedErrorCountMetric(ch chan<- prometheus.Metric) {
 	ch <- prometheus.MustNewConstMetric(
 		deprecatedMetricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
 	)
 }
 
-func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
+func (e *Exporter) collectDeprecatedBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
 	jailNameToCountMap, err := e.db.CountBadIpsPerJail()
 	e.lastError = err
 
@@ -171,7 +171,7 @@ func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
 	}
 }
 
-func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
+func (e *Exporter) collectDeprecatedBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
 	jailNameToCountMap, err := e.db.CountBannedIpsPerJail()
 	e.lastError = err
 
@@ -187,7 +187,7 @@ func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
 	}
 }
 
-func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) {
+func (e *Exporter) collectDeprecatedEnabledJailMetrics(ch chan<- prometheus.Metric) {
 	jailNameToEnabledMap, err := e.db.JailNameToEnabledValue()
 	e.lastError = err
 
@@ -203,15 +203,15 @@ func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) {
 	}
 }
 
-func (e *Exporter) collectErrorCountMetricNew(ch chan<- prometheus.Metric) {
+func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) {
 	ch <- prometheus.MustNewConstMetric(
 		metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
 	)
 	ch <- prometheus.MustNewConstMetric(
-		metricErrorCountNew, prometheus.CounterValue, float64(e.socketConnectionErrorCount), "socket_conn",
+		metricErrorCount, prometheus.CounterValue, float64(e.socketConnectionErrorCount), "socket_conn",
 	)
 	ch <- prometheus.MustNewConstMetric(
-		metricErrorCountNew, prometheus.CounterValue, float64(e.socketRequestErrorCount), "socket_req",
+		metricErrorCount, prometheus.CounterValue, float64(e.socketRequestErrorCount), "socket_req",
 	)
 }