mirror of
				https://github.com/prometheus/node_exporter.git
				synced 2025-08-20 18:33:52 -07:00 
			
		
		
		
	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:
		
							parent
							
								
									edc40d1e0b
								
							
						
					
					
						commit
						4062f16665
					
				| 
						 | 
					@ -67,6 +67,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
 | 
				
			||||||
| 
						 | 
					@ -111,6 +112,11 @@ func NewSystemdCollector(logger *slog.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_seconds"),
 | 
				
			||||||
 | 
							"The scheduled shutdown timestamp')",
 | 
				
			||||||
 | 
							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)
 | 
				
			||||||
| 
						 | 
					@ -160,6 +166,7 @@ func NewSystemdCollector(logger *slog.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,
 | 
				
			||||||
| 
						 | 
					@ -262,8 +269,15 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
 | 
				
			||||||
		begin = time.Now()
 | 
							begin = time.Now()
 | 
				
			||||||
		err = c.collectSystemState(conn, ch)
 | 
							err = c.collectSystemState(conn, ch)
 | 
				
			||||||
		c.logger.Debug("collectSystemState took", "duration_seconds", time.Since(begin).Seconds())
 | 
							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
 | 
						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) {
 | 
					func (c *systemdCollector) collectUnitStartTimeMetrics(conn *dbus.Conn, ch chan<- prometheus.Metric, units []unit) {
 | 
				
			||||||
	var startTimeUsec uint64
 | 
						var startTimeUsec uint64
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue