diff --git a/README.md b/README.md index 747e62a..2fd35df 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ Access exported metrics at `/metrics` (on the provided port). Exposed metrics: * `up` - Returns 1 if the service is up +* `enabled_jails` - Returns 1 for each jail that is enabled, 0 if disabled. * `bad_ips` (per jail) * A *bad IP* is defined as an IP that has been banned at least once in the past * Bad IPs are counted per jail @@ -120,4 +121,8 @@ fail2ban_banned_ips{jail="jail2"} 2 # HELP fail2ban_up Was the last fail2ban query successful. # TYPE fail2ban_up gauge fail2ban_up 1 +# HELP fail2ban_enabled_jails Enabled jails. +# TYPE fail2ban_enabled_jails gauge +fail2ban_enabled_jails{jail="jail1"} 1 +fail2ban_enabled_jails{jail="jail2"} 1 ``` diff --git a/src/db/db.go b/src/db/db.go index dc759eb..e4f8974 100644 --- a/src/db/db.go +++ b/src/db/db.go @@ -8,6 +8,7 @@ import ( const queryBadIpsPerJail = "SELECT j.name, (SELECT COUNT(1) FROM bips b WHERE j.name = b.jail) FROM jails j" const queryBannedIpsPerJail = "SELECT j.name, (SELECT COUNT(1) FROM bans b WHERE j.name = b.jail) FROM jails j" +const queryJailNameToEnabled = "SELECT j.name, j.enabled FROM jails j" type Fail2BanDB struct { DatabasePath string @@ -36,6 +37,10 @@ func (db *Fail2BanDB) CountBadIpsPerJail() (map[string]int, error) { return db.RunJailNameToCountQuery(queryBadIpsPerJail) } +func (db *Fail2BanDB) JailNameToEnabledValue() (map[string]int, error) { + return db.RunJailNameToCountQuery(queryJailNameToEnabled) +} + func (db *Fail2BanDB) RunJailNameToCountQuery(query string) (map[string]int, error) { stmt, err := db.sqliteDB.Prepare(query) defer db.mustCloseStatement(stmt) diff --git a/src/exporter.go b/src/exporter.go index df968c0..9a031c0 100644 --- a/src/exporter.go +++ b/src/exporter.go @@ -34,6 +34,11 @@ var ( "Number of bad IPs stored in the database (per jail).", []string{"jail"}, nil, ) + metricEnabledJails = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "enabled_jails"), + "Enabled jails.", + []string{"jail"}, nil, + ) ) type Exporter struct { @@ -44,6 +49,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { ch <- metricUp ch <- metricBadIpsPerJail ch <- metricBannedIpsPerJail + ch <- metricEnabledJails } func (e *Exporter) Collect(ch chan<- prometheus.Metric) { @@ -52,6 +58,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { ) e.collectBadIpsPerJailMetrics(ch) e.collectBannedIpsPerJailMetrics(ch) + e.collectEnabledJailMetrics(ch) } func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) { @@ -80,6 +87,19 @@ func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) { } } +func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) { + jailNameToEnabledMap, err := e.db.JailNameToEnabledValue() + if err != nil { + log.Print(err) + } + + for jailName, count := range jailNameToEnabledMap { + ch <- prometheus.MustNewConstMetric( + metricEnabledJails, prometheus.GaugeValue, float64(count), jailName, + ) + } +} + func printAppVersion() { fmt.Println(version) fmt.Printf(" build date: %s\r\n commit hash: %s\r\n built by: %s\r\n", date, commit, builtBy)