diff options
author | Ben Kochie <superq@gmail.com> | 2018-09-24 15:04:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-24 15:04:55 +0200 |
commit | 0fdc089187c3d5d9fe62e528653986ee6e71325b (patch) | |
tree | 2a1ba7951e90cf971cabbb0743a6326285f6f6b4 | |
parent | 4672ea1671b7adec0bfdd99339484bdeaa51f38b (diff) | |
download | prometheus_node_collector-0fdc089187c3d5d9fe62e528653986ee6e71325b.tar.bz2 prometheus_node_collector-0fdc089187c3d5d9fe62e528653986ee6e71325b.tar.xz prometheus_node_collector-0fdc089187c3d5d9fe62e528653986ee6e71325b.zip |
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>
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | collector/systemd_linux.go | 17 | ||||
-rw-r--r-- | collector/systemd_linux_test.go | 3 |
3 files changed, 12 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a119177..e02195f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md | |||
@@ -23,6 +23,7 @@ Darwin meminfo metrics have been renamed to match Prometheus conventions. #1060 | |||
23 | * [BUGFIX] Systemd units will not be ignored if you're running older versions of systemd #1039 | 23 | * [BUGFIX] Systemd units will not be ignored if you're running older versions of systemd #1039 |
24 | * [BUGFIX] Handle vanishing PIDs #1043 | 24 | * [BUGFIX] Handle vanishing PIDs #1043 |
25 | * [BUGFIX] Correctly cast Darwin memory info #1060 | 25 | * [BUGFIX] Correctly cast Darwin memory info #1060 |
26 | * [BUGFIX] Filter systemd units in Go for compatibility with older versions #1083 | ||
26 | 27 | ||
27 | ## 0.16.0 / 2018-05-15 | 28 | ## 0.16.0 / 2018-05-15 |
28 | 29 | ||
diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go index 7140b7b..dd8a3f0 100644 --- a/collector/systemd_linux.go +++ b/collector/systemd_linux.go | |||
@@ -228,7 +228,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { | |||
228 | defer conn.Close() | 228 | defer conn.Close() |
229 | 229 | ||
230 | // Filter out any units that are not installed and are pulled in only as dependencies. | 230 | // Filter out any units that are not installed and are pulled in only as dependencies. |
231 | allUnits, err := conn.ListUnitsFiltered([]string{"loaded"}) | 231 | allUnits, err := conn.ListUnits() |
232 | 232 | ||
233 | if err != nil { | 233 | if err != nil { |
234 | return nil, err | 234 | return nil, err |
@@ -243,7 +243,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { | |||
243 | if strings.HasSuffix(unit.Name, ".timer") { | 243 | if strings.HasSuffix(unit.Name, ".timer") { |
244 | lastTriggerValue, err := conn.GetUnitTypeProperty(unit.Name, "Timer", "LastTriggerUSec") | 244 | lastTriggerValue, err := conn.GetUnitTypeProperty(unit.Name, "Timer", "LastTriggerUSec") |
245 | if err != nil { | 245 | if err != nil { |
246 | log.Debugf("couldn't get unit '%s' LastTriggerUSec: %s\n", unit.Name, err) | 246 | log.Debugf("couldn't get unit '%s' LastTriggerUSec: %s", unit.Name, err) |
247 | continue | 247 | continue |
248 | } | 248 | } |
249 | 249 | ||
@@ -253,7 +253,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { | |||
253 | // NRestarts wasn't added until systemd 235. | 253 | // NRestarts wasn't added until systemd 235. |
254 | restartsCount, err := conn.GetUnitTypeProperty(unit.Name, "Service", "NRestarts") | 254 | restartsCount, err := conn.GetUnitTypeProperty(unit.Name, "Service", "NRestarts") |
255 | if err != nil { | 255 | if err != nil { |
256 | log.Debugf("couldn't get unit '%s' NRestarts: %s\n", unit.Name, err) | 256 | log.Debugf("couldn't get unit '%s' NRestarts: %s", unit.Name, err) |
257 | } else { | 257 | } else { |
258 | nRestarts := restartsCount.Value.Value().(uint32) | 258 | nRestarts := restartsCount.Value.Value().(uint32) |
259 | unit.nRestarts = &nRestarts | 259 | unit.nRestarts = &nRestarts |
@@ -263,7 +263,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { | |||
263 | if strings.HasSuffix(unit.Name, ".socket") { | 263 | if strings.HasSuffix(unit.Name, ".socket") { |
264 | acceptedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NAccepted") | 264 | acceptedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NAccepted") |
265 | if err != nil { | 265 | if err != nil { |
266 | log.Debugf("couldn't get unit '%s' NAccepted: %s\n", unit.Name, err) | 266 | log.Debugf("couldn't get unit '%s' NAccepted: %s", unit.Name, err) |
267 | continue | 267 | continue |
268 | } | 268 | } |
269 | 269 | ||
@@ -271,7 +271,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { | |||
271 | 271 | ||
272 | currentConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NConnections") | 272 | currentConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NConnections") |
273 | if err != nil { | 273 | if err != nil { |
274 | log.Debugf("couldn't get unit '%s' NConnections: %s\n", unit.Name, err) | 274 | log.Debugf("couldn't get unit '%s' NConnections: %s", unit.Name, err) |
275 | continue | 275 | continue |
276 | } | 276 | } |
277 | unit.currentConnections = currentConnectionCount.Value.Value().(uint32) | 277 | unit.currentConnections = currentConnectionCount.Value.Value().(uint32) |
@@ -279,7 +279,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { | |||
279 | // NRefused wasn't added until systemd 239. | 279 | // NRefused wasn't added until systemd 239. |
280 | refusedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NRefused") | 280 | refusedConnectionCount, err := conn.GetUnitTypeProperty(unit.Name, "Socket", "NRefused") |
281 | if err != nil { | 281 | if err != nil { |
282 | log.Debugf("couldn't get unit '%s' NRefused: %s\n", unit.Name, err) | 282 | log.Debugf("couldn't get unit '%s' NRefused: %s", unit.Name, err) |
283 | } else { | 283 | } else { |
284 | nRefused := refusedConnectionCount.Value.Value().(uint32) | 284 | nRefused := refusedConnectionCount.Value.Value().(uint32) |
285 | unit.refusedConnections = &nRefused | 285 | unit.refusedConnections = &nRefused |
@@ -291,7 +291,7 @@ func (c *systemdCollector) getAllUnits() ([]unit, error) { | |||
291 | } else { | 291 | } else { |
292 | timestampValue, err := conn.GetUnitProperty(unit.Name, "ActiveEnterTimestamp") | 292 | timestampValue, err := conn.GetUnitProperty(unit.Name, "ActiveEnterTimestamp") |
293 | if err != nil { | 293 | if err != nil { |
294 | log.Debugf("couldn't get unit '%s' StartTimeUsec: %s\n", unit.Name, err) | 294 | log.Debugf("couldn't get unit '%s' StartTimeUsec: %s", unit.Name, err) |
295 | continue | 295 | continue |
296 | } | 296 | } |
297 | 297 | ||
@@ -321,7 +321,8 @@ func summarizeUnits(units []unit) map[string]float64 { | |||
321 | func filterUnits(units []unit, whitelistPattern, blacklistPattern *regexp.Regexp) []unit { | 321 | func filterUnits(units []unit, whitelistPattern, blacklistPattern *regexp.Regexp) []unit { |
322 | filtered := make([]unit, 0, len(units)) | 322 | filtered := make([]unit, 0, len(units)) |
323 | for _, unit := range units { | 323 | for _, unit := range units { |
324 | if whitelistPattern.MatchString(unit.Name) && !blacklistPattern.MatchString(unit.Name) { | 324 | if whitelistPattern.MatchString(unit.Name) && !blacklistPattern.MatchString(unit.Name) && unit.LoadState == "loaded" { |
325 | log.Debugf("Adding unit: %s", unit.Name) | ||
325 | filtered = append(filtered, unit) | 326 | filtered = append(filtered, unit) |
326 | } else { | 327 | } else { |
327 | log.Debugf("Ignoring unit: %s", unit.Name) | 328 | log.Debugf("Ignoring unit: %s", unit.Name) |
diff --git a/collector/systemd_linux_test.go b/collector/systemd_linux_test.go index 3d7d7f6..26257aa 100644 --- a/collector/systemd_linux_test.go +++ b/collector/systemd_linux_test.go | |||
@@ -126,7 +126,8 @@ func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) { | |||
126 | fixtures := getUnitListFixtures() | 126 | fixtures := getUnitListFixtures() |
127 | collector := c.(*systemdCollector) | 127 | collector := c.(*systemdCollector) |
128 | filtered := filterUnits(fixtures[0], collector.unitWhitelistPattern, collector.unitBlacklistPattern) | 128 | filtered := filterUnits(fixtures[0], collector.unitWhitelistPattern, collector.unitBlacklistPattern) |
129 | if len(filtered) != len(fixtures[0]) { | 129 | // Adjust fixtures by 3 "not-found" units. |
130 | if len(filtered) != len(fixtures[0])-3 { | ||
130 | t.Error("Default filters removed units") | 131 | t.Error("Default filters removed units") |
131 | } | 132 | } |
132 | } | 133 | } |