You've already forked prometheus-fail2ban-exporter
feat: collect new up metric from fail2ban socket
Add support for connecting the exporter directly to the fail2ban server's socket to send requests and receive data. The path to the socket file is optional and specified on startup. Export a new metric based on the response of the `ping` command sent to the fail2ban server. The metric is set to 1 if the server responds with `pong` and 0 in any other case. This metric is only shown if the path to the socket file was provided on startup.
This commit is contained in:
@ -3,15 +3,20 @@ package main
|
||||
import (
|
||||
"fail2ban-prometheus-exporter/cfg"
|
||||
fail2banDb "fail2ban-prometheus-exporter/db"
|
||||
"fail2ban-prometheus-exporter/socket"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const namespace = "fail2ban"
|
||||
const (
|
||||
namespace = "fail2ban"
|
||||
sockNamespace = "f2b"
|
||||
)
|
||||
|
||||
var (
|
||||
version = "dev"
|
||||
@ -44,28 +49,44 @@ var (
|
||||
"Number of errors found since startup.",
|
||||
[]string{"type"}, nil,
|
||||
)
|
||||
metricServerPing = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(sockNamespace, "", "up"),
|
||||
"Check if the fail2ban server is up",
|
||||
nil, nil,
|
||||
)
|
||||
)
|
||||
|
||||
type Exporter struct {
|
||||
db *fail2banDb.Fail2BanDB
|
||||
socket *socket.Fail2BanSocket
|
||||
lastError error
|
||||
dbErrorCount int
|
||||
}
|
||||
|
||||
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
||||
ch <- metricUp
|
||||
ch <- metricBadIpsPerJail
|
||||
ch <- metricBannedIpsPerJail
|
||||
ch <- metricEnabledJails
|
||||
ch <- metricErrorCount
|
||||
if e.db != nil {
|
||||
ch <- metricUp
|
||||
ch <- metricBadIpsPerJail
|
||||
ch <- metricBannedIpsPerJail
|
||||
ch <- metricEnabledJails
|
||||
ch <- metricErrorCount
|
||||
}
|
||||
if e.socket != nil {
|
||||
ch <- metricServerPing
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
||||
e.collectBadIpsPerJailMetrics(ch)
|
||||
e.collectBannedIpsPerJailMetrics(ch)
|
||||
e.collectEnabledJailMetrics(ch)
|
||||
e.collectUpMetric(ch)
|
||||
e.collectErrorCountMetric(ch)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Exporter) collectUpMetric(ch chan<- prometheus.Metric) {
|
||||
@ -132,6 +153,17 @@ func (e *Exporter) collectEnabledJailMetrics(ch chan<- prometheus.Metric) {
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
|
||||
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)
|
||||
@ -144,8 +176,12 @@ func main() {
|
||||
} else {
|
||||
log.Print("starting fail2ban exporter")
|
||||
|
||||
exporter := &Exporter{
|
||||
db: fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath),
|
||||
exporter := &Exporter{}
|
||||
if appSettings.Fail2BanDbPath != "" {
|
||||
exporter.db = fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath)
|
||||
}
|
||||
if appSettings.Fail2BanSocketPath != "" {
|
||||
exporter.socket = socket.MustConnectToSocket(appSettings.Fail2BanSocketPath)
|
||||
}
|
||||
prometheus.MustRegister(exporter)
|
||||
|
||||
|
Reference in New Issue
Block a user