diff --git a/internal/telemd/instrument.go b/internal/telemd/instrument.go index b414705..63244eb 100644 --- a/internal/telemd/instrument.go +++ b/internal/telemd/instrument.go @@ -499,6 +499,7 @@ func (c *DockerCgroupv1NetworkInstrument) MeasureAndReport(channel telem.Telemet } for _, containerId := range containerIds { + containerId = containerId[:12] pid, ok := c.pids[containerId] if !ok { @@ -548,6 +549,7 @@ func (c *DockerCgroupv2NetworkInstrument) MeasureAndReport(channel telem.Telemet 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 { diff --git a/internal/telemd/sysparse.go b/internal/telemd/sysparse.go index 616f7fe..dcdf660 100644 --- a/internal/telemd/sysparse.go +++ b/internal/telemd/sysparse.go @@ -3,7 +3,9 @@ package telemd import ( "bufio" "errors" + "log" "os" + "regexp" "strconv" "strings" "time" @@ -226,7 +228,39 @@ func readTotalProcessNetworkStats(pid string) (rx int64, tx int64, err error) { return rx, tx, scanner.Err() } +func allPids() ([]string, error) { + r, _ := regexp.Compile("^\\d*") + dirs, err := listFilterDir("/proc", func(info os.FileInfo) bool { + return info.IsDir() && r.MatchString(info.Name()) + }) + + if err != nil { + log.Println("error fetching PIDs", err) + return nil, err + } + return dirs, nil +} + 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) + // 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") + if err == nil { + pidMap[command] = pid + } + } + return pidMap, nil +} + +func containerProcessIdsUsingDockerCli() (map[string]string, error) { command, err := execCommand("docker ps -q | xargs docker inspect --format '{{ .Id }} {{ .State.Pid }}'") if err != nil {