-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cgroups host collector (#1581)
Linux control groups host collector that detects whether the specified mountPoint is a cgroup filesystem and what version it is. The collector also collects information of the configured cgroup controllers. Signed-off-by: Evans Mungai <evans@replicated.com>
- Loading branch information
Showing
13 changed files
with
582 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// This Control Groups collector is heavily based on k0s' | ||
// probes implementation https://github.com/k0sproject/k0s/blob/main/internal/pkg/sysinfo/probes/linux/cgroups.go | ||
|
||
package collect | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
const hostCGroupsPath = `host-collectors/system/cgroups.json` | ||
|
||
type CollectHostCGroups struct { | ||
hostCollector *troubleshootv1beta2.HostCGroups | ||
BundlePath string | ||
} | ||
|
||
type cgroupResult struct { | ||
Enabled bool `json:"enabled"` | ||
MountPoint string `json:"mountPoint"` | ||
Controllers []string `json:"controllers"` | ||
} | ||
|
||
type cgroupsResult struct { | ||
CGroupEnabled bool `json:"cgroup-enabled"` | ||
CGroupV1 cgroupResult `json:"cgroup-v1"` | ||
CGroupV2 cgroupResult `json:"cgroup-v2"` | ||
// AllControllers is a list of all cgroup controllers found in the system | ||
AllControllers []string `json:"allControllers"` | ||
} | ||
|
||
func (c *CollectHostCGroups) Title() string { | ||
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "cgroups") | ||
} | ||
|
||
func (c *CollectHostCGroups) IsExcluded() (bool, error) { | ||
return isExcluded(c.hostCollector.Exclude) | ||
} | ||
|
||
func (c *CollectHostCGroups) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { | ||
// https://man7.org/linux/man-pages/man7/cgroups.7.html | ||
// Implementation is based on https://github.com/k0sproject/k0s/blob/main/internal/pkg/sysinfo/probes/linux/cgroups.go | ||
|
||
if c.hostCollector.MountPoint == "" { | ||
c.hostCollector.MountPoint = "/sys/fs/cgroup" | ||
} | ||
|
||
results, err := discoverConfiguration(c.hostCollector.MountPoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Save the results | ||
resultsJson, err := json.MarshalIndent(results, "", " ") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
output := NewResult() | ||
err = output.SaveResult(c.BundlePath, hostCGroupsPath, bytes.NewBuffer(resultsJson)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
func parseV1ControllerNames(r io.Reader) ([]string, error) { | ||
names := []string{} | ||
var lineNo uint | ||
lines := bufio.NewScanner(r) | ||
for lines.Scan() { | ||
lineNo = lineNo + 1 | ||
if err := lines.Err(); err != nil { | ||
return nil, fmt.Errorf("failed to parse /proc/cgroups at line %d: %w ", lineNo, err) | ||
} | ||
text := lines.Text() | ||
if len(text) == 0 { | ||
continue | ||
} | ||
|
||
if text[0] != '#' { | ||
parts := strings.Fields(text) | ||
if len(parts) >= 4 && parts[3] != "0" { | ||
names = append(names, parts[0]) | ||
} | ||
} | ||
} | ||
klog.V(2).Info("cgroup v1 controllers loaded") | ||
|
||
return names, nil | ||
} |
Oops, something went wrong.