Skip to content

Commit

Permalink
Add support for NRestarts counter introduced in systemd 235 (#992)
Browse files Browse the repository at this point in the history
* Add support for NRestarts counter introduced in systemd 235

`.service` units increment this counter any time the Restart= condition is
triggered.

Signed-off-by: Matthew McGinn <mamcgi@gmail.com>
  • Loading branch information
xginn8 authored and SuperQ committed Jul 5, 2018
1 parent ee1e199 commit 8af84a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
**Breaking changes**

* [CHANGE]
* [FEATURE]
* [FEATURE] Collect NRestarts property for systemd service units
* [ENHANCEMENT]
* [BUGFIX]

Expand Down
19 changes: 19 additions & 0 deletions collector/systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type systemdCollector struct {
unitDesc *prometheus.Desc
systemRunningDesc *prometheus.Desc
summaryDesc *prometheus.Desc
nRestartsDesc *prometheus.Desc
timerLastTriggerDesc *prometheus.Desc
unitWhitelistPattern *regexp.Regexp
unitBlacklistPattern *regexp.Regexp
Expand Down Expand Up @@ -63,6 +64,9 @@ func NewSystemdCollector() (Collector, error) {
summaryDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "units"),
"Summary of systemd unit states", []string{"state"}, nil)
nRestartsDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "service_restart_total"),
"Service unit count of Restart triggers", []string{"state"}, nil)
timerLastTriggerDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "timer_last_trigger_seconds"),
"Seconds since epoch of last trigger.", []string{"name"}, nil)
Expand All @@ -73,6 +77,7 @@ func NewSystemdCollector() (Collector, error) {
unitDesc: unitDesc,
systemRunningDesc: systemRunningDesc,
summaryDesc: summaryDesc,
nRestartsDesc: nRestartsDesc,
timerLastTriggerDesc: timerLastTriggerDesc,
unitWhitelistPattern: unitWhitelistPattern,
unitBlacklistPattern: unitBlacklistPattern,
Expand Down Expand Up @@ -112,6 +117,11 @@ func (c *systemdCollector) collectUnitStatusMetrics(ch chan<- prometheus.Metric,
c.unitDesc, prometheus.GaugeValue, isActive,
unit.Name, stateName)
}
if strings.HasSuffix(unit.Name, ".service") {
ch <- prometheus.MustNewConstMetric(
c.nRestartsDesc, prometheus.CounterValue,
float64(unit.nRestarts), unit.Name)
}
}
}

Expand Down Expand Up @@ -153,6 +163,7 @@ func (c *systemdCollector) newDbus() (*dbus.Conn, error) {
type unit struct {
dbus.UnitStatus
lastTriggerUsec uint64
nRestarts uint32
}

func (c *systemdCollector) getAllUnits() ([]unit, error) {
Expand Down Expand Up @@ -181,6 +192,14 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {

unit.lastTriggerUsec = lastTriggerValue.Value.Value().(uint64)
}
if strings.HasSuffix(unit.Name, ".service") {
nRestarts, err := conn.GetUnitTypeProperty(unit.Name, "Service", "NRestarts")
if err != nil {
log.Debugf("couldn't get unit '%s' NRestarts: %s\n", unit.Name, err)
continue
}
unit.nRestarts = nRestarts.Value.Value().(uint32)
}

result = append(result, unit)
}
Expand Down

0 comments on commit 8af84a2

Please sign in to comment.