Skip to content

Commit 6cfb643

Browse files
IbraAoadSuperQmikelolasagastialex5517
authored
feat: add SystemdVirtualization type (#3254)
* feat: add SystemdVirtualization type --------- Signed-off-by: IbraAoad <[email protected]> Signed-off-by: Ibrahim Awwad <[email protected]> Signed-off-by: Mikel Olasagasti Uranga <[email protected]> Signed-off-by: Alexander Soelberg Heidarsson <[email protected]> Co-authored-by: Ben Kochie <[email protected]> Co-authored-by: Mikel Olasagasti Uranga <[email protected]> Co-authored-by: Alexander Soelberg Heidarsson <[email protected]>
1 parent ae746c8 commit 6cfb643

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

collector/systemd_linux.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type systemdCollector struct {
7474
socketCurrentConnectionsDesc *prometheus.Desc
7575
socketRefusedConnectionsDesc *prometheus.Desc
7676
systemdVersionDesc *prometheus.Desc
77+
virtualizationDesc *prometheus.Desc
7778
// Use regexps for more flexibility than device_filter.go allows
7879
systemdUnitIncludePattern *regexp.Regexp
7980
systemdUnitExcludePattern *regexp.Regexp
@@ -132,6 +133,9 @@ func NewSystemdCollector(logger *slog.Logger) (Collector, error) {
132133
systemdVersionDesc := prometheus.NewDesc(
133134
prometheus.BuildFQName(namespace, subsystem, "version"),
134135
"Detected systemd version", []string{"version"}, nil)
136+
virtualizationDesc := prometheus.NewDesc(
137+
prometheus.BuildFQName(namespace, subsystem, "virtualization_info"),
138+
"Detected virtualization technology", []string{"virtualization_type"}, nil)
135139

136140
if *oldSystemdUnitExclude != "" {
137141
if !systemdUnitExcludeSet {
@@ -167,6 +171,7 @@ func NewSystemdCollector(logger *slog.Logger) (Collector, error) {
167171
socketCurrentConnectionsDesc: socketCurrentConnectionsDesc,
168172
socketRefusedConnectionsDesc: socketRefusedConnectionsDesc,
169173
systemdVersionDesc: systemdVersionDesc,
174+
virtualizationDesc: virtualizationDesc,
170175
systemdUnitIncludePattern: systemdUnitIncludePattern,
171176
systemdUnitExcludePattern: systemdUnitExcludePattern,
172177
logger: logger,
@@ -194,6 +199,14 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
194199
systemdVersionFull,
195200
)
196201

202+
systemdVirtualization := c.getSystemdVirtualization(conn)
203+
ch <- prometheus.MustNewConstMetric(
204+
c.virtualizationDesc,
205+
prometheus.GaugeValue,
206+
1.0,
207+
systemdVirtualization,
208+
)
209+
197210
allUnits, err := c.getAllUnits(conn)
198211
if err != nil {
199212
return fmt.Errorf("couldn't get units: %w", err)
@@ -505,3 +518,19 @@ func (c *systemdCollector) getSystemdVersion(conn *dbus.Conn) (float64, string)
505518
}
506519
return v, version
507520
}
521+
522+
func (c *systemdCollector) getSystemdVirtualization(conn *dbus.Conn) string {
523+
virt, err := conn.GetManagerProperty("Virtualization")
524+
if err != nil {
525+
c.logger.Debug("Could not get Virtualization property", "err", err)
526+
return "unknown"
527+
}
528+
529+
virtStr := strings.Trim(virt, `"`)
530+
if virtStr == "" {
531+
// If no virtualization type is returned, assume it's bare metal.
532+
return "none"
533+
}
534+
535+
return virtStr
536+
}

0 commit comments

Comments
 (0)