1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package pods
import (
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/containers/podman/v2/cmd/podman/registry"
"github.com/containers/podman/v2/cmd/podman/validate"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/containers/podman/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
topDescription = `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.`
topOptions = entities.PodTopOptions{}
topCommand = &cobra.Command{
Use: "top [options] POD [FORMAT-DESCRIPTORS|ARGS...]",
Short: "Display the running processes of containers in a pod",
Long: topDescription,
RunE: top,
Args: cobra.ArbitraryArgs,
Example: `podman pod top podID
podman pod top --latest
podman pod top podID pid seccomp args %C
podman pod top podID -eo user,pid,comm`,
}
)
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: topCommand,
Parent: podCmd,
})
descriptors, err := util.GetContainerPidInformationDescriptors()
if err == nil {
topDescription = fmt.Sprintf("%s\n\n Format Descriptors:\n %s", topDescription, strings.Join(descriptors, ","))
topCommand.Long = topDescription
}
flags := topCommand.Flags()
flags.SetInterspersed(false)
flags.BoolVar(&topOptions.ListDescriptors, "list-descriptors", false, "")
_ = flags.MarkHidden("list-descriptors") // meant only for bash completion
validate.AddLatestFlag(topCommand, &topOptions.Latest)
}
func top(_ *cobra.Command, args []string) error {
if topOptions.ListDescriptors {
descriptors, err := util.GetContainerPidInformationDescriptors()
if err != nil {
return err
}
fmt.Println(strings.Join(descriptors, "\n"))
return nil
}
if len(args) < 1 && !topOptions.Latest {
return errors.Errorf("you must provide the name or id of a running pod")
}
if topOptions.Latest {
topOptions.Descriptors = args
} else {
topOptions.NameOrID = args[0]
topOptions.Descriptors = args[1:]
}
topResponse, err := registry.ContainerEngine().PodTop(context.Background(), topOptions)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
for _, proc := range topResponse.Value {
if _, err := fmt.Fprintln(w, proc); err != nil {
return err
}
}
return w.Flush()
}
|