Commit 248b02da authored by ale's avatar ale
Browse files

Handle change in cgroups hierarchy root with Debian Bullseye

The cgroups tree is no longer rooted at /sys/fs/cgroup/systemd, the
'systemd' part has now been dropped.

Fixes issue ai3/prod#252.
parent 62bd6c04
Pipeline #20542 passed with stages
in 2 minutes and 34 seconds
......@@ -29,13 +29,25 @@ var (
doDebug = flag.Bool("debug", false, "log debug messages")
userHZ float64
cgroupsRootPath string
)
func setCgroupsRootPath() {
// Handle the drop of the 'systemd' hierarchy with Debian Bullseye.
cgroupsRootPath = "/sys/fs/cgroup/systemd"
if _, err := os.Stat(cgroupsRootPath); os.IsNotExist(err) {
cgroupsRootPath = "/sys/fs/cgroup"
}
}
func init() {
userHZ = 100
if clktck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK); err == nil {
userHZ = float64(clktck)
}
setCgroupsRootPath()
}
func debug(s string, args ...interface{}) {
......@@ -282,9 +294,8 @@ var subsystems = []subsystem{
}
func walkCGroups() ([]prometheus.Metric, error) {
rootDir := "/sys/fs/cgroup/systemd"
var metrics []prometheus.Metric
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(cgroupsRootPath, func(path string, info os.FileInfo, err error) error {
if err != nil || !info.IsDir() || !strings.HasSuffix(path, ".service") {
return nil
}
......@@ -292,7 +303,7 @@ func walkCGroups() ([]prometheus.Metric, error) {
if strings.HasPrefix(info.Name(), "systemd-") {
return nil
}
m, err := walkCGroup(path[len(rootDir)+1:])
m, err := walkCGroup(path[len(cgroupsRootPath)+1:])
if err != nil {
return nil
}
......@@ -405,6 +416,7 @@ func main() {
mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
// nolint: errcheck
io.WriteString(w, `<html>
<body><h1>cgroups-exporter</h1><p><a href=\"/metrics\">/metrics</a></p>
</body>`)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment