2021-02-05 23:49:47 +01:00
|
|
|
package main
|
|
|
|
|
2021-02-06 00:01:00 +01:00
|
|
|
import (
|
2021-02-06 16:17:35 +01:00
|
|
|
"fail2ban-prometheus-exporter/cfg"
|
2021-02-06 12:45:46 +01:00
|
|
|
fail2banDb "fail2ban-prometheus-exporter/db"
|
2021-02-06 16:17:35 +01:00
|
|
|
"fmt"
|
2021-02-06 12:45:46 +01:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2021-02-06 00:01:00 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const namespace = "fail2ban"
|
|
|
|
|
2021-02-06 12:45:46 +01:00
|
|
|
var (
|
2021-02-06 16:17:35 +01:00
|
|
|
version = "dev"
|
|
|
|
commit = "none"
|
|
|
|
date = "unknown"
|
|
|
|
builtBy = "unknown"
|
|
|
|
|
2021-02-06 12:45:46 +01:00
|
|
|
metricUp = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "", "up"),
|
|
|
|
"Was the last fail2ban query successful.",
|
|
|
|
nil, nil,
|
|
|
|
)
|
2021-02-06 13:24:31 +01:00
|
|
|
metricBannedIpsPerJail = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "", "banned_ips"),
|
|
|
|
"Number of banned IPs stored in the database (per jail).",
|
|
|
|
[]string{"jail"}, nil,
|
|
|
|
)
|
2021-02-06 13:12:01 +01:00
|
|
|
metricBadIpsPerJail = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "", "bad_ips"),
|
|
|
|
"Number of bad IPs stored in the database (per jail).",
|
|
|
|
[]string{"jail"}, nil,
|
2021-02-06 12:45:46 +01:00
|
|
|
)
|
2021-04-07 19:55:34 +02:00
|
|
|
metricEnabledJails = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "", "enabled_jails"),
|
|
|
|
"Enabled jails.",
|
|
|
|
[]string{"jail"}, nil,
|
|
|
|
)
|
2021-04-07 22:46:41 +02:00
|
|
|
metricErrorCount = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "", "errors"),
|
|
|
|
"Number of errors found since startup.",
|
|
|
|
[]string{"type"}, nil,
|
|
|
|
)
|
2021-02-06 00:01:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Exporter struct {
|
2021-04-07 22:46:41 +02:00
|
|
|
db *fail2banDb.Fail2BanDB
|
|
|
|
lastError error
|
|
|
|
dbErrorCount int
|
2021-02-06 00:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
2021-02-06 12:45:46 +01:00
|
|
|
ch <- metricUp
|
2021-02-06 13:12:01 +01:00
|
|
|
ch <- metricBadIpsPerJail
|
2021-02-06 13:24:31 +01:00
|
|
|
ch <- metricBannedIpsPerJail
|
2021-04-07 19:55:34 +02:00
|
|
|
ch <- metricEnabledJails
|
2021-04-07 22:46:41 +02:00
|
|
|
ch <- metricErrorCount
|
2021-02-06 00:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
2021-02-06 16:17:35 +01:00
|
|
|
e.collectBadIpsPerJailMetrics(ch)
|
|
|
|
e.collectBannedIpsPerJailMetrics(ch)
|
2021-04-07 19:55:34 +02:00
|
|
|
e.collectEnabledJailMetrics(ch)
|
2021-04-07 22:32:49 +02:00
|
|
|
e.collectUpMetric(ch)
|
2021-04-07 22:46:41 +02:00
|
|
|
e.collectErrorCountMetric(ch)
|
2021-04-07 22:32:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) collectUpMetric(ch chan<- prometheus.Metric) {
|
|
|
|
var upMetricValue float64 = 1
|
|
|
|
if e.lastError != nil {
|
|
|
|
upMetricValue = 0
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricUp, prometheus.GaugeValue, upMetricValue,
|
|
|
|
)
|
2021-02-06 12:45:46 +01:00
|
|
|
}
|
|
|
|
|
2021-04-07 22:46:41 +02:00
|
|
|
func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:17:35 +01:00
|
|
|
func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
jailNameToCountMap, err := e.db.CountBadIpsPerJail()
|
2021-04-07 22:32:49 +02:00
|
|
|
e.lastError = err
|
|
|
|
|
2021-02-06 13:12:01 +01:00
|
|
|
if err != nil {
|
2021-04-07 22:46:41 +02:00
|
|
|
e.dbErrorCount++
|
2021-02-06 13:12:01 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for jailName, count := range jailNameToCountMap {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricBadIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
|
|
|
)
|
|
|
|
}
|
2021-02-06 00:01:00 +01:00
|
|
|
}
|
2021-02-05 23:49:47 +01:00
|
|
|
|
2021-02-06 16:17:35 +01:00
|
|
|
func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
jailNameToCountMap, err := e.db.CountBannedIpsPerJail()
|
2021-04-07 22:32:49 +02:00
|
|
|
e.lastError = err
|
|
|
|
|
2021-02-06 13:24:31 +01:00
|
|
|
if err != nil {
|
2021-04-07 22:46:41 +02:00
|
|
|
e.dbErrorCount++
|
2021-02-06 13:24:31 +01:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for jailName, count := range jailNameToCountMap {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricBannedIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 19:55:34 +02:00
|
|
|
func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
jailNameToEnabledMap, err := e.db.JailNameToEnabledValue()
|
2021-04-07 22:32:49 +02:00
|
|
|
e.lastError = err
|
|
|
|
|
2021-04-07 19:55:34 +02:00
|
|
|
if err != nil {
|
2021-04-07 22:46:41 +02:00
|
|
|
e.dbErrorCount++
|
2021-04-07 19:55:34 +02:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for jailName, count := range jailNameToEnabledMap {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricEnabledJails, prometheus.GaugeValue, float64(count), jailName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:17:35 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-02-05 23:49:47 +01:00
|
|
|
func main() {
|
2021-02-06 16:17:35 +01:00
|
|
|
appSettings := cfg.Parse()
|
|
|
|
if appSettings.VersionMode {
|
|
|
|
printAppVersion()
|
|
|
|
} else {
|
|
|
|
log.Print("starting fail2ban exporter")
|
2021-02-06 00:01:00 +01:00
|
|
|
|
2021-02-06 16:17:35 +01:00
|
|
|
exporter := &Exporter{
|
|
|
|
db: fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath),
|
|
|
|
}
|
|
|
|
prometheus.MustRegister(exporter)
|
2021-02-06 00:01:00 +01:00
|
|
|
|
2021-02-06 16:17:35 +01:00
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", appSettings.MetricsPort), nil))
|
|
|
|
}
|
2021-02-05 23:49:47 +01:00
|
|
|
}
|