You've already forked prometheus-fail2ban-exporter
							
							Merge branch '1-export-metrics-on-enabled-disabled-jails' into 'main'
Resolve "Export metrics on enabled/disabled jails" Closes #1 See merge request hectorjsmith/fail2ban-prometheus-exporter!18
This commit is contained in:
		@@ -99,6 +99,7 @@ Access exported metrics at `/metrics` (on the provided port).
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
Exposed metrics:
 | 
					Exposed metrics:
 | 
				
			||||||
* `up` - Returns 1 if the service is up
 | 
					* `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)
 | 
					* `bad_ips` (per jail)
 | 
				
			||||||
    * A *bad IP* is defined as an IP that has been banned at least once in the past
 | 
					    * A *bad IP* is defined as an IP that has been banned at least once in the past
 | 
				
			||||||
    * Bad IPs are counted per jail
 | 
					    * 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.
 | 
					# HELP fail2ban_up Was the last fail2ban query successful.
 | 
				
			||||||
# TYPE fail2ban_up gauge
 | 
					# TYPE fail2ban_up gauge
 | 
				
			||||||
fail2ban_up 1
 | 
					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
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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 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 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 {
 | 
					type Fail2BanDB struct {
 | 
				
			||||||
	DatabasePath string
 | 
						DatabasePath string
 | 
				
			||||||
@@ -36,6 +37,10 @@ func (db *Fail2BanDB) CountBadIpsPerJail() (map[string]int, error) {
 | 
				
			|||||||
	return db.RunJailNameToCountQuery(queryBadIpsPerJail)
 | 
						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) {
 | 
					func (db *Fail2BanDB) RunJailNameToCountQuery(query string) (map[string]int, error) {
 | 
				
			||||||
	stmt, err := db.sqliteDB.Prepare(query)
 | 
						stmt, err := db.sqliteDB.Prepare(query)
 | 
				
			||||||
	defer db.mustCloseStatement(stmt)
 | 
						defer db.mustCloseStatement(stmt)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,6 +34,11 @@ var (
 | 
				
			|||||||
		"Number of bad IPs stored in the database (per jail).",
 | 
							"Number of bad IPs stored in the database (per jail).",
 | 
				
			||||||
		[]string{"jail"}, nil,
 | 
							[]string{"jail"}, nil,
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
						metricEnabledJails = prometheus.NewDesc(
 | 
				
			||||||
 | 
							prometheus.BuildFQName(namespace, "", "enabled_jails"),
 | 
				
			||||||
 | 
							"Enabled jails.",
 | 
				
			||||||
 | 
							[]string{"jail"}, nil,
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Exporter struct {
 | 
					type Exporter struct {
 | 
				
			||||||
@@ -44,6 +49,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
 | 
				
			|||||||
	ch <- metricUp
 | 
						ch <- metricUp
 | 
				
			||||||
	ch <- metricBadIpsPerJail
 | 
						ch <- metricBadIpsPerJail
 | 
				
			||||||
	ch <- metricBannedIpsPerJail
 | 
						ch <- metricBannedIpsPerJail
 | 
				
			||||||
 | 
						ch <- metricEnabledJails
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
 | 
					func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
 | 
				
			||||||
@@ -52,6 +58,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
 | 
				
			|||||||
	)
 | 
						)
 | 
				
			||||||
	e.collectBadIpsPerJailMetrics(ch)
 | 
						e.collectBadIpsPerJailMetrics(ch)
 | 
				
			||||||
	e.collectBannedIpsPerJailMetrics(ch)
 | 
						e.collectBannedIpsPerJailMetrics(ch)
 | 
				
			||||||
 | 
						e.collectEnabledJailMetrics(ch)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
 | 
					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() {
 | 
					func printAppVersion() {
 | 
				
			||||||
	fmt.Println(version)
 | 
						fmt.Println(version)
 | 
				
			||||||
	fmt.Printf("    build date:  %s\r\n    commit hash: %s\r\n    built by:    %s\r\n", date, commit, builtBy)
 | 
						fmt.Printf("    build date:  %s\r\n    commit hash: %s\r\n    built by:    %s\r\n", date, commit, builtBy)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user