From 6d28285924317aef3707c3513705bb1fea1108a4 Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Mon, 20 Jul 2020 18:43:03 -0400 Subject: [PATCH] Add libvirt version info Change-Id: I79e277f1eb31a5784d407e4c426c9b952f3c1334 --- collectors/version.go | 84 +++++++++++++++++++++++++++++++++++++++++++ libvirtd_exporter.go | 6 ++++ 2 files changed, 90 insertions(+) create mode 100644 collectors/version.go diff --git a/collectors/version.go b/collectors/version.go new file mode 100644 index 0000000..d298db0 --- /dev/null +++ b/collectors/version.go @@ -0,0 +1,84 @@ +// Copyright 2019 VEXXHOST, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package collectors + +import ( + "fmt" + + "github.com/libvirt/libvirt-go" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/common/log" +) + +type VersionCollector struct { + prometheus.Collector + + Connection *libvirt.Connect + + Version *prometheus.Desc +} + +func NewVersionCollector(conn *libvirt.Connect) (*VersionCollector, error) { + return &VersionCollector{ + Connection: conn, + + Version: prometheus.NewDesc( + "libvirtd_info", + "Version details for LibvirtD", + []string{"driver", "driver_version", "version"}, nil, + ), + }, nil +} + +func (c *VersionCollector) Describe(ch chan<- *prometheus.Desc) { + ch <- c.Version +} + +func (c *VersionCollector) Collect(ch chan<- prometheus.Metric) { + hypervisorType, err := c.Connection.GetType() + if err != nil { + log.Errorln(err) + return + } + + hypervisorVersion, err := c.Connection.GetVersion() + if err != nil { + log.Errorln(err) + return + } + + libvirtVersion, err := c.Connection.GetLibVersion() + if err != nil { + log.Errorln(err) + return + } + + ch <- prometheus.MustNewConstMetric( + c.Version, + prometheus.CounterValue, + float64(1), + hypervisorType, + versionToString(hypervisorVersion), + versionToString(libvirtVersion), + ) +} + +func versionToString(version uint32) string { + major := version / 1000000 + minor := (version - (major * 1000000)) / 1000 + release := version - (major * 1000000) - (minor * 1000) + + return fmt.Sprintf("%d.%d.%d", major, minor, release) +} diff --git a/libvirtd_exporter.go b/libvirtd_exporter.go index 6902d21..9e5fd25 100644 --- a/libvirtd_exporter.go +++ b/libvirtd_exporter.go @@ -57,12 +57,18 @@ func main() { } defer conn.Close() + versionCollector, err := collectors.NewVersionCollector(conn) + if err != nil { + log.Fatalln(err) + } + domainStats, err := collectors.NewDomainStatsCollector(conn, *libvirtNova) if err != nil { log.Fatalln(err) } prometheus.MustRegister(domainStats) + prometheus.MustRegister(versionCollector) http.Handle("/metrics", promhttp.Handler()) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {