Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Collector struct {
logger *slog.Logger
systemdBootMonotonic *prometheus.Desc
systemdBootTime *prometheus.Desc
systemdVersion *prometheus.Desc
unitCPUTotal *prometheus.Desc
unitState *prometheus.Desc
unitInfo *prometheus.Desc
Expand Down Expand Up @@ -98,6 +99,11 @@ func NewCollector(logger *slog.Logger) (*Collector, error) {
prometheus.BuildFQName(namespace, "", "boot_time_seconds"),
"Systemd boot stage timestamps", []string{"stage"}, nil,
)
systemdVersion := prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "version"),
"systemd version",
[]string{"version"}, nil,
)
// Type is labeled twice e.g. name="foo.service" and type="service" to maintain compatibility
// with users before we started exporting type label
unitState := prometheus.NewDesc(
Expand Down Expand Up @@ -230,6 +236,7 @@ func NewCollector(logger *slog.Logger) (*Collector, error) {
logger: logger,
systemdBootMonotonic: systemdBootMonotonic,
systemdBootTime: systemdBootTime,
systemdVersion: systemdVersion,
unitCPUTotal: unitCPUTotal,
unitState: unitState,
unitInfo: unitInfo,
Expand Down Expand Up @@ -270,6 +277,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
func (c *Collector) Describe(desc chan<- *prometheus.Desc) {
desc <- c.systemdBootMonotonic
desc <- c.systemdBootTime
desc <- c.systemdVersion
desc <- c.unitCPUTotal
desc <- c.unitState
desc <- c.unitInfo
Expand Down Expand Up @@ -304,6 +312,15 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error {
}
defer conn.Close()

systemdVersion := c.getSystemdVersion(conn)

ch <- prometheus.MustNewConstMetric(
c.systemdVersion,
prometheus.GaugeValue,
1,
systemdVersion,
)

err = c.collectBootStageTimestamps(conn, ch)
if err != nil {
c.logger.Debug("Failed to collect boot stage timestamps", "err", err.Error())
Expand Down Expand Up @@ -785,3 +802,16 @@ func (c *Collector) collectWatchdogMetrics(conn *dbus.Conn, ch chan<- prometheus

return nil
}

func (c *Collector) getSystemdVersion(conn *dbus.Conn) string {
version, err := conn.GetManagerProperty("Version")
if err != nil {
c.logger.Error("Unable to get systemd version property, defaulting to 0", "err", err.Error())
return "0"
}

version = strings.TrimPrefix(strings.TrimSuffix(version, `"`), `"`)
c.logger.Debug("Got systemd version", "version", version)

return version
}