new metric for response of server ping

Add a new metric to report back the response of the `ping` command.
This commit is contained in:
Hector 2021-08-29 11:28:07 +01:00
parent 556c09c2f4
commit a816558d49

View File

@ -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,10 +49,16 @@ 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
}
@ -58,6 +69,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- metricBannedIpsPerJail
ch <- metricEnabledJails
ch <- metricErrorCount
ch <- metricServerPing
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
@ -66,6 +78,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.collectEnabledJailMetrics(ch)
e.collectUpMetric(ch)
e.collectErrorCountMetric(ch)
e.collectServerPingMetric(ch)
}
func (e *Exporter) collectUpMetric(ch chan<- prometheus.Metric) {
@ -132,6 +145,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)
@ -146,6 +170,7 @@ func main() {
exporter := &Exporter{
db: fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath),
socket: socket.MustConnectToSocket(appSettings.Fail2BanSocketPath),
}
prometheus.MustRegister(exporter)