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-08-29 13:50:53 +02:00
|
|
|
"fail2ban-prometheus-exporter/socket"
|
2021-02-06 16:17:35 +01:00
|
|
|
"fmt"
|
2021-08-29 13:50:53 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2021-08-29 13:50:53 +02:00
|
|
|
const (
|
|
|
|
namespace = "fail2ban"
|
|
|
|
sockNamespace = "f2b"
|
|
|
|
)
|
2021-02-06 00:01:00 +01:00
|
|
|
|
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-08-29 13:50:53 +02:00
|
|
|
metricServerPing = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(sockNamespace, "", "up"),
|
|
|
|
"Check if the fail2ban server is up",
|
|
|
|
nil, nil,
|
|
|
|
)
|
2021-02-06 00:01:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Exporter struct {
|
2021-04-07 22:46:41 +02:00
|
|
|
db *fail2banDb.Fail2BanDB
|
2021-08-29 13:50:53 +02:00
|
|
|
socket *socket.Fail2BanSocket
|
2021-04-07 22:46:41 +02:00
|
|
|
lastError error
|
|
|
|
dbErrorCount int
|
2021-02-06 00:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
2021-08-29 13:50:53 +02:00
|
|
|
if e.db != nil {
|
|
|
|
ch <- metricUp
|
|
|
|
ch <- metricBadIpsPerJail
|
|
|
|
ch <- metricBannedIpsPerJail
|
|
|
|
ch <- metricEnabledJails
|
|
|
|
ch <- metricErrorCount
|
|
|
|
}
|
|
|
|
if e.socket != nil {
|
|
|
|
ch <- metricServerPing
|
|
|
|
}
|
2021-02-06 00:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
2021-08-29 13:50:53 +02:00
|
|
|
if e.db != nil {
|
|
|
|
e.collectBadIpsPerJailMetrics(ch)
|
|
|
|
e.collectBannedIpsPerJailMetrics(ch)
|
|
|
|
e.collectEnabledJailMetrics(ch)
|
|
|
|
e.collectUpMetric(ch)
|
|
|
|
e.collectErrorCountMetric(ch)
|
|
|
|
}
|
|
|
|
if e.socket != nil {
|
|
|
|
e.collectServerPingMetric(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-08-29 13:50:53 +02:00
|
|
|
func (e *Exporter) collectServerPingMetric(ch chan<- prometheus.Metric) {
|
|
|
|
pingSuccess := e.socket.Ping()
|
|
|
|
var pingSuccessInt float64 = 1
|
|
|
|
if !pingSuccess {
|
|
|
|
pingSuccessInt = 0
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricServerPing, prometheus.GaugeValue, pingSuccessInt,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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-08-29 13:50:53 +02:00
|
|
|
exporter := &Exporter{}
|
|
|
|
if appSettings.Fail2BanDbPath != "" {
|
|
|
|
exporter.db = fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath)
|
|
|
|
}
|
|
|
|
if appSettings.Fail2BanSocketPath != "" {
|
|
|
|
exporter.socket = socket.MustConnectToSocket(appSettings.Fail2BanSocketPath)
|
2021-02-06 16:17:35 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|