Skip to content

Commit

Permalink
Add system shutdown timestamp
Browse files Browse the repository at this point in the history
Add a metric for the scheduled shutdown time from systemd.

Signed-off-by: Ben Kochie <superq@gmail.com>
  • Loading branch information
SuperQ committed Sep 5, 2024
1 parent b9d0932 commit 7aa1d05
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions collector/systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type systemdCollector struct {
unitTasksCurrentDesc *prometheus.Desc
unitTasksMaxDesc *prometheus.Desc
systemRunningDesc *prometheus.Desc
systemShutdownDesc *prometheus.Desc
summaryDesc *prometheus.Desc
nRestartsDesc *prometheus.Desc
timerLastTriggerDesc *prometheus.Desc
Expand Down Expand Up @@ -112,6 +113,11 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) {
"Whether the system is operational (see 'systemctl is-system-running')",
nil, nil,
)
systemShutdownDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "system_shutdown_timestamp"),
"Time for a scheduled shutdown (see 'systemctl status systemd-shutdownd.service')",
nil, nil,
)
summaryDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "units"),
"Summary of systemd unit states", []string{"state"}, nil)
Expand Down Expand Up @@ -161,6 +167,7 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) {
unitTasksCurrentDesc: unitTasksCurrentDesc,
unitTasksMaxDesc: unitTasksMaxDesc,
systemRunningDesc: systemRunningDesc,
systemShutdownDesc: systemShutdownDesc,
summaryDesc: summaryDesc,
nRestartsDesc: nRestartsDesc,
timerLastTriggerDesc: timerLastTriggerDesc,
Expand Down Expand Up @@ -265,6 +272,10 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
level.Debug(c.logger).Log("msg", "collectSystemState took", "duration_seconds", time.Since(begin).Seconds())
}

begin = time.Now()
err = c.collectScheduledShutdownMetrics(conn, ch)
level.Debug(c.logger).Log("msg", "collectScheduledShutdownMetrics took", "duration_seconds", time.Since(begin).Seconds())

return err
}

Expand Down Expand Up @@ -343,6 +354,23 @@ func (c *systemdCollector) collectSockets(conn *dbus.Conn, ch chan<- prometheus.
}
}

func (c *systemdCollector) collectScheduledShutdownMetrics(conn *dbus.Conn, ch chan<- prometheus.Metric) error {
var shutdownTimeUsec uint64

timestampValue, err := conn.GetServicePropertyContext(context.TODO(), "org.freedesktop.login1", "ScheduledShutdown")
if err != nil {
level.Debug(c.logger).Log("msg", "couldn't get ScheduledShutdown", "err", err)
return errors.New("Couldn't get ScheduledShutdown property")
}
shutdownTimeUsec = timestampValue.Value.Value().(uint64)

ch <- prometheus.MustNewConstMetric(
c.systemShutdownDesc, prometheus.GaugeValue,
float64(shutdownTimeUsec)/1e6,
)
return nil
}

func (c *systemdCollector) collectUnitStartTimeMetrics(conn *dbus.Conn, ch chan<- prometheus.Metric, units []unit) {
var startTimeUsec uint64

Expand Down

0 comments on commit 7aa1d05

Please sign in to comment.