feat: add SystemdVirtualization type (#3254)

* feat: add SystemdVirtualization type
---------

Signed-off-by: IbraAoad <Ibrahim.Awwad@canonical.com>
Signed-off-by: Ibrahim Awwad <ibraaoad@gmail.com>
Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
Signed-off-by: Alexander Soelberg Heidarsson <89837986+alex5517@users.noreply.github.com>
Co-authored-by: Ben Kochie <superq@gmail.com>
Co-authored-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
Co-authored-by: Alexander Soelberg Heidarsson <89837986+alex5517@users.noreply.github.com>
master
Ibrahim Awwad 2025-02-23 14:26:37 +02:00 committed by GitHub
parent ae746c8b1d
commit 6cfb6437fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 29 additions and 0 deletions

View File

@ -74,6 +74,7 @@ type systemdCollector struct {
socketCurrentConnectionsDesc *prometheus.Desc
socketRefusedConnectionsDesc *prometheus.Desc
systemdVersionDesc *prometheus.Desc
virtualizationDesc *prometheus.Desc
// Use regexps for more flexibility than device_filter.go allows
systemdUnitIncludePattern *regexp.Regexp
systemdUnitExcludePattern *regexp.Regexp
@ -132,6 +133,9 @@ func NewSystemdCollector(logger *slog.Logger) (Collector, error) {
systemdVersionDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "version"),
"Detected systemd version", []string{"version"}, nil)
virtualizationDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "virtualization_info"),
"Detected virtualization technology", []string{"virtualization_type"}, nil)
if *oldSystemdUnitExclude != "" {
if !systemdUnitExcludeSet {
@ -167,6 +171,7 @@ func NewSystemdCollector(logger *slog.Logger) (Collector, error) {
socketCurrentConnectionsDesc: socketCurrentConnectionsDesc,
socketRefusedConnectionsDesc: socketRefusedConnectionsDesc,
systemdVersionDesc: systemdVersionDesc,
virtualizationDesc: virtualizationDesc,
systemdUnitIncludePattern: systemdUnitIncludePattern,
systemdUnitExcludePattern: systemdUnitExcludePattern,
logger: logger,
@ -194,6 +199,14 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
systemdVersionFull,
)
systemdVirtualization := c.getSystemdVirtualization(conn)
ch <- prometheus.MustNewConstMetric(
c.virtualizationDesc,
prometheus.GaugeValue,
1.0,
systemdVirtualization,
)
allUnits, err := c.getAllUnits(conn)
if err != nil {
return fmt.Errorf("couldn't get units: %w", err)
@ -505,3 +518,19 @@ func (c *systemdCollector) getSystemdVersion(conn *dbus.Conn) (float64, string)
}
return v, version
}
func (c *systemdCollector) getSystemdVirtualization(conn *dbus.Conn) string {
virt, err := conn.GetManagerProperty("Virtualization")
if err != nil {
c.logger.Debug("Could not get Virtualization property", "err", err)
return "unknown"
}
virtStr := strings.Trim(virt, `"`)
if virtStr == "" {
// If no virtualization type is returned, assume it's bare metal.
return "none"
}
return virtStr
}