2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"github.com/docker/docker/engine"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
2015-03-25 10:34:41 -07:00
|
|
|
// CmdTop displays the running processes of a container.
|
|
|
|
//
|
|
|
|
// Usage: docker top CONTAINER
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdTop(args ...string) error {
|
|
|
|
cmd := cli.Subcmd("top", "CONTAINER [ps OPTIONS]", "Display the running processes of a container", true)
|
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
|
2015-03-29 03:22:46 +02:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
val := url.Values{}
|
|
|
|
if cmd.NArg() > 1 {
|
|
|
|
val.Set("ps_args", strings.Join(cmd.Args()[1:], " "))
|
|
|
|
}
|
|
|
|
|
2015-01-12 19:56:01 +00:00
|
|
|
stream, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/top?"+val.Encode(), nil, nil)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var procs engine.Env
|
|
|
|
if err := procs.Decode(stream); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
|
|
|
fmt.Fprintln(w, strings.Join(procs.GetList("Titles"), "\t"))
|
|
|
|
processes := [][]string{}
|
|
|
|
if err := procs.GetJson("Processes", &processes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, proc := range processes {
|
|
|
|
fmt.Fprintln(w, strings.Join(proc, "\t"))
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
return nil
|
|
|
|
}
|