diff --git a/plugin.go b/plugin.go index 606fe21..5a0c502 100644 --- a/plugin.go +++ b/plugin.go @@ -168,17 +168,24 @@ func getContainerId() (string, error) { defer file.Close() scanner := bufio.NewScanner(file) - if !scanner.Scan() { - return "", errors.New("cgroup is empty") + for scanner.Scan() { + // The file name is the container identifier + // Example: 12:rdma:/docker/a33940bf95ec6c57b6d79a3edfa784a1364ce840fb92c9a7f3951b9f7b0ccccb/docker/330d9b057fdd54b224cd38daf370524f7e8c567ccf95f33a2388f02c0b854982 + line := scanner.Text() + pathOffset := strings.LastIndex(line, ":") + if pathOffset == -1 { + continue + } + + cgroup := line[pathOffset + 1:] + + result := path.Base(cgroup) + if result == "." || result == "/" { + continue + } + + return result, nil } - // The file name is the container identifier - // Example: 12:rdma:/docker/a33940bf95ec6c57b6d79a3edfa784a1364ce840fb92c9a7f3951b9f7b0ccccb/docker/330d9b057fdd54b224cd38daf370524f7e8c567ccf95f33a2388f02c0b854982 - cgroup := scanner.Text() - result := path.Base(cgroup) - if result == "." { - return "", errors.New("cgroup is unexpected") - } - - return result, nil + return "", errors.New("no suitable cgroup entry found") } \ No newline at end of file