config/node: refactor checking for CollectMode
shout out to Kir (@kolyshkin) for coming up with this method
Drastically simplify querying systemd for CollectMode.
This is more resistant to the feature being backported into the version shipped in distros.
@@ -4,8 +4,6 @@ package node
import (
"os/exec"
- "regexp"
- "strconv"
"sync"
"github.com/pkg/errors"
@@ -19,26 +17,13 @@ var (
func SystemdHasCollectMode() bool {
systemdHasCollectModeOnce.Do(func() {
- stdout, err := exec.Command("systemctl", "--version").Output()
+ // This will show whether the currently running systemd supports CollectMode
+ _, err := exec.Command("systemctl", "show", "-p", "CollectMode", "systemd").Output()
if err != nil {
- systemdHasCollectModeErr = err
+ systemdHasCollectModeErr = errors.Wrapf(err, "check systemd CollectMode")
return
}
- matches := regexp.MustCompile(`^systemd (?P<Version>\d+)`).FindStringSubmatch(string(stdout))
-
- if len(matches) != 2 {
- systemdHasCollectModeErr = errors.Errorf("systemd version command returned incompatible formatted information: %v", string(stdout))
- return
- }
-
- systemdVersion, err := strconv.Atoi(matches[1])
- if err != nil {
- systemdHasCollectModeErr = err
- return
- }
- if systemdVersion >= 236 {
systemdHasCollectMode = true
- }
})
return systemdHasCollectMode
}
