2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-09 18:13:01 -04:00
|
|
|
"encoding/json"
|
2015-03-24 23:57:23 -04:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2015-04-09 18:13:01 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04: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 {
|
2015-05-05 00:18:28 -04:00
|
|
|
cmd := Cli.Subcmd("top", []string{"CONTAINER [ps OPTIONS]"}, "Display the running processes of a container", true)
|
2015-03-24 23:57:23 -04:00
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
|
2015-03-28 21:22:46 -04: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-07-09 22:05:50 -04:00
|
|
|
serverResp, 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
|
|
|
|
}
|
2015-04-09 18:13:01 -04:00
|
|
|
|
2015-07-09 22:05:50 -04:00
|
|
|
defer serverResp.body.Close()
|
2015-07-03 02:19:23 -04:00
|
|
|
|
2015-04-09 18:13:01 -04:00
|
|
|
procList := types.ContainerProcessList{}
|
2015-07-09 22:05:50 -04:00
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&procList); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-04-09 18:13:01 -04:00
|
|
|
|
2015-03-24 23:57:23 -04:00
|
|
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
2015-04-09 18:13:01 -04:00
|
|
|
fmt.Fprintln(w, strings.Join(procList.Titles, "\t"))
|
|
|
|
|
|
|
|
for _, proc := range procList.Processes {
|
2015-03-24 23:57:23 -04:00
|
|
|
fmt.Fprintln(w, strings.Join(proc, "\t"))
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
return nil
|
|
|
|
}
|