From 88df4ea0f97f2892a6fd588bd3abfad1db634629 Mon Sep 17 00:00:00 2001 From: haircommander Date: Thu, 16 Aug 2018 18:26:24 -0400 Subject: Add podman pod top Using the vendored changes from psgo, incorporate JoinNamespaceAndProcessInfoByPids to get process information for each pid namespace of running containers in the pod. Also added a man page, and tests. Signed-off-by: haircommander Closes: #1298 Approved by: mheon --- cmd/podman/pod.go | 1 + cmd/podman/pod_top.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 cmd/podman/pod_top.go (limited to 'cmd/podman') diff --git a/cmd/podman/pod.go b/cmd/podman/pod.go index e691204d8..dde43f77c 100644 --- a/cmd/podman/pod.go +++ b/cmd/podman/pod.go @@ -20,6 +20,7 @@ Pods are a group of one or more containers sharing the same network, pid and ipc podStartCommand, podStatsCommand, podStopCommand, + podTopCommand, podUnpauseCommand, } podCommand = cli.Command{ diff --git a/cmd/podman/pod_top.go b/cmd/podman/pod_top.go new file mode 100644 index 000000000..703ec0a44 --- /dev/null +++ b/cmd/podman/pod_top.go @@ -0,0 +1,101 @@ +package main + +import ( + "fmt" + "os" + "strings" + "text/tabwriter" + + "github.com/containers/libpod/cmd/podman/libpodruntime" + "github.com/containers/libpod/cmd/podman/shared" + "github.com/containers/libpod/libpod" + "github.com/pkg/errors" + "github.com/urfave/cli" +) + +var ( + podTopFlags = []cli.Flag{ + LatestFlag, + cli.BoolFlag{ + Name: "list-descriptors", + Hidden: true, + }, + } + podTopDescription = fmt.Sprintf(`Display the running processes containers in a pod. Specify format descriptors +to alter the output. You may run "podman pod top -l pid pcpu seccomp" to print +the process ID, the CPU percentage and the seccomp mode of each process of +the latest pod. +%s +`, getDescriptorString()) + + podTopCommand = cli.Command{ + Name: "top", + Usage: "Display the running processes of containers in a pod", + Description: podTopDescription, + Flags: podTopFlags, + Action: podTopCmd, + ArgsUsage: "POD-NAME [format descriptors]", + SkipArgReorder: true, + } +) + +func podTopCmd(c *cli.Context) error { + var pod *libpod.Pod + var err error + args := c.Args() + + if c.Bool("list-descriptors") { + descriptors, err := libpod.GetContainerPidInformationDescriptors() + if err != nil { + return err + } + fmt.Println(strings.Join(descriptors, "\n")) + return nil + } + + if len(args) < 1 && !c.Bool("latest") { + return errors.Errorf("you must provide the name or id of a running pod") + } + if err := validateFlags(c, podTopFlags); err != nil { + return err + } + + runtime, err := libpodruntime.GetRuntime(c) + if err != nil { + return errors.Wrapf(err, "error creating libpod runtime") + } + defer runtime.Shutdown(false) + + var descriptors []string + if c.Bool("latest") { + descriptors = args + pod, err = runtime.GetLatestPod() + } else { + descriptors = args[1:] + pod, err = runtime.LookupPod(args[0]) + } + + if err != nil { + return errors.Wrapf(err, "unable to lookup requested container") + } + + podStatus, err := shared.GetPodStatus(pod) + if err != nil { + return err + } + if podStatus != "Running" { + return errors.Errorf("pod top can only be used on pods with at least one running container") + } + + psOutput, err := pod.GetPodPidInformation(descriptors) + if err != nil { + return err + } + + w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0) + for _, proc := range psOutput { + fmt.Fprintln(w, proc) + } + w.Flush() + return nil +} -- cgit v1.2.3-54-g00ecf