aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven Lange <langesven@users.noreply.github.com>2018-07-18 16:02:05 +0200
committerBen Kochie <superq@gmail.com>2018-07-18 16:02:05 +0200
commit2ae8c1c7a7c6a82b2c779aaf84a02899b0ada21b (patch)
treec1e6d8ee90e189af60ca77eed0c93f51ddbab4d6
parent354115511c61c71a46eacf541908f6fb75a63c9a (diff)
downloadprometheus_node_collector-2ae8c1c7a7c6a82b2c779aaf84a02899b0ada21b.tar.bz2
prometheus_node_collector-2ae8c1c7a7c6a82b2c779aaf84a02899b0ada21b.tar.xz
prometheus_node_collector-2ae8c1c7a7c6a82b2c779aaf84a02899b0ada21b.zip
Add systemd uptime metric collection (#952)
* Add systemd uptime metric collection Signed-off-by: Sven Lange <tdl@hadiko.de>
-rw-r--r--CHANGELOG.md1
-rw-r--r--collector/systemd_linux.go27
2 files changed, 28 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 85a0485..2cb808d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
6* [FEATURE] Collect NRefused property for systemd socket units (available as of systemd v239) 6* [FEATURE] Collect NRefused property for systemd socket units (available as of systemd v239)
7* [FEATURE] Collect NRestarts property for systemd service units 7* [FEATURE] Collect NRestarts property for systemd service units
8* [FEATURE] Add socket unit stats to systemd collector #968 8* [FEATURE] Add socket unit stats to systemd collector #968
9* [FEATURE] Collect start time for systemd units
9* [ENHANCEMENT] 10* [ENHANCEMENT]
10* [BUGFIX] 11* [BUGFIX]
11 12
diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go
index 3e16c45..99a10de 100644
--- a/collector/systemd_linux.go
+++ b/collector/systemd_linux.go
@@ -34,6 +34,7 @@ var (
34 34
35type systemdCollector struct { 35type systemdCollector struct {
36 unitDesc *prometheus.Desc 36 unitDesc *prometheus.Desc
37 unitStartTimeDesc *prometheus.Desc
37 systemRunningDesc *prometheus.Desc 38 systemRunningDesc *prometheus.Desc
38 summaryDesc *prometheus.Desc 39 summaryDesc *prometheus.Desc
39 nRestartsDesc *prometheus.Desc 40 nRestartsDesc *prometheus.Desc
@@ -59,6 +60,10 @@ func NewSystemdCollector() (Collector, error) {
59 prometheus.BuildFQName(namespace, subsystem, "unit_state"), 60 prometheus.BuildFQName(namespace, subsystem, "unit_state"),
60 "Systemd unit", []string{"name", "state"}, nil, 61 "Systemd unit", []string{"name", "state"}, nil,
61 ) 62 )
63 unitStartTimeDesc := prometheus.NewDesc(
64 prometheus.BuildFQName(namespace, subsystem, "unit_start_time_seconds"),
65 "Start time of the unit since unix epoch in seconds.", []string{"name"}, nil,
66 )
62 systemRunningDesc := prometheus.NewDesc( 67 systemRunningDesc := prometheus.NewDesc(
63 prometheus.BuildFQName(namespace, subsystem, "system_running"), 68 prometheus.BuildFQName(namespace, subsystem, "system_running"),
64 "Whether the system is operational (see 'systemctl is-system-running')", 69 "Whether the system is operational (see 'systemctl is-system-running')",
@@ -87,6 +92,7 @@ func NewSystemdCollector() (Collector, error) {
87 92
88 return &systemdCollector{ 93 return &systemdCollector{
89 unitDesc: unitDesc, 94 unitDesc: unitDesc,
95 unitStartTimeDesc: unitStartTimeDesc,
90 systemRunningDesc: systemRunningDesc, 96 systemRunningDesc: systemRunningDesc,
91 summaryDesc: summaryDesc, 97 summaryDesc: summaryDesc,
92 nRestartsDesc: nRestartsDesc, 98 nRestartsDesc: nRestartsDesc,
@@ -110,6 +116,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
110 116
111 units := filterUnits(allUnits, c.unitWhitelistPattern, c.unitBlacklistPattern) 117 units := filterUnits(allUnits, c.unitWhitelistPattern, c.unitBlacklistPattern)
112 c.collectUnitStatusMetrics(ch, units) 118 c.collectUnitStatusMetrics(ch, units)
119 c.collectUnitStartTimeMetrics(ch, units)
113 c.collectTimers(ch, units) 120 c.collectTimers(ch, units)
114 c.collectSockets(ch, units) 121 c.collectSockets(ch, units)
115 122
@@ -160,6 +167,14 @@ func (c *systemdCollector) collectSockets(ch chan<- prometheus.Metric, units []u
160 return nil 167 return nil
161} 168}
162 169
170func (c *systemdCollector) collectUnitStartTimeMetrics(ch chan<- prometheus.Metric, units []unit) {
171 for _, unit := range units {
172 ch <- prometheus.MustNewConstMetric(
173 c.unitStartTimeDesc, prometheus.GaugeValue,
174 float64(unit.startTimeUsec)/1e6, unit.Name)
175 }
176}
177
163func (c *systemdCollector) collectTimers(ch chan<- prometheus.Metric, units []unit) error { 178func (c *systemdCollector) collectTimers(ch chan<- prometheus.Metric, units []unit) error {
164 for _, unit := range units { 179 for _, unit := range units {
165 if !strings.HasSuffix(unit.Name, ".timer") { 180 if !strings.HasSuffix(unit.Name, ".timer") {
@@ -198,6 +213,7 @@ func (c *systemdCollector) newDbus() (*dbus.Conn, error) {
198type unit struct { 213type unit struct {
199 dbus.UnitStatus 214 dbus.UnitStatus
200 lastTriggerUsec uint64 215 lastTriggerUsec uint64
216 startTimeUsec uint64
201 nRestarts uint32 217 nRestarts uint32
202 acceptedConnections uint32 218 acceptedConnections uint32
203 currentConnections uint32 219 currentConnections uint32
@@ -261,6 +277,17 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
261 unit.refusedConnections = refusedConnectionCount.Value.Value().(uint32) 277 unit.refusedConnections = refusedConnectionCount.Value.Value().(uint32)
262 } 278 }
263 279
280 if unit.ActiveState != "active" {
281 unit.startTimeUsec = 0
282 } else {
283 timestampValue, err := conn.GetUnitProperty(unit.Name, "ActiveEnterTimestamp")
284 if err != nil {
285 return nil, fmt.Errorf("couldn't get unit '%s' StartTimeUsec: %s", unit.Name, err)
286 }
287
288 unit.startTimeUsec = timestampValue.Value.Value().(uint64)
289 }
290
264 result = append(result, unit) 291 result = append(result, unit)
265 } 292 }
266 293