add new version metric handler

Add a new handler to collect the exporter and fail2ban version data.
This commit is contained in:
Hector 2021-09-10 07:02:07 +01:00
parent d9f1ee33c8
commit 3f09e5af8c

View File

@ -85,6 +85,11 @@ var (
"Total number of IPs banned by this jail (includes expired bans)", "Total number of IPs banned by this jail (includes expired bans)",
[]string{"jail"}, nil, []string{"jail"}, nil,
) )
metricVersionInfo = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "version"),
"Version of the exporter and fail2ban server",
[]string{"exporter", "fail2ban"}, nil,
)
) )
type Exporter struct { type Exporter struct {
@ -135,6 +140,9 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
if err == nil && s != nil { if err == nil && s != nil {
e.collectJailMetrics(ch, s) e.collectJailMetrics(ch, s)
} }
e.collectVersionMetric(ch, s)
} else {
e.collectVersionMetric(ch, nil)
} }
e.collectErrorCountMetric(ch) e.collectErrorCountMetric(ch)
} }
@ -273,6 +281,17 @@ func (e *Exporter) collectJailStatsMetric(ch chan<- prometheus.Metric, s *socket
) )
} }
func (e *Exporter) collectVersionMetric(ch chan<- prometheus.Metric, s *socket.Fail2BanSocket) {
fail2banVersion := "9.9.9"
if s != nil {
// TODO: Get fail2ban server version
}
ch <- prometheus.MustNewConstMetric(
metricVersionInfo, prometheus.GaugeValue, float64(1), version, fail2banVersion,
)
}
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)