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>pull/1080/merge
parent
4672ea1671
commit
0fdc089187
|
@ -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] Systemd units will not be ignored if you're running older versions of systemd #1039
|
||||||
* [BUGFIX] Handle vanishing PIDs #1043
|
* [BUGFIX] Handle vanishing PIDs #1043
|
||||||
* [BUGFIX] Correctly cast Darwin memory info #1060
|
* [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
|
## 0.16.0 / 2018-05-15
|
||||||
|
|
||||||
|
|
|
@ -228,7 +228,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
// Filter out any units that are not installed and are pulled in only as dependencies.
|
// 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -243,7 +243,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
|
||||||
if strings.HasSuffix(unit.Name, ".timer") {
|
if strings.HasSuffix(unit.Name, ".timer") {
|
||||||
lastTriggerValue, err := conn.GetUnitTypeProperty(unit.Name, "Timer", "LastTriggerUSec")
|
lastTriggerValue, err := conn.GetUnitTypeProperty(unit.Name, "Timer", "LastTriggerUSec")
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,7 +253,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
|
||||||
// NRestarts wasn't added until systemd 235.
|
// NRestarts wasn't added until systemd 235.
|
||||||
restartsCount, err := conn.GetUnitTypeProperty(unit.Name, "Service", "NRestarts")
|
restartsCount, err := conn.GetUnitTypeProperty(unit.Name, "Service", "NRestarts")
|
||||||
if err != nil {
|
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 {
|
} else {
|
||||||
nRestarts := restartsCount.Value.Value().(uint32)
|
nRestarts := restartsCount.Value.Value().(uint32)
|
||||||
unit.nRestarts = &nRestarts
|
unit.nRestarts = &nRestarts
|
||||||
|
@ -263,7 +263,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
|
||||||
if strings.HasSuffix(unit.Name, ".socket") {
|
if strings.HasSuffix(unit.Name, ".socket") {
|
||||||
acceptedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NAccepted")
|
acceptedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NAccepted")
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,7 +271,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
|
||||||
|
|
||||||
currentConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NConnections")
|
currentConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NConnections")
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
unit.currentConnections = currentConnectionCount.Value.Value().(uint32)
|
unit.currentConnections = currentConnectionCount.Value.Value().(uint32)
|
||||||
|
@ -279,7 +279,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
|
||||||
// NRefused wasn't added until systemd 239.
|
// NRefused wasn't added until systemd 239.
|
||||||
refusedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NRefused")
|
refusedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NRefused")
|
||||||
if err != nil {
|
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 {
|
} else {
|
||||||
nRefused := refusedConnectionCount.Value.Value().(uint32)
|
nRefused := refusedConnectionCount.Value.Value().(uint32)
|
||||||
unit.refusedConnections = &nRefused
|
unit.refusedConnections = &nRefused
|
||||||
|
@ -291,7 +291,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) {
|
||||||
} else {
|
} else {
|
||||||
timestampValue, err := conn.GetUnitProperty(unit.Name, "ActiveEnterTimestamp")
|
timestampValue, err := conn.GetUnitProperty(unit.Name, "ActiveEnterTimestamp")
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,7 +321,8 @@ func summarizeUnits(units []unit) map[string]float64 {
|
||||||
func filterUnits(units []unit, whitelistPattern, blacklistPattern *regexp.Regexp) []unit {
|
func filterUnits(units []unit, whitelistPattern, blacklistPattern *regexp.Regexp) []unit {
|
||||||
filtered := make([]unit, 0, len(units))
|
filtered := make([]unit, 0, len(units))
|
||||||
for _, unit := range 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)
|
filtered = append(filtered, unit)
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Ignoring unit: %s", unit.Name)
|
log.Debugf("Ignoring unit: %s", unit.Name)
|
||||||
|
|
|
@ -126,7 +126,8 @@ func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) {
|
||||||
fixtures := getUnitListFixtures()
|
fixtures := getUnitListFixtures()
|
||||||
collector := c.(*systemdCollector)
|
collector := c.(*systemdCollector)
|
||||||
filtered := filterUnits(fixtures[0], collector.unitWhitelistPattern, collector.unitBlacklistPattern)
|
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")
|
t.Error("Default filters removed units")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue