Merge branch '4-configure-tool-using-cli-parameters' into 'main'

Resolve "Configure tool using CLI parameters"

Closes #4

See merge request hectorjsmith/fail2ban-prometheus-exporter!3
This commit is contained in:
Hector 2021-02-06 15:22:07 +00:00
commit 188626198f
3 changed files with 83 additions and 16 deletions

48
cfg/cfg.go Normal file
View file

@ -0,0 +1,48 @@
package cfg
import (
"flag"
"fmt"
"os"
)
const (
minServerPort = 1000
maxServerPort = 65535
)
type AppSettings struct {
VersionMode bool
MetricsPort int
Fail2BanDbPath string
}
func Parse() *AppSettings {
appSettings := &AppSettings{}
flag.BoolVar(&appSettings.VersionMode, "version", false, "show version info and exit")
flag.IntVar(&appSettings.MetricsPort, "port", 9191, "port to use for the metrics server")
flag.StringVar(&appSettings.Fail2BanDbPath, "db", "", "path to the fail2ban sqlite database")
flag.Parse()
appSettings.validateFlags()
return appSettings
}
func (settings *AppSettings) validateFlags() {
var flagsValid = true
if !settings.VersionMode {
if settings.Fail2BanDbPath == "" {
fmt.Println("missing flag 'db'")
flagsValid = false
}
if settings.MetricsPort < minServerPort || settings.MetricsPort > maxServerPort {
fmt.Printf("invalid server port, must be within %d and %d (found %d)\n",
minServerPort, maxServerPort, settings.MetricsPort)
flagsValid = false
}
}
if !flagsValid {
flag.Usage()
os.Exit(1)
}
}

View file

@ -10,7 +10,7 @@ const queryBannedIpsPerJail = "SELECT j.name, (SELECT COUNT(1) FROM bans b WHERE
type Fail2BanDB struct { type Fail2BanDB struct {
DatabasePath string DatabasePath string
sqliteDB *sql.DB sqliteDB *sql.DB
} }
func MustConnectToDb(databasePath string) *Fail2BanDB { func MustConnectToDb(databasePath string) *Fail2BanDB {

View file

@ -1,7 +1,9 @@
package main package main
import ( import (
"fail2ban-prometheus-exporter/cfg"
fail2banDb "fail2ban-prometheus-exporter/db" fail2banDb "fail2ban-prometheus-exporter/db"
"fmt"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
@ -12,7 +14,11 @@ import (
const namespace = "fail2ban" const namespace = "fail2ban"
var ( var (
db = fail2banDb.MustConnectToDb("fail2ban.sqlite3") version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
metricUp = prometheus.NewDesc( metricUp = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"), prometheus.BuildFQName(namespace, "", "up"),
"Was the last fail2ban query successful.", "Was the last fail2ban query successful.",
@ -31,6 +37,7 @@ var (
) )
type Exporter struct { type Exporter struct {
db *fail2banDb.Fail2BanDB
} }
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
@ -43,12 +50,12 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
metricUp, prometheus.GaugeValue, 1, metricUp, prometheus.GaugeValue, 1,
) )
collectBadIpsPerJailMetrics(ch) e.collectBadIpsPerJailMetrics(ch)
collectBannedIpsPerJailMetrics(ch) e.collectBannedIpsPerJailMetrics(ch)
} }
func collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) { func (e *Exporter) collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
jailNameToCountMap, err := db.CountBadIpsPerJail() jailNameToCountMap, err := e.db.CountBadIpsPerJail()
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
@ -60,8 +67,8 @@ func collectBadIpsPerJailMetrics(ch chan<- prometheus.Metric) {
} }
} }
func collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) { func (e *Exporter) collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
jailNameToCountMap, err := db.CountBannedIpsPerJail() jailNameToCountMap, err := e.db.CountBannedIpsPerJail()
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
@ -73,12 +80,24 @@ func collectBannedIpsPerJailMetrics(ch chan<- prometheus.Metric) {
} }
} }
func main() { func printAppVersion() {
log.Print("starting fail2ban exporter") fmt.Println(version)
fmt.Printf(" build date: %s\r\n commit hash: %s\r\n built by: %s\r\n", date, commit, builtBy)
exporter := &Exporter{} }
prometheus.MustRegister(exporter)
func main() {
http.Handle("/metrics", promhttp.Handler()) appSettings := cfg.Parse()
log.Fatal(http.ListenAndServe(":9101", nil)) if appSettings.VersionMode {
printAppVersion()
} else {
log.Print("starting fail2ban exporter")
exporter := &Exporter{
db: fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath),
}
prometheus.MustRegister(exporter)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", appSettings.MetricsPort), nil))
}
} }