From 0fdc089187c3d5d9fe62e528653986ee6e71325b Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Mon, 24 Sep 2018 15:04:55 +0200 Subject: [PATCH] Change systemd unit filtering (#1083) * Change systemd unit filtering Get all units from systemd and filter in Go. * Improves compatibility with older versions of systemd. * Improve debugging by printing when units pass the filter. * Remove extraneous newlines from log messages. Signed-off-by: Ben Kochie --- CHANGELOG.md | 1 + collector/systemd_linux.go | 17 +++++++++-------- collector/systemd_linux_test.go | 3 ++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1191778..e02195fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Darwin meminfo metrics have been renamed to match Prometheus conventions. #1060 * [BUGFIX] Systemd units will not be ignored if you're running older versions of systemd #1039 * [BUGFIX] Handle vanishing PIDs #1043 * [BUGFIX] Correctly cast Darwin memory info #1060 +* [BUGFIX] Filter systemd units in Go for compatibility with older versions #1083 ## 0.16.0 / 2018-05-15 diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go index 7140b7b4..dd8a3f0e 100644 --- a/collector/systemd_linux.go +++ b/collector/systemd_linux.go @@ -228,7 +228,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { defer conn.Close() // Filter out any units that are not installed and are pulled in only as dependencies. - allUnits, err := conn.ListUnitsFiltered([]string{"loaded"}) + allUnits, err := conn.ListUnits() if err != nil { return nil, err @@ -243,7 +243,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { if strings.HasSuffix(unit.Name, ".timer") { lastTriggerValue, err := conn.GetUnitTypeProperty(unit.Name, "Timer", "LastTriggerUSec") if err != nil { - log.Debugf("couldn't get unit '%s' LastTriggerUSec: %s\n", unit.Name, err) + log.Debugf("couldn't get unit '%s' LastTriggerUSec: %s", unit.Name, err) continue } @@ -253,7 +253,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { // NRestarts wasn't added until systemd 235. restartsCount, err := conn.GetUnitTypeProperty(unit.Name, "Service", "NRestarts") if err != nil { - log.Debugf("couldn't get unit '%s' NRestarts: %s\n", unit.Name, err) + log.Debugf("couldn't get unit '%s' NRestarts: %s", unit.Name, err) } else { nRestarts := restartsCount.Value.Value().(uint32) unit.nRestarts = &nRestarts @@ -263,7 +263,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { if strings.HasSuffix(unit.Name, ".socket") { acceptedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NAccepted") if err != nil { - log.Debugf("couldn't get unit '%s' NAccepted: %s\n", unit.Name, err) + log.Debugf("couldn't get unit '%s' NAccepted: %s", unit.Name, err) continue } @@ -271,7 +271,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { currentConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NConnections") if err != nil { - log.Debugf("couldn't get unit '%s' NConnections: %s\n", unit.Name, err) + log.Debugf("couldn't get unit '%s' NConnections: %s", unit.Name, err) continue } unit.currentConnections = currentConnectionCount.Value.Value().(uint32) @@ -279,7 +279,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { // NRefused wasn't added until systemd 239. refusedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NRefused") if err != nil { - log.Debugf("couldn't get unit '%s' NRefused: %s\n", unit.Name, err) + log.Debugf("couldn't get unit '%s' NRefused: %s", unit.Name, err) } else { nRefused := refusedConnectionCount.Value.Value().(uint32) unit.refusedConnections = &nRefused @@ -291,7 +291,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { } else { timestampValue, err := conn.GetUnitProperty(unit.Name, "ActiveEnterTimestamp") if err != nil { - log.Debugf("couldn't get unit '%s' StartTimeUsec: %s\n", unit.Name, err) + log.Debugf("couldn't get unit '%s' StartTimeUsec: %s", unit.Name, err) continue } @@ -321,7 +321,8 @@ func summarizeUnits(units []unit) map[string]float64 { func filterUnits(units []unit, whitelistPattern, blacklistPattern *regexp.Regexp) []unit { filtered := make([]unit, 0, len(units)) for _, unit := range units { - if whitelistPattern.MatchString(unit.Name) && !blacklistPattern.MatchString(unit.Name) { + if whitelistPattern.MatchString(unit.Name) && !blacklistPattern.MatchString(unit.Name) && unit.LoadState == "loaded" { + log.Debugf("Adding unit: %s", unit.Name) filtered = append(filtered, unit) } else { log.Debugf("Ignoring unit: %s", unit.Name) diff --git a/collector/systemd_linux_test.go b/collector/systemd_linux_test.go index 3d7d7f6c..26257aa5 100644 --- a/collector/systemd_linux_test.go +++ b/collector/systemd_linux_test.go @@ -126,7 +126,8 @@ func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) { fixtures := getUnitListFixtures() collector := c.(*systemdCollector) filtered := filterUnits(fixtures[0], collector.unitWhitelistPattern, collector.unitBlacklistPattern) - if len(filtered) != len(fixtures[0]) { + // Adjust fixtures by 3 "not-found" units. + if len(filtered) != len(fixtures[0])-3 { t.Error("Default filters removed units") } }