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 b9d0932179
commit 7aa1d057e2
No known key found for this signature in database
GPG key ID: C646B23C9E3245F1

View file

@ -68,6 +68,7 @@ type systemdCollector struct {
unitTasksCurrentDesc *prometheus.Desc unitTasksCurrentDesc *prometheus.Desc
unitTasksMaxDesc *prometheus.Desc unitTasksMaxDesc *prometheus.Desc
systemRunningDesc *prometheus.Desc systemRunningDesc *prometheus.Desc
systemShutdownDesc *prometheus.Desc
summaryDesc *prometheus.Desc summaryDesc *prometheus.Desc
nRestartsDesc *prometheus.Desc nRestartsDesc *prometheus.Desc
timerLastTriggerDesc *prometheus.Desc timerLastTriggerDesc *prometheus.Desc
@ -112,6 +113,11 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) {
"Whether the system is operational (see 'systemctl is-system-running')", "Whether the system is operational (see 'systemctl is-system-running')",
nil, nil, 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( summaryDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "units"), prometheus.BuildFQName(namespace, subsystem, "units"),
"Summary of systemd unit states", []string{"state"}, nil) "Summary of systemd unit states", []string{"state"}, nil)
@ -161,6 +167,7 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) {
unitTasksCurrentDesc: unitTasksCurrentDesc, unitTasksCurrentDesc: unitTasksCurrentDesc,
unitTasksMaxDesc: unitTasksMaxDesc, unitTasksMaxDesc: unitTasksMaxDesc,
systemRunningDesc: systemRunningDesc, systemRunningDesc: systemRunningDesc,
systemShutdownDesc: systemShutdownDesc,
summaryDesc: summaryDesc, summaryDesc: summaryDesc,
nRestartsDesc: nRestartsDesc, nRestartsDesc: nRestartsDesc,
timerLastTriggerDesc: timerLastTriggerDesc, timerLastTriggerDesc: timerLastTriggerDesc,
@ -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()) 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 return err
} }
@ -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) { func (c *systemdCollector) collectUnitStartTimeMetrics(conn *dbus.Conn, ch chan<- prometheus.Metric, units []unit) {
var startTimeUsec uint64 var startTimeUsec uint64