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 <superq@gmail.com>
This commit is contained in:
Ben Kochie 2018-09-24 15:04:55 +02:00 committed by GitHub
parent 4672ea1671
commit 0fdc089187
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 9 deletions

View File

@ -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

View File

@ -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)

View File

@ -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")
}
}