Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3754857
D1836.id4638.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D1836.id4638.diff
View Options
diff --git a/build/root/Makefile b/build/root/Makefile
--- a/build/root/Makefile
+++ b/build/root/Makefile
@@ -3,16 +3,20 @@
RM := rm
OUT_DIR ?= target
BIN_DIR := $(OUT_DIR)/bin
+BINS=${BIN_DIR}/containers-ps ${BIN_DIR}/what-container
all: bins
-bins: $BIN_DIR/containers-ps
+bins: ${BINS}
-$BIN_DIR:
+${BIN_DIR}:
${MKDIR} ${BIN_DIR}
-$BIN_DIR/containers-ps: $BIN_DIR
+${BIN_DIR}/containers-ps: ${BIN_DIR}
${GO} build -o ${BIN_DIR}/containers-ps cmd/containersps/containersps.go
+${BIN_DIR}/what-container: ${BIN_DIR}
+ ${GO} build -o ${BIN_DIR}/what-container cmd/whatcontainer/whatcontainer.go
+
clean:
- ${RM} -f ${BIN_DIR}/containers-ps
+ ${RM} -f ${BINS}
diff --git a/cmd/containersps/containersps.go b/cmd/containersps/containersps.go
--- a/cmd/containersps/containersps.go
+++ b/cmd/containersps/containersps.go
@@ -2,13 +2,13 @@
import (
"context"
+ "devcentral.nasqueron.org/source/docker-processes/pkg/dockerutils"
"devcentral.nasqueron.org/source/docker-processes/pkg/process"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"log"
"os"
- "strings"
)
const DockerApiVersion = "1.37"
@@ -29,7 +29,7 @@
psArgs := []string{"auxw"}
for _, container := range containers {
- name := getContainerName(container)
+ name := dockerutils.GetContainerName(container)
response, err := dockerClient.ContainerTop(context.Background(), container.ID, psArgs)
if err != nil {
@@ -60,23 +60,3 @@
processInfo.VSZ, processInfo.RSS, processInfo.Command)
}
-func getContainerName(container types.Container) string {
- names := container.Names
-
- if len(names) == 0 {
- return container.ID[:10]
- }
-
- bestCandidate := names[0][1:]
-
- // Linked containers offer link names before the container name.
- if strings.Contains(bestCandidate, "/") {
- for _, name := range names {
- if !strings.Contains(name[1:], "/") {
- return name[1:]
- }
- }
- }
-
- return bestCandidate
-}
diff --git a/cmd/whatcontainer/whatcontainer.go b/cmd/whatcontainer/whatcontainer.go
new file mode 100644
--- /dev/null
+++ b/cmd/whatcontainer/whatcontainer.go
@@ -0,0 +1,112 @@
+package main
+
+import (
+ "bufio"
+ "context"
+ "devcentral.nasqueron.org/source/docker-processes/pkg/dockerutils"
+ "devcentral.nasqueron.org/source/docker-processes/pkg/process"
+ "flag"
+ "fmt"
+ "github.com/docker/docker/api/types"
+ "github.com/docker/docker/client"
+ "log"
+ "os"
+ "strconv"
+ "strings"
+)
+
+const DockerApiVersion = "1.37"
+
+type Config struct {
+ WithPosition bool
+ Position int
+}
+
+func main() {
+ config := parseArguments()
+ scanner := bufio.NewScanner(os.Stdin)
+ processesMap := getProcessesMap()
+
+ for scanner.Scan() {
+ line := addContainerName(scanner.Text(), processesMap, config)
+ fmt.Println(line)
+ }
+}
+
+func parseArguments() Config {
+ config := Config{}
+
+ positionPtr := flag.Int("p", -1, "the position of the field with the PID")
+
+ flag.Parse()
+
+ if *positionPtr > 0 {
+ config.Position = *positionPtr
+ config.WithPosition = true
+ }
+
+ return config
+}
+
+func addContainerName (line string, processesMap map[int64]string, config Config) string {
+ fields := strings.Fields(line)
+
+ for i, field := range fields {
+ if !isValidFieldPosition(i + 1, config) {
+ continue
+ }
+
+ pidCandidate, err := strconv.ParseInt(field, 10, 64)
+
+ if err != nil {
+ continue
+ }
+
+ if containerName, ok := processesMap[pidCandidate]; ok {
+ return fmt.Sprintf("%s %s", line, containerName)
+ }
+ }
+
+ return line
+}
+
+func isValidFieldPosition(position int, config Config) bool {
+ if config.WithPosition {
+ return position == config.Position
+ } else {
+ // 1 for top and ps -ef
+ // 2 for ps auxw
+ return position < 3
+ }
+}
+
+func getProcessesMap () map[int64]string {
+ dockerClient, err := client.NewClientWithOpts(client.WithVersion(DockerApiVersion))
+ if err != nil {
+ log.Println("Can't connect to Docker engine.")
+ os.Exit(1)
+ }
+
+ containers, err := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})
+ if err != nil {
+ panic(err)
+ }
+
+ processesMap := make(map[int64]string)
+
+ psArgs := []string{"auxw"}
+ for _, container := range containers {
+ containerName := dockerutils.GetContainerName(container)
+
+ response, err := dockerClient.ContainerTop(context.Background(), container.ID, psArgs)
+ if err != nil {
+ continue
+ }
+ for _, containerProcess := range response.Processes {
+ processInfo := process.Parse(response.Titles, containerProcess)
+ processesMap[processInfo.Pid] = containerName
+ }
+ }
+
+ return processesMap
+}
diff --git a/pkg/dockerutils/dockerutils.go b/pkg/dockerutils/dockerutils.go
new file mode 100644
--- /dev/null
+++ b/pkg/dockerutils/dockerutils.go
@@ -0,0 +1,27 @@
+package dockerutils
+
+import (
+ "github.com/docker/docker/api/types"
+ "strings"
+)
+
+func GetContainerName(container types.Container) string {
+ names := container.Names
+
+ if len(names) == 0 {
+ return container.ID[:10]
+ }
+
+ bestCandidate := names[0][1:]
+
+ // Linked containers offer link names before the container name.
+ if strings.Contains(bestCandidate, "/") {
+ for _, name := range names {
+ if !strings.Contains(name[1:], "/") {
+ return name[1:]
+ }
+ }
+ }
+
+ return bestCandidate
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 19, 10:32 (20 h, 8 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2252261
Default Alt Text
D1836.id4638.diff (5 KB)
Attached To
Mode
D1836: Provide a what-container pipe command to add a container column
Attached
Detach File
Event Timeline
Log In to Comment