|
| 1 | +package collect |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "encoding/json" |
| 7 | + "os/exec" |
| 8 | + "regexp" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 12 | + "k8s.io/klog/v2" |
| 13 | +) |
| 14 | + |
| 15 | +// Ensure `CollectHostSysctl` implements `HostCollector` interface at compile time. |
| 16 | +var _ HostCollector = (*CollectHostSysctl)(nil) |
| 17 | + |
| 18 | +// Helper var to allow stubbing `exec.Command` for tests |
| 19 | +var execCommand = exec.Command |
| 20 | + |
| 21 | +const HostSysctlPath = `host-collectors/system/sysctl.json` |
| 22 | + |
| 23 | +type CollectHostSysctl struct { |
| 24 | + hostCollector *troubleshootv1beta2.HostSysctl |
| 25 | + BundlePath string |
| 26 | +} |
| 27 | + |
| 28 | +func (c *CollectHostSysctl) Title() string { |
| 29 | + return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "Sysctl") |
| 30 | +} |
| 31 | + |
| 32 | +func (c *CollectHostSysctl) IsExcluded() (bool, error) { |
| 33 | + return isExcluded(c.hostCollector.Exclude) |
| 34 | +} |
| 35 | + |
| 36 | +func (c *CollectHostSysctl) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { |
| 37 | + klog.V(2).Info("Running sysctl collector") |
| 38 | + cmd := execCommand("sysctl", "-a") |
| 39 | + out, err := cmd.Output() |
| 40 | + if err != nil { |
| 41 | + klog.V(2).ErrorS(err, "failed to run sysctl") |
| 42 | + if exitErr, ok := err.(*exec.ExitError); ok { |
| 43 | + return nil, errors.Wrapf(err, "failed to run sysctl exit-code=%d stderr=%s", exitErr.ExitCode(), exitErr.Stderr) |
| 44 | + } else { |
| 45 | + return nil, errors.Wrap(err, "failed to run sysctl") |
| 46 | + } |
| 47 | + } |
| 48 | + values := parseSysctlParameters(out) |
| 49 | + |
| 50 | + payload, err := json.Marshal(values) |
| 51 | + if err != nil { |
| 52 | + klog.V(2).ErrorS(err, "failed to marshal data to json") |
| 53 | + return nil, errors.Wrap(err, "failed to marshal data to json") |
| 54 | + } |
| 55 | + |
| 56 | + output := NewResult() |
| 57 | + output.SaveResult(c.BundlePath, HostSysctlPath, bytes.NewBuffer(payload)) |
| 58 | + klog.V(2).Info("Finished writing JSON output") |
| 59 | + return output, nil |
| 60 | +} |
| 61 | + |
| 62 | +// Linux sysctl outputs <key> = <value> where in Darwin you get <key> : <value> |
| 63 | +// where <value> can be a string, number or multiple space separated strings |
| 64 | +var sysctlLineRegex = regexp.MustCompile(`(\S+)\s*(=|:)\s*(.*)$`) |
| 65 | + |
| 66 | +func parseSysctlParameters(output []byte) map[string]string { |
| 67 | + scanner := bufio.NewScanner(bytes.NewReader(output)) |
| 68 | + |
| 69 | + result := map[string]string{} |
| 70 | + for scanner.Scan() { |
| 71 | + l := scanner.Text() |
| 72 | + // <1:key> <2:separator> <3:value> |
| 73 | + matches := sysctlLineRegex.FindStringSubmatch(l) |
| 74 | + |
| 75 | + switch len(matches) { |
| 76 | + // there are no matches for the value and separator, ignore and log |
| 77 | + case 0, 1, 2: |
| 78 | + klog.V(2).Infof("skipping sysctl line since we found no matches for it: %s", l) |
| 79 | + // key exists but value could be empty, register as an empty string value but log something for reference |
| 80 | + case 3: |
| 81 | + klog.V(2).Infof("found no value for sysctl line, keeping it with an empty value: %s", l) |
| 82 | + result[matches[1]] = "" |
| 83 | + default: |
| 84 | + result[matches[1]] = matches[3] |
| 85 | + } |
| 86 | + } |
| 87 | + return result |
| 88 | +} |
0 commit comments