2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2016-03-01 02:09:48 -05:00
|
|
|
"sync"
|
2015-03-24 23:57:23 -04:00
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2016-02-03 18:41:26 -05:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/events"
|
|
|
|
"github.com/docker/engine-api/types/filters"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdStats displays a live stream of resource usage statistics for one or more containers.
|
|
|
|
//
|
|
|
|
// This shows real-time information on CPU usage, memory usage, and network I/O.
|
|
|
|
//
|
2015-10-03 08:53:25 -04:00
|
|
|
// Usage: docker stats [OPTIONS] [CONTAINER...]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdStats(args ...string) error {
|
2015-10-03 08:53:25 -04:00
|
|
|
cmd := Cli.Subcmd("stats", []string{"[CONTAINER...]"}, Cli.DockerCommands["stats"].Description, true)
|
|
|
|
all := cmd.Bool([]string{"a", "-all"}, false, "Show all containers (default shows just running)")
|
2015-02-13 11:45:04 -05:00
|
|
|
noStream := cmd.Bool([]string{"-no-stream"}, false, "Disable streaming stats and only pull the first result")
|
2015-07-03 05:26:09 -04:00
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
|
|
|
names := cmd.Args()
|
2015-10-03 08:53:25 -04:00
|
|
|
showAll := len(names) == 0
|
2016-02-29 14:24:51 -05:00
|
|
|
closeChan := make(chan error)
|
2016-02-27 21:30:31 -05:00
|
|
|
|
|
|
|
// monitorContainerEvents watches for container creation and removal (only
|
|
|
|
// used when calling `docker stats` without arguments).
|
2016-02-29 14:24:51 -05:00
|
|
|
monitorContainerEvents := func(started chan<- struct{}, c chan events.Message) {
|
2016-02-27 21:30:31 -05:00
|
|
|
f := filters.NewArgs()
|
|
|
|
f.Add("type", "container")
|
|
|
|
options := types.EventsOptions{
|
|
|
|
Filters: f,
|
|
|
|
}
|
|
|
|
resBody, err := cli.client.Events(context.Background(), options)
|
|
|
|
// Whether we successfully subscribed to events or not, we can now
|
|
|
|
// unblock the main goroutine.
|
|
|
|
close(started)
|
|
|
|
if err != nil {
|
2016-02-29 14:24:51 -05:00
|
|
|
closeChan <- err
|
2016-02-27 21:30:31 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resBody.Close()
|
2016-02-29 14:24:51 -05:00
|
|
|
|
2016-02-27 21:30:31 -05:00
|
|
|
decodeEvents(resBody, func(event events.Message, err error) error {
|
|
|
|
if err != nil {
|
2016-02-29 14:24:51 -05:00
|
|
|
closeChan <- err
|
|
|
|
return nil
|
2016-02-27 21:30:31 -05:00
|
|
|
}
|
2016-02-29 14:24:51 -05:00
|
|
|
c <- event
|
2016-02-27 21:30:31 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-01 02:09:48 -05:00
|
|
|
// waitFirst is a WaitGroup to wait first stat data's reach for each container
|
|
|
|
waitFirst := &sync.WaitGroup{}
|
|
|
|
|
2016-02-29 14:24:51 -05:00
|
|
|
cStats := stats{}
|
2016-02-27 21:30:31 -05:00
|
|
|
// getContainerList simulates creation event for all previously existing
|
|
|
|
// containers (only used when calling `docker stats` without arguments).
|
2016-02-29 14:24:51 -05:00
|
|
|
getContainerList := func() {
|
2015-12-06 02:34:23 -05:00
|
|
|
options := types.ContainerListOptions{
|
|
|
|
All: *all,
|
2015-10-03 08:53:25 -04:00
|
|
|
}
|
2016-03-16 15:19:22 -04:00
|
|
|
cs, err := cli.client.ContainerList(context.Background(), options)
|
2015-10-03 08:53:25 -04:00
|
|
|
if err != nil {
|
2016-02-29 14:24:51 -05:00
|
|
|
closeChan <- err
|
2015-10-03 08:53:25 -04:00
|
|
|
}
|
2016-02-29 14:24:51 -05:00
|
|
|
for _, container := range cs {
|
|
|
|
s := &containerStats{Name: container.ID[:12]}
|
2016-03-01 02:09:48 -05:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
|
|
|
go s.Collect(cli.client, !*noStream, waitFirst)
|
|
|
|
}
|
2015-10-03 08:53:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-27 21:30:31 -05:00
|
|
|
if showAll {
|
|
|
|
// If no names were specified, start a long running goroutine which
|
|
|
|
// monitors container events. We make sure we're subscribed before
|
|
|
|
// retrieving the list of running containers to avoid a race where we
|
|
|
|
// would "miss" a creation.
|
|
|
|
started := make(chan struct{})
|
2016-02-29 14:24:51 -05:00
|
|
|
eh := eventHandler{handlers: make(map[string]func(events.Message))}
|
|
|
|
eh.Handle("create", func(e events.Message) {
|
|
|
|
if *all {
|
|
|
|
s := &containerStats{Name: e.ID[:12]}
|
2016-03-01 02:09:48 -05:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
|
|
|
go s.Collect(cli.client, !*noStream, waitFirst)
|
|
|
|
}
|
2016-02-29 14:24:51 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
eh.Handle("start", func(e events.Message) {
|
|
|
|
s := &containerStats{Name: e.ID[:12]}
|
2016-03-01 02:09:48 -05:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
|
|
|
go s.Collect(cli.client, !*noStream, waitFirst)
|
|
|
|
}
|
2016-02-29 14:24:51 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
eh.Handle("die", func(e events.Message) {
|
|
|
|
if !*all {
|
|
|
|
cStats.remove(e.ID[:12])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
eventChan := make(chan events.Message)
|
|
|
|
go eh.Watch(eventChan)
|
|
|
|
go monitorContainerEvents(started, eventChan)
|
|
|
|
defer close(eventChan)
|
2016-02-27 21:30:31 -05:00
|
|
|
<-started
|
|
|
|
|
|
|
|
// Start a short-lived goroutine to retrieve the initial list of
|
|
|
|
// containers.
|
2016-03-01 02:09:48 -05:00
|
|
|
getContainerList()
|
2015-10-03 08:53:25 -04:00
|
|
|
} else {
|
2016-02-27 21:30:31 -05:00
|
|
|
// Artificially send creation events for the containers we were asked to
|
|
|
|
// monitor (same code path than we use when monitoring all containers).
|
|
|
|
for _, name := range names {
|
2016-02-29 14:24:51 -05:00
|
|
|
s := &containerStats{Name: name}
|
2016-03-01 02:09:48 -05:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
|
|
|
go s.Collect(cli.client, !*noStream, waitFirst)
|
|
|
|
}
|
2016-02-27 21:30:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't expect any asynchronous errors: closeChan can be closed.
|
2015-10-03 08:53:25 -04:00
|
|
|
close(closeChan)
|
2016-02-27 21:30:31 -05:00
|
|
|
|
|
|
|
// Do a quick pause to detect any error with the provided list of
|
|
|
|
// container names.
|
|
|
|
time.Sleep(1500 * time.Millisecond)
|
|
|
|
var errs []string
|
|
|
|
cStats.mu.Lock()
|
|
|
|
for _, c := range cStats.cs {
|
|
|
|
c.mu.Lock()
|
|
|
|
if c.err != nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("%s: %v", c.Name, c.err))
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
cStats.mu.Unlock()
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return fmt.Errorf("%s", strings.Join(errs, ", "))
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-27 21:30:31 -05:00
|
|
|
|
2016-03-01 02:09:48 -05:00
|
|
|
// before print to screen, make sure each container get at least one valid stat data
|
|
|
|
waitFirst.Wait()
|
|
|
|
|
2016-02-27 21:30:31 -05:00
|
|
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
|
|
|
printHeader := func() {
|
|
|
|
if !*noStream {
|
|
|
|
fmt.Fprint(cli.out, "\033[2J")
|
|
|
|
fmt.Fprint(cli.out, "\033[H")
|
|
|
|
}
|
2015-12-15 14:15:43 -05:00
|
|
|
io.WriteString(w, "CONTAINER\tCPU %\tMEM USAGE / LIMIT\tMEM %\tNET I/O\tBLOCK I/O\tPIDS\n")
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2016-02-29 14:24:51 -05:00
|
|
|
|
2015-04-20 04:08:01 -04:00
|
|
|
for range time.Tick(500 * time.Millisecond) {
|
2015-03-24 23:57:23 -04:00
|
|
|
printHeader()
|
|
|
|
toRemove := []int{}
|
2015-10-03 08:53:25 -04:00
|
|
|
cStats.mu.Lock()
|
|
|
|
for i, s := range cStats.cs {
|
2015-02-13 11:45:04 -05:00
|
|
|
if err := s.Display(w); err != nil && !*noStream {
|
2015-03-24 23:57:23 -04:00
|
|
|
toRemove = append(toRemove, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for j := len(toRemove) - 1; j >= 0; j-- {
|
|
|
|
i := toRemove[j]
|
2015-10-03 08:53:25 -04:00
|
|
|
cStats.cs = append(cStats.cs[:i], cStats.cs[i+1:]...)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-10-03 08:53:25 -04:00
|
|
|
if len(cStats.cs) == 0 && !showAll {
|
2015-03-24 23:57:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-03 08:53:25 -04:00
|
|
|
cStats.mu.Unlock()
|
2015-03-24 23:57:23 -04:00
|
|
|
w.Flush()
|
2015-02-13 11:45:04 -05:00
|
|
|
if *noStream {
|
|
|
|
break
|
|
|
|
}
|
2015-10-03 08:53:25 -04:00
|
|
|
select {
|
|
|
|
case err, ok := <-closeChan:
|
|
|
|
if ok {
|
|
|
|
if err != nil {
|
|
|
|
// this is suppressing "unexpected EOF" in the cli when the
|
2015-12-10 01:57:25 -05:00
|
|
|
// daemon restarts so it shutdowns cleanly
|
2015-10-03 08:53:25 -04:00
|
|
|
if err == io.ErrUnexpectedEOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// just skip
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|