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:
parent
556c09c2f4
commit
a816558d49
@ -3,15 +3,20 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fail2ban-prometheus-exporter/cfg"
|
"fail2ban-prometheus-exporter/cfg"
|
||||||
fail2banDb "fail2ban-prometheus-exporter/db"
|
fail2banDb "fail2ban-prometheus-exporter/db"
|
||||||
|
"fail2ban-prometheus-exporter/socket"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
_ "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"
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const namespace = "fail2ban"
|
const (
|
||||||
|
namespace = "fail2ban"
|
||||||
|
sockNamespace = "f2b"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "dev"
|
version = "dev"
|
||||||
@ -44,10 +49,16 @@ var (
|
|||||||
"Number of errors found since startup.",
|
"Number of errors found since startup.",
|
||||||
[]string{"type"}, nil,
|
[]string{"type"}, nil,
|
||||||
)
|
)
|
||||||
|
metricServerPing = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(sockNamespace, "", "up"),
|
||||||
|
"Check if the fail2ban server is up",
|
||||||
|
nil, nil,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
type Exporter struct {
|
type Exporter struct {
|
||||||
db *fail2banDb.Fail2BanDB
|
db *fail2banDb.Fail2BanDB
|
||||||
|
socket *socket.Fail2BanSocket
|
||||||
lastError error
|
lastError error
|
||||||
dbErrorCount int
|
dbErrorCount int
|
||||||
}
|
}
|
||||||
@ -58,6 +69,7 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
|||||||
ch <- metricBannedIpsPerJail
|
ch <- metricBannedIpsPerJail
|
||||||
ch <- metricEnabledJails
|
ch <- metricEnabledJails
|
||||||
ch <- metricErrorCount
|
ch <- metricErrorCount
|
||||||
|
ch <- metricServerPing
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
||||||
@ -66,6 +78,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
|||||||
e.collectEnabledJailMetrics(ch)
|
e.collectEnabledJailMetrics(ch)
|
||||||
e.collectUpMetric(ch)
|
e.collectUpMetric(ch)
|
||||||
e.collectErrorCountMetric(ch)
|
e.collectErrorCountMetric(ch)
|
||||||
|
e.collectServerPingMetric(ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exporter) collectUpMetric(ch chan<- prometheus.Metric) {
|
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() {
|
func printAppVersion() {
|
||||||
fmt.Println(version)
|
fmt.Println(version)
|
||||||
fmt.Printf(" build date: %s\r\n commit hash: %s\r\n built by: %s\r\n", date, commit, builtBy)
|
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{
|
exporter := &Exporter{
|
||||||
db: fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath),
|
db: fail2banDb.MustConnectToDb(appSettings.Fail2BanDbPath),
|
||||||
|
socket: socket.MustConnectToSocket(appSettings.Fail2BanSocketPath),
|
||||||
}
|
}
|
||||||
prometheus.MustRegister(exporter)
|
prometheus.MustRegister(exporter)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user