feat: support for textfile metrics (#13)

Add support for collecting arbitrary metrics from a textfile as well as
metrics collected from fail2ban. This allows other data to be exported
along with the fail2ban metrics (e.g. instance metadata).
Update the docker image to allow mounting a folder with a collection of
metric files to be exported. Only files ending in `.prom` with be read.
Update project README with the new functionality.
This commit is contained in:
Hector
2021-10-12 20:38:26 +00:00
parent 351d3344f7
commit 5a107cc547
7 changed files with 180 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"fail2ban-prometheus-exporter/cfg"
"fail2ban-prometheus-exporter/export"
"fail2ban-prometheus-exporter/textfile"
"fmt"
"log"
"net/http"
@ -43,6 +44,11 @@ func rootHtmlHandler(w http.ResponseWriter, r *http.Request) {
}
}
func metricHandler(w http.ResponseWriter, r *http.Request, collector *textfile.Collector) {
promhttp.Handler().ServeHTTP(w, r)
collector.WriteTextFileMetrics(w, r)
}
func main() {
appSettings := cfg.Parse()
if appSettings.VersionMode {
@ -55,8 +61,13 @@ func main() {
exporter := export.NewExporter(appSettings, version)
prometheus.MustRegister(exporter)
textFileCollector := textfile.NewCollector(appSettings)
prometheus.MustRegister(textFileCollector)
http.HandleFunc("/", rootHtmlHandler)
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc(metricsPath, func(w http.ResponseWriter, r *http.Request) {
metricHandler(w, r, textFileCollector)
})
log.Printf("metrics available at '%s'", metricsPath)
svrErr := make(chan error)