refactor: deprecate database metrics

Update all old database-based metrics to include the `deprecated` text.
Add a warning on startup if connecting to the fail2ban database to state
that this functionality will be removed in a future release.
Rename deprecated methods and variables.
This commit is contained in:
Hector 2021-08-30 16:38:33 +00:00
parent 737a86b6fd
commit 5b62670e9d
2 changed files with 53 additions and 52 deletions

View File

@ -22,7 +22,7 @@ func Parse() *AppSettings {
appSettings := &AppSettings{} appSettings := &AppSettings{}
flag.BoolVar(&appSettings.VersionMode, "version", false, "show version info and exit") 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.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.StringVar(&appSettings.Fail2BanSocketPath, "socket", "", "path to the fail2ban server socket")
flag.Parse() flag.Parse()

View File

@ -14,8 +14,8 @@ import (
) )
const ( const (
namespace = "fail2ban" deprecatedNamespace = "fail2ban"
sockNamespace = "f2b" namespace = "f2b"
) )
var ( var (
@ -24,64 +24,64 @@ var (
date = "unknown" date = "unknown"
builtBy = "unknown" builtBy = "unknown"
metricUp = prometheus.NewDesc( deprecatedMetricUp = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"), prometheus.BuildFQName(deprecatedNamespace, "", "up"),
"Was the last fail2ban query successful.", "(Deprecated) Was the last fail2ban query successful.",
nil, nil, nil, nil,
) )
metricBannedIpsPerJail = prometheus.NewDesc( deprecatedMetricBannedIpsPerJail = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "banned_ips"), prometheus.BuildFQName(deprecatedNamespace, "", "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, []string{"jail"}, nil,
) )
metricBadIpsPerJail = prometheus.NewDesc( deprecatedMetricBadIpsPerJail = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "bad_ips"), prometheus.BuildFQName(deprecatedNamespace, "", "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, []string{"jail"}, nil,
) )
metricEnabledJails = prometheus.NewDesc( deprecatedMetricEnabledJails = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "enabled_jails"), prometheus.BuildFQName(deprecatedNamespace, "", "enabled_jails"),
"Enabled jails.", "(Deprecated) Enabled jails.",
[]string{"jail"}, nil, []string{"jail"}, nil,
) )
metricErrorCount = prometheus.NewDesc( deprecatedMetricErrorCount = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "errors"), prometheus.BuildFQName(deprecatedNamespace, "", "errors"),
"Number of errors found since startup.", "(Deprecated) Number of errors found since startup.",
[]string{"type"}, nil, []string{"type"}, nil,
) )
metricErrorCountNew = prometheus.NewDesc( metricErrorCount = prometheus.NewDesc(
prometheus.BuildFQName(sockNamespace, "", "errors"), prometheus.BuildFQName(namespace, "", "errors"),
"Number of errors found since startup", "Number of errors found since startup",
[]string{"type"}, nil, []string{"type"}, nil,
) )
metricServerUp = prometheus.NewDesc( metricServerUp = prometheus.NewDesc(
prometheus.BuildFQName(sockNamespace, "", "up"), prometheus.BuildFQName(namespace, "", "up"),
"Check if the fail2ban server is up", "Check if the fail2ban server is up",
nil, nil, nil, nil,
) )
metricJailCount = prometheus.NewDesc( metricJailCount = prometheus.NewDesc(
prometheus.BuildFQName(sockNamespace, "", "jail_count"), prometheus.BuildFQName(namespace, "", "jail_count"),
"Number of defined jails", "Number of defined jails",
nil, nil, nil, nil,
) )
metricJailFailedCurrent = prometheus.NewDesc( metricJailFailedCurrent = prometheus.NewDesc(
prometheus.BuildFQName(sockNamespace, "", "jail_failed_current"), prometheus.BuildFQName(namespace, "", "jail_failed_current"),
"Number of current failures on this jail's filter", "Number of current failures on this jail's filter",
[]string{"jail"}, nil, []string{"jail"}, nil,
) )
metricJailFailedTotal = prometheus.NewDesc( metricJailFailedTotal = prometheus.NewDesc(
prometheus.BuildFQName(sockNamespace, "", "jail_failed_total"), prometheus.BuildFQName(namespace, "", "jail_failed_total"),
"Number of total failures on this jail's filter", "Number of total failures on this jail's filter",
[]string{"jail"}, nil, []string{"jail"}, nil,
) )
metricJailBannedCurrent = prometheus.NewDesc( metricJailBannedCurrent = prometheus.NewDesc(
prometheus.BuildFQName(sockNamespace, "", "jail_banned_current"), prometheus.BuildFQName(namespace, "", "jail_banned_current"),
"Number of IPs currently banned in this jail", "Number of IPs currently banned in this jail",
[]string{"jail"}, nil, []string{"jail"}, nil,
) )
metricJailBannedTotal = prometheus.NewDesc( 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)", "Total number of IPs banned by this jail (includes expired bans)",
[]string{"jail"}, nil, []string{"jail"}, nil,
) )
@ -98,11 +98,11 @@ type Exporter struct {
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
if e.db != nil { if e.db != nil {
ch <- metricUp ch <- deprecatedMetricUp
ch <- metricBadIpsPerJail ch <- deprecatedMetricBadIpsPerJail
ch <- metricBannedIpsPerJail ch <- deprecatedMetricBannedIpsPerJail
ch <- metricEnabledJails ch <- deprecatedMetricEnabledJails
ch <- metricErrorCount ch <- deprecatedMetricErrorCount
} }
if e.socketPath != "" { if e.socketPath != "" {
ch <- metricServerUp ch <- metricServerUp
@ -112,16 +112,16 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- metricJailBannedCurrent ch <- metricJailBannedCurrent
ch <- metricJailBannedTotal ch <- metricJailBannedTotal
} }
ch <- metricErrorCountNew ch <- metricErrorCount
} }
func (e *Exporter) Collect(ch chan<- prometheus.Metric) { func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
if e.db != nil { if e.db != nil {
e.collectBadIpsPerJailMetrics(ch) e.collectDeprecatedBadIpsPerJailMetrics(ch)
e.collectBannedIpsPerJailMetrics(ch) e.collectDeprecatedBannedIpsPerJailMetrics(ch)
e.collectEnabledJailMetrics(ch) e.collectDeprecatedEnabledJailMetrics(ch)
e.collectUpMetric(ch) e.collectDeprecatedUpMetric(ch)
e.collectErrorCountMetric(ch) e.collectDeprecatedErrorCountMetric(ch)
} }
if e.socketPath != "" { if e.socketPath != "" {
s, err := socket.ConnectToSocket(e.socketPath) s, err := socket.ConnectToSocket(e.socketPath)
@ -136,26 +136,26 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.collectJailMetrics(ch, s) 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 var upMetricValue float64 = 1
if e.lastError != nil { if e.lastError != nil {
upMetricValue = 0 upMetricValue = 0
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricUp, prometheus.GaugeValue, upMetricValue, deprecatedMetricUp, prometheus.GaugeValue, upMetricValue,
) )
} }
func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) { func (e *Exporter) collectDeprecatedErrorCountMetric(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db", 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() jailNameToCountMap, err := e.db.CountBadIpsPerJail()
e.lastError = err e.lastError = err
@ -166,12 +166,12 @@ func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
for jailName, count := range jailNameToCountMap { for jailName, count := range jailNameToCountMap {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricBadIpsPerJail, prometheus.GaugeValue, float64(count), jailName, deprecatedMetricBadIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
) )
} }
} }
func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) { func (e *Exporter) collectDeprecatedBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
jailNameToCountMap, err := e.db.CountBannedIpsPerJail() jailNameToCountMap, err := e.db.CountBannedIpsPerJail()
e.lastError = err e.lastError = err
@ -182,12 +182,12 @@ func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
for jailName, count := range jailNameToCountMap { for jailName, count := range jailNameToCountMap {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricBannedIpsPerJail, prometheus.GaugeValue, float64(count), jailName, deprecatedMetricBannedIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
) )
} }
} }
func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) { func (e *Exporter) collectDeprecatedEnabledJailMetrics(ch chan<- prometheus.Metric) {
jailNameToEnabledMap, err := e.db.JailNameToEnabledValue() jailNameToEnabledMap, err := e.db.JailNameToEnabledValue()
e.lastError = err e.lastError = err
@ -198,20 +198,20 @@ func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) {
for jailName, count := range jailNameToEnabledMap { for jailName, count := range jailNameToEnabledMap {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricEnabledJails, prometheus.GaugeValue, float64(count), jailName, deprecatedMetricEnabledJails, prometheus.GaugeValue, float64(count), jailName,
) )
} }
} }
func (e *Exporter) collectErrorCountMetricNew(ch chan<- prometheus.Metric) { func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricErrorCountNew, prometheus.CounterValue, float64(e.dbErrorCount), "db", metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricErrorCountNew, prometheus.CounterValue, float64(e.socketConnectionErrorCount), "socket_conn", metricErrorCount, prometheus.CounterValue, float64(e.socketConnectionErrorCount), "socket_conn",
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricErrorCountNew, prometheus.CounterValue, float64(e.socketRequestErrorCount), "socket_req", metricErrorCount, prometheus.CounterValue, float64(e.socketRequestErrorCount), "socket_req",
) )
} }
@ -287,6 +287,7 @@ func main() {
exporter := &Exporter{} exporter := &Exporter{}
if appSettings.Fail2BanDbPath != "" { 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) exporter.db = fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath)
} }
if appSettings.Fail2BanSocketPath != "" { if appSettings.Fail2BanSocketPath != "" {