2021-02-05 22:49:47 +00:00
|
|
|
package main
|
|
|
|
|
2021-02-05 23:01:00 +00:00
|
|
|
import (
|
2021-02-06 15:17:35 +00:00
|
|
|
"fail2ban-prometheus-exporter/cfg"
|
2021-02-06 11:45:46 +00:00
|
|
|
fail2banDb "fail2ban-prometheus-exporter/db"
|
2021-08-29 11:50:53 +00:00
|
|
|
"fail2ban-prometheus-exporter/socket"
|
2021-02-06 15:17:35 +00:00
|
|
|
"fmt"
|
2021-08-29 11:50:53 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2021-02-06 11:45:46 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2021-02-05 23:01:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
|
|
|
|
2021-08-29 11:50:53 +00:00
|
|
|
const (
|
|
|
|
namespace = "fail2ban"
|
|
|
|
sockNamespace = "f2b"
|
|
|
|
)
|
2021-02-05 23:01:00 +00:00
|
|
|
|
2021-02-06 11:45:46 +00:00
|
|
|
var (
|
2021-02-06 15:17:35 +00:00
|
|
|
version = "dev"
|
|
|
|
commit = "none"
|
|
|
|
date = "unknown"
|
|
|
|
builtBy = "unknown"
|
|
|
|
|
2021-02-06 11:45:46 +00:00
|
|
|
metricUp = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "", "up"),
|
|
|
|
"Was the last fail2ban query successful.",
|
|
|
|
nil, nil,
|
|
|
|
)
|
2021-02-06 12:24:31 +00: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 12:12:01 +00: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 11:45:46 +00:00
|
|
|
)
|
2021-04-07 17:55:34 +00:00
|
|
|
metricEnabledJails = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "", "enabled_jails"),
|
|
|
|
"Enabled jails.",
|
|
|
|
[]string{"jail"}, nil,
|
|
|
|
)
|
2021-04-07 20:46:41 +00:00
|
|
|
metricErrorCount = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "", "errors"),
|
|
|
|
"Number of errors found since startup.",
|
|
|
|
[]string{"type"}, nil,
|
|
|
|
)
|
2021-08-29 16:54:20 +00:00
|
|
|
|
2021-08-29 11:50:53 +00:00
|
|
|
metricServerPing = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(sockNamespace, "", "up"),
|
|
|
|
"Check if the fail2ban server is up",
|
|
|
|
nil, nil,
|
|
|
|
)
|
2021-08-29 16:54:20 +00:00
|
|
|
metricJailCount = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(sockNamespace, "", "jail_count"),
|
|
|
|
"Number of defined jails",
|
|
|
|
nil, nil,
|
|
|
|
)
|
|
|
|
metricJailFailedCurrent = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(sockNamespace, "", "jail_failed_current"),
|
|
|
|
"Number of current failures on this jail's filter",
|
|
|
|
[]string{"jail"}, nil,
|
|
|
|
)
|
|
|
|
metricJailFailedTotal = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(sockNamespace, "", "jail_failed_total"),
|
|
|
|
"Number of total failures on this jail's filter",
|
|
|
|
[]string{"jail"}, nil,
|
|
|
|
)
|
|
|
|
metricJailBannedCurrent = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(sockNamespace, "", "jail_banned_current"),
|
|
|
|
"Number of IPs currently banned in this jail",
|
|
|
|
[]string{"jail"}, nil,
|
|
|
|
)
|
|
|
|
metricJailBannedTotal = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(sockNamespace, "", "jail_banned_total"),
|
|
|
|
"Total number of IPs banned by this jail (includes expired bans)",
|
|
|
|
[]string{"jail"}, nil,
|
|
|
|
)
|
2021-02-05 23:01:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Exporter struct {
|
2021-04-07 20:46:41 +00:00
|
|
|
db *fail2banDb.Fail2BanDB
|
2021-08-29 11:50:53 +00:00
|
|
|
socket *socket.Fail2BanSocket
|
2021-04-07 20:46:41 +00:00
|
|
|
lastError error
|
|
|
|
dbErrorCount int
|
2021-02-05 23:01:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
2021-08-29 11:50:53 +00:00
|
|
|
if e.db != nil {
|
|
|
|
ch <- metricUp
|
|
|
|
ch <- metricBadIpsPerJail
|
|
|
|
ch <- metricBannedIpsPerJail
|
|
|
|
ch <- metricEnabledJails
|
|
|
|
ch <- metricErrorCount
|
|
|
|
}
|
|
|
|
if e.socket != nil {
|
|
|
|
ch <- metricServerPing
|
2021-08-29 16:54:20 +00:00
|
|
|
ch <- metricJailCount
|
|
|
|
ch <- metricJailFailedCurrent
|
|
|
|
ch <- metricJailFailedTotal
|
|
|
|
ch <- metricJailBannedCurrent
|
|
|
|
ch <- metricJailBannedTotal
|
2021-08-29 11:50:53 +00:00
|
|
|
}
|
2021-02-05 23:01:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
2021-08-29 11:50:53 +00: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-08-29 16:54:20 +00:00
|
|
|
e.collectJailMetrics(ch)
|
2021-08-29 11:50:53 +00:00
|
|
|
}
|
2021-04-07 20:32:49 +00: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 11:45:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 20:46:41 +00:00
|
|
|
func (e *Exporter) collectErrorCountMetric(ch chan<- prometheus.Metric) {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricErrorCount, prometheus.CounterValue, float64(e.dbErrorCount), "db",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-06 15:17:35 +00:00
|
|
|
func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
jailNameToCountMap, err := e.db.CountBadIpsPerJail()
|
2021-04-07 20:32:49 +00:00
|
|
|
e.lastError = err
|
|
|
|
|
2021-02-06 12:12:01 +00:00
|
|
|
if err != nil {
|
2021-04-07 20:46:41 +00:00
|
|
|
e.dbErrorCount++
|
2021-02-06 12:12:01 +00:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for jailName, count := range jailNameToCountMap {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricBadIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
|
|
|
)
|
|
|
|
}
|
2021-02-05 23:01:00 +00:00
|
|
|
}
|
2021-02-05 22:49:47 +00:00
|
|
|
|
2021-02-06 15:17:35 +00:00
|
|
|
func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
jailNameToCountMap, err := e.db.CountBannedIpsPerJail()
|
2021-04-07 20:32:49 +00:00
|
|
|
e.lastError = err
|
|
|
|
|
2021-02-06 12:24:31 +00:00
|
|
|
if err != nil {
|
2021-04-07 20:46:41 +00:00
|
|
|
e.dbErrorCount++
|
2021-02-06 12:24:31 +00:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for jailName, count := range jailNameToCountMap {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricBannedIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:55:34 +00:00
|
|
|
func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
jailNameToEnabledMap, err := e.db.JailNameToEnabledValue()
|
2021-04-07 20:32:49 +00:00
|
|
|
e.lastError = err
|
|
|
|
|
2021-04-07 17:55:34 +00:00
|
|
|
if err != nil {
|
2021-04-07 20:46:41 +00:00
|
|
|
e.dbErrorCount++
|
2021-04-07 17:55:34 +00:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for jailName, count := range jailNameToEnabledMap {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricEnabledJails, prometheus.GaugeValue, float64(count), jailName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:50:53 +00: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-08-29 16:54:20 +00:00
|
|
|
func (e *Exporter) collectJailMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
jails, err := e.socket.GetJails()
|
|
|
|
var count float64 = 0
|
|
|
|
if err == nil {
|
|
|
|
count = float64(len(jails))
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricJailCount, prometheus.GaugeValue, count,
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := range jails {
|
|
|
|
e.collectJailStatsMetric(ch, jails[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, jail string) {
|
|
|
|
stats, err := e.socket.GetJailStats(jail)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to get stats for jail %s: %v", jail, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricJailFailedCurrent, prometheus.GaugeValue, float64(stats.FailedCurrent), jail,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricJailFailedTotal, prometheus.GaugeValue, float64(stats.FailedTotal), jail,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricJailBannedCurrent, prometheus.GaugeValue, float64(stats.BannedCurrent), jail,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricJailBannedTotal, prometheus.GaugeValue, float64(stats.BannedTotal), jail,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-06 15:17:35 +00: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 22:49:47 +00:00
|
|
|
func main() {
|
2021-02-06 15:17:35 +00:00
|
|
|
appSettings := cfg.Parse()
|
|
|
|
if appSettings.VersionMode {
|
|
|
|
printAppVersion()
|
|
|
|
} else {
|
|
|
|
log.Print("starting fail2ban exporter")
|
2021-02-05 23:01:00 +00:00
|
|
|
|
2021-08-29 11:50:53 +00:00
|
|
|
exporter := &Exporter{}
|
|
|
|
if appSettings.Fail2BanDbPath != "" {
|
|
|
|
exporter.db = fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath)
|
|
|
|
}
|
|
|
|
if appSettings.Fail2BanSocketPath != "" {
|
|
|
|
exporter.socket = socket.MustConnectToSocket(appSettings.Fail2BanSocketPath)
|
2021-02-06 15:17:35 +00:00
|
|
|
}
|
|
|
|
prometheus.MustRegister(exporter)
|
2021-02-05 23:01:00 +00:00
|
|
|
|
2021-02-06 15:17:35 +00:00
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", appSettings.MetricsPort), nil))
|
|
|
|
}
|
2021-02-05 22:49:47 +00:00
|
|
|
}
|