2021-02-05 22:49:47 +00:00
|
|
|
package main
|
|
|
|
|
2021-02-05 23:01:00 +00:00
|
|
|
import (
|
2021-02-06 11:45:46 +00:00
|
|
|
fail2banDb "fail2ban-prometheus-exporter/db"
|
|
|
|
_ "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"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const namespace = "fail2ban"
|
|
|
|
|
2021-02-06 11:45:46 +00:00
|
|
|
var (
|
|
|
|
db = fail2banDb.MustConnectToDb("fail2ban.sqlite3")
|
|
|
|
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-02-05 23:01:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Exporter struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
2021-02-06 11:45:46 +00:00
|
|
|
ch <- metricUp
|
2021-02-06 12:12:01 +00:00
|
|
|
ch <- metricBadIpsPerJail
|
2021-02-06 12:24:31 +00:00
|
|
|
ch <- metricBannedIpsPerJail
|
2021-02-05 23:01:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2021-02-06 11:45:46 +00:00
|
|
|
metricUp, prometheus.GaugeValue, 1,
|
|
|
|
)
|
2021-02-06 12:12:01 +00:00
|
|
|
collectBadIpsPerJailMetrics(ch)
|
2021-02-06 12:24:31 +00:00
|
|
|
collectBannedIpsPerJailMetrics(ch)
|
2021-02-06 11:45:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-06 12:12:01 +00:00
|
|
|
func collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
jailNameToCountMap, err := db.CountBadIpsPerJail()
|
|
|
|
if err != nil {
|
|
|
|
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 12:24:31 +00:00
|
|
|
func collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
|
|
|
|
jailNameToCountMap, err := db.CountBannedIpsPerJail()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for jailName, count := range jailNameToCountMap {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metricBannedIpsPerJail, prometheus.GaugeValue, float64(count), jailName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 22:49:47 +00:00
|
|
|
func main() {
|
2021-02-05 23:01:00 +00:00
|
|
|
log.Print("starting fail2ban exporter")
|
|
|
|
|
|
|
|
exporter := &Exporter{}
|
|
|
|
prometheus.MustRegister(exporter)
|
|
|
|
|
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
log.Fatal(http.ListenAndServe(":9101", nil))
|
2021-02-05 22:49:47 +00:00
|
|
|
}
|