Add system shutdown timestamp

Add a metric for the scheduled shutdown time from systemd.

Signed-off-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
Ben Kochie 2024-09-05 12:00:52 +02:00
parent edc40d1e0b
commit 4062f16665
No known key found for this signature in database
GPG Key ID: C646B23C9E3245F1

View File

@ -67,6 +67,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
@ -111,6 +112,11 @@ func NewSystemdCollector(logger *slog.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_seconds"),
"The scheduled shutdown timestamp')",
nil, nil,
)
summaryDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "units"),
"Summary of systemd unit states", []string{"state"}, nil)
@ -160,6 +166,7 @@ func NewSystemdCollector(logger *slog.Logger) (Collector, error) {
unitTasksCurrentDesc: unitTasksCurrentDesc,
unitTasksMaxDesc: unitTasksMaxDesc,
systemRunningDesc: systemRunningDesc,
systemShutdownDesc: systemShutdownDesc,
summaryDesc: summaryDesc,
nRestartsDesc: nRestartsDesc,
timerLastTriggerDesc: timerLastTriggerDesc,
@ -262,8 +269,15 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
begin = time.Now()
err = c.collectSystemState(conn, ch)
c.logger.Debug("collectSystemState took", "duration_seconds", time.Since(begin).Seconds())
if err != nil {
return err
}
}
begin = time.Now()
err = c.collectScheduledShutdownMetrics(conn, ch)
c.logger.Debug("collectScheduledShutdownMetrics took", "duration_seconds", time.Since(begin).Seconds())
return err
}
@ -342,6 +356,26 @@ 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.Manager", "ScheduledShutdown")
shutdownValue, err := conn.GetManagerProperty("ScheduledShutdown")
c.logger.Info("got ScheduledShutdown", "value", shutdownValue)
return nil
if err != nil {
c.logger.Debug("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