Skip to content

Commit

Permalink
Read cgroup file to associate pids and container ids
Browse files Browse the repository at this point in the history
Refs: #18
  • Loading branch information
praith-dsg committed May 27, 2022
1 parent 8f9050d commit 717a863
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
3 changes: 0 additions & 3 deletions internal/telemd/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ func (c *DockerCgroupv1NetworkInstrument) MeasureAndReport(channel telem.Telemet
}

for _, containerId := range containerIds {
containerId = containerId[:12]
pid, ok := c.pids[containerId]

if !ok {
Expand Down Expand Up @@ -547,9 +546,7 @@ func (c *DockerCgroupv2NetworkInstrument) MeasureAndReport(channel telem.Telemet
for _, containerIdFolder := range dirs {
index := len(prefix)
stop := len(containerIdFolder) - len(".scope")
// 12 is the length of the short-form for container IDs
containerId := containerIdFolder[index:stop]
containerId = containerIdFolder[index : index+12]
pid, ok := c.pids[containerId]

if !ok {
Expand Down
53 changes: 52 additions & 1 deletion internal/telemd/sysparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,60 @@ func allPids() ([]string, error) {
return dirs, nil
}

func getContainerId(pid string) (string, error) {
// gets content of /proc/<pid>/cgroup
// in cgroup v1 is list of multiple lines -> look for first that contains 'docker' substring
// 11:freezer:/docker/dc65d1e5672961e7191260dec3dd532ad346719ea3ae23035e3b560867bd1183
file, err := os.Open("/proc/" + pid + "/cgroup")
if err != nil {
return "", err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "docker") {
split := strings.Split(line, "/")[2]
prefix := "docker-"
suffix := ".scope"

if strings.HasPrefix(split, prefix) {
// cgroup v2
return strings.TrimSuffix(strings.TrimPrefix(split, prefix), suffix), nil
} else {
// cgroup v1
return split, nil
}
}
}
return "", errors.New("Did not find container for PID " + pid)
}

func containerProcessIds() (map[string]string, error) {
// get all PIDs from /proc
pids, err := allPids()

if err != nil {
return nil, err
}

pidMap := make(map[string]string, 0)
for _, pid := range pids {
containerId, err := getContainerId(pid)
if err == nil {
if _, ok := pidMap[containerId]; !ok {
pidMap[containerId] = pid
}
}
}
return pidMap, nil
}

// requires root
func containerProcessIdsUsingNsenter() (map[string]string, error) {
// get all PIDs from /proc
pids, err := allPids()
if err != nil {
return nil, err
}
Expand All @@ -252,7 +303,7 @@ func containerProcessIds() (map[string]string, error) {
// execute for each PID 'nsenter -t $PID -u hostname'
for _, pid := range pids {
// check if result is equal to short containerId
command, err := execCommand("sudo nsenter -t " + pid + " -u hostname")
command, err := execCommand("nsenter -t " + pid + " -u hostname")
if err == nil {
pidMap[command] = pid
}
Expand Down

0 comments on commit 717a863

Please sign in to comment.