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-09-13 21:25:54 +02:00
|
|
|
"fail2ban-prometheus-exporter/export"
|
2021-10-12 22:38:26 +02:00
|
|
|
"fail2ban-prometheus-exporter/textfile"
|
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-09-21 10:34:23 +02:00
|
|
|
const (
|
|
|
|
metricsPath = "/metrics"
|
|
|
|
)
|
|
|
|
|
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 00:01:00 +01:00
|
|
|
)
|
|
|
|
|
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-09-25 23:23:28 +02:00
|
|
|
func rootHtmlHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := w.Write([]byte(
|
|
|
|
`<html>
|
|
|
|
<head><title>Fail2Ban Exporter</title></head>
|
|
|
|
<body>
|
|
|
|
<h1>Fail2Ban Exporter</h1>
|
|
|
|
<p><a href="` + metricsPath + `">Metrics</a></p>
|
|
|
|
</body>
|
|
|
|
</html>`))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error handling root url: %v", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 22:38:26 +02:00
|
|
|
func metricHandler(w http.ResponseWriter, r *http.Request, collector *textfile.Collector) {
|
|
|
|
promhttp.Handler().ServeHTTP(w, r)
|
|
|
|
collector.WriteTextFileMetrics(w, r)
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-09-21 10:34:23 +02:00
|
|
|
addr := fmt.Sprintf("0.0.0.0:%d", appSettings.MetricsPort)
|
|
|
|
|
|
|
|
log.Printf("starting fail2ban exporter at %s", addr)
|
2021-02-06 00:01:00 +01:00
|
|
|
|
2021-09-13 21:25:54 +02:00
|
|
|
exporter := export.NewExporter(appSettings, version)
|
2021-02-06 16:17:35 +01:00
|
|
|
prometheus.MustRegister(exporter)
|
2021-02-06 00:01:00 +01:00
|
|
|
|
2021-10-12 22:38:26 +02:00
|
|
|
textFileCollector := textfile.NewCollector(appSettings)
|
|
|
|
prometheus.MustRegister(textFileCollector)
|
|
|
|
|
2021-09-25 23:23:28 +02:00
|
|
|
http.HandleFunc("/", rootHtmlHandler)
|
2021-10-12 22:38:26 +02:00
|
|
|
http.HandleFunc(metricsPath, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
metricHandler(w, r, textFileCollector)
|
|
|
|
})
|
2021-09-21 10:34:23 +02:00
|
|
|
log.Printf("metrics available at '%s'", metricsPath)
|
|
|
|
|
|
|
|
svrErr := make(chan error)
|
|
|
|
go func() {
|
|
|
|
svrErr <- http.ListenAndServe(addr, nil)
|
|
|
|
}()
|
|
|
|
log.Print("ready")
|
|
|
|
|
|
|
|
err := <-svrErr
|
|
|
|
log.Print(err)
|
2021-02-06 16:17:35 +01:00
|
|
|
}
|
2021-02-05 23:49:47 +01:00
|
|
|
}
|