Skip to content

Commit

Permalink
Add systemd uptime metric collection (prometheus#952)
Browse files Browse the repository at this point in the history
* Add systemd uptime metric collection

Signed-off-by: Sven Lange <tdl@hadiko.de>
  • Loading branch information
langesven authored and oblitorum committed Apr 9, 2024
1 parent ddecb0b commit 19951cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [FEATURE] Collect NRefused property for systemd socket units (available as of systemd v239)
* [FEATURE] Collect NRestarts property for systemd service units
* [FEATURE] Add socket unit stats to systemd collector #968
* [FEATURE] Collect start time for systemd units
* [ENHANCEMENT]
* [BUGFIX]

Expand Down
27 changes: 27 additions & 0 deletions collector/systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (

type systemdCollector struct {
unitDesc *prometheus.Desc
unitStartTimeDesc *prometheus.Desc
systemRunningDesc *prometheus.Desc
summaryDesc *prometheus.Desc
nRestartsDesc *prometheus.Desc
Expand All @@ -59,6 +60,10 @@ func NewSystemdCollector() (Collector, error) {
prometheus.BuildFQName(namespace, subsystem, "unit_state"),
"Systemd unit", []string{"name", "state"}, nil,
)
unitStartTimeDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "unit_start_time_seconds"),
"Start time of the unit since unix epoch in seconds.", []string{"name"}, nil,
)
systemRunningDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "system_running"),
"Whether the system is operational (see 'systemctl is-system-running')",
Expand Down Expand Up @@ -87,6 +92,7 @@ func NewSystemdCollector() (Collector, error) {

return &systemdCollector{
unitDesc: unitDesc,
unitStartTimeDesc: unitStartTimeDesc,
systemRunningDesc: systemRunningDesc,
summaryDesc: summaryDesc,
nRestartsDesc: nRestartsDesc,
Expand All @@ -110,6 +116,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {

units := filterUnits(allUnits, c.unitWhitelistPattern, c.unitBlacklistPattern)
c.collectUnitStatusMetrics(ch, units)
c.collectUnitStartTimeMetrics(ch, units)
c.collectTimers(ch, units)
c.collectSockets(ch, units)

Expand Down Expand Up @@ -160,6 +167,14 @@ func (c *systemdCollector) collectSockets(ch chan<- prometheus.Metric, units []u
return nil
}

func (c *systemdCollector) collectUnitStartTimeMetrics(ch chan<- prometheus.Metric, units []unit) {
for _, unit := range units {
ch <- prometheus.MustNewConstMetric(
c.unitStartTimeDesc, prometheus.GaugeValue,
float64(unit.startTimeUsec)/1e6, unit.Name)
}
}

func (c *systemdCollector) collectTimers(ch chan<- prometheus.Metric, units []unit) error {
for _, unit := range units {
if !strings.HasSuffix(unit.Name, ".timer") {
Expand Down Expand Up @@ -198,6 +213,7 @@ func (c *systemdCollector) newDbus() (*dbus.Conn, error) {
type unit struct {
dbus.UnitStatus
lastTriggerUsec uint64
startTimeUsec uint64
nRestarts uint32
acceptedConnections uint32
currentConnections uint32
Expand Down Expand Up @@ -261,6 +277,17 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
unit.refusedConnections = refusedConnectionCount.Value.Value().(uint32)
}

if unit.ActiveState != "active" {
unit.startTimeUsec = 0
} else {
timestampValue, err := conn.GetUnitProperty(unit.Name, "ActiveEnterTimestamp")
if err != nil {
return nil, fmt.Errorf("couldn't get unit '%s' StartTimeUsec: %s", unit.Name, err)
}

unit.startTimeUsec = timestampValue.Value.Value().(uint64)
}

result = append(result, unit)
}

Expand Down

0 comments on commit 19951cc

Please sign in to comment.