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
parent
ae746c8b1d
commit
6cfb6437fc
|
@ -74,6 +74,7 @@ type systemdCollector struct {
|
||||||
socketCurrentConnectionsDesc *prometheus.Desc
|
socketCurrentConnectionsDesc *prometheus.Desc
|
||||||
socketRefusedConnectionsDesc *prometheus.Desc
|
socketRefusedConnectionsDesc *prometheus.Desc
|
||||||
systemdVersionDesc *prometheus.Desc
|
systemdVersionDesc *prometheus.Desc
|
||||||
|
virtualizationDesc *prometheus.Desc
|
||||||
// Use regexps for more flexibility than device_filter.go allows
|
// Use regexps for more flexibility than device_filter.go allows
|
||||||
systemdUnitIncludePattern *regexp.Regexp
|
systemdUnitIncludePattern *regexp.Regexp
|
||||||
systemdUnitExcludePattern *regexp.Regexp
|
systemdUnitExcludePattern *regexp.Regexp
|
||||||
|
@ -132,6 +133,9 @@ func NewSystemdCollector(logger *slog.Logger) (Collector, error) {
|
||||||
systemdVersionDesc := prometheus.NewDesc(
|
systemdVersionDesc := prometheus.NewDesc(
|
||||||
prometheus.BuildFQName(namespace, subsystem, "version"),
|
prometheus.BuildFQName(namespace, subsystem, "version"),
|
||||||
"Detected systemd version", []string{"version"}, nil)
|
"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 *oldSystemdUnitExclude != "" {
|
||||||
if !systemdUnitExcludeSet {
|
if !systemdUnitExcludeSet {
|
||||||
|
@ -167,6 +171,7 @@ func NewSystemdCollector(logger *slog.Logger) (Collector, error) {
|
||||||
socketCurrentConnectionsDesc: socketCurrentConnectionsDesc,
|
socketCurrentConnectionsDesc: socketCurrentConnectionsDesc,
|
||||||
socketRefusedConnectionsDesc: socketRefusedConnectionsDesc,
|
socketRefusedConnectionsDesc: socketRefusedConnectionsDesc,
|
||||||
systemdVersionDesc: systemdVersionDesc,
|
systemdVersionDesc: systemdVersionDesc,
|
||||||
|
virtualizationDesc: virtualizationDesc,
|
||||||
systemdUnitIncludePattern: systemdUnitIncludePattern,
|
systemdUnitIncludePattern: systemdUnitIncludePattern,
|
||||||
systemdUnitExcludePattern: systemdUnitExcludePattern,
|
systemdUnitExcludePattern: systemdUnitExcludePattern,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
@ -194,6 +199,14 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
|
||||||
systemdVersionFull,
|
systemdVersionFull,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
systemdVirtualization := c.getSystemdVirtualization(conn)
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
c.virtualizationDesc,
|
||||||
|
prometheus.GaugeValue,
|
||||||
|
1.0,
|
||||||
|
systemdVirtualization,
|
||||||
|
)
|
||||||
|
|
||||||
allUnits, err := c.getAllUnits(conn)
|
allUnits, err := c.getAllUnits(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("couldn't get units: %w", err)
|
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
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue