2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2021-03-09 18:45:56 -05:00
|
|
|
flagsHelper "github.com/docker/cli/cli/flags"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/events"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2017-03-27 21:21:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type statsOptions struct {
|
2016-07-18 14:30:15 -04:00
|
|
|
all bool
|
|
|
|
noStream bool
|
2017-09-27 12:24:26 -04:00
|
|
|
noTrunc bool
|
2016-07-18 14:30:15 -04:00
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
containers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStatsCommand creates a new cobra.Command for `docker stats`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewStatsCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
var opts statsOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "stats [OPTIONS] [CONTAINER...]",
|
|
|
|
Short: "Display a live stream of container(s) resource usage statistics",
|
|
|
|
Args: cli.RequiresMinArgs(0),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.containers = args
|
|
|
|
return runStats(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.all, "all", "a", false, "Show all containers (default shows just running)")
|
|
|
|
flags.BoolVar(&opts.noStream, "no-stream", false, "Disable streaming stats and only pull the first result")
|
2017-09-27 12:24:26 -04:00
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
|
2021-03-09 18:45:56 -05:00
|
|
|
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// runStats 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.
|
2017-05-02 16:14:43 -04:00
|
|
|
// nolint: gocyclo
|
2017-10-11 12:18:27 -04:00
|
|
|
func runStats(dockerCli command.Cli, opts *statsOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
showAll := len(opts.containers) == 0
|
|
|
|
closeChan := make(chan error)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// monitorContainerEvents watches for container creation and removal (only
|
|
|
|
// used when calling `docker stats` without arguments).
|
2022-05-10 06:10:12 -04:00
|
|
|
monitorContainerEvents := func(started chan<- struct{}, c chan events.Message, stopped <-chan struct{}) {
|
2016-09-08 13:11:39 -04:00
|
|
|
f := filters.NewArgs()
|
|
|
|
f.Add("type", "container")
|
|
|
|
options := types.EventsOptions{
|
|
|
|
Filters: f,
|
|
|
|
}
|
2016-08-09 16:34:07 -04:00
|
|
|
|
|
|
|
eventq, errq := dockerCli.Client().Events(ctx, options)
|
|
|
|
|
|
|
|
// Whether we successfully subscribed to eventq or not, we can now
|
2016-09-08 13:11:39 -04:00
|
|
|
// unblock the main goroutine.
|
|
|
|
close(started)
|
2022-05-10 06:10:12 -04:00
|
|
|
defer close(c)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-08-09 16:34:07 -04:00
|
|
|
for {
|
|
|
|
select {
|
2022-05-10 06:10:12 -04:00
|
|
|
case <-stopped:
|
|
|
|
return
|
2016-08-09 16:34:07 -04:00
|
|
|
case event := <-eventq:
|
|
|
|
c <- event
|
|
|
|
case err := <-errq:
|
2016-09-08 13:11:39 -04:00
|
|
|
closeChan <- err
|
2016-08-09 16:34:07 -04:00
|
|
|
return
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-08-09 16:34:07 -04:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-10-25 19:19:14 -04:00
|
|
|
// Get the daemonOSType if not set already
|
|
|
|
if daemonOSType == "" {
|
|
|
|
svctx := context.Background()
|
|
|
|
sv, err := dockerCli.Client().ServerVersion(svctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
daemonOSType = sv.Os
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
// waitFirst is a WaitGroup to wait first stat data's reach for each container
|
|
|
|
waitFirst := &sync.WaitGroup{}
|
|
|
|
|
|
|
|
cStats := stats{}
|
|
|
|
// getContainerList simulates creation event for all previously existing
|
|
|
|
// containers (only used when calling `docker stats` without arguments).
|
|
|
|
getContainerList := func() {
|
|
|
|
options := types.ContainerListOptions{
|
|
|
|
All: opts.all,
|
|
|
|
}
|
|
|
|
cs, err := dockerCli.Client().ContainerList(ctx, options)
|
|
|
|
if err != nil {
|
|
|
|
closeChan <- err
|
|
|
|
}
|
|
|
|
for _, container := range cs {
|
2018-10-23 11:05:44 -04:00
|
|
|
s := NewStats(container.ID[:12])
|
2016-09-08 13:11:39 -04:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
2016-11-12 01:14:34 -05:00
|
|
|
go collect(ctx, s, dockerCli.Client(), !opts.noStream, waitFirst)
|
2016-09-08 13:11:39 -04: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-09-22 17:04:34 -04:00
|
|
|
eh := command.InitEventHandler()
|
2016-09-08 13:11:39 -04:00
|
|
|
eh.Handle("create", func(e events.Message) {
|
|
|
|
if opts.all {
|
2018-10-23 11:05:44 -04:00
|
|
|
s := NewStats(e.ID[:12])
|
2016-09-08 13:11:39 -04:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
2016-11-12 01:14:34 -05:00
|
|
|
go collect(ctx, s, dockerCli.Client(), !opts.noStream, waitFirst)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
eh.Handle("start", func(e events.Message) {
|
2018-10-23 11:05:44 -04:00
|
|
|
s := NewStats(e.ID[:12])
|
2016-09-08 13:11:39 -04:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
2016-11-12 01:14:34 -05:00
|
|
|
go collect(ctx, s, dockerCli.Client(), !opts.noStream, waitFirst)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
eh.Handle("die", func(e events.Message) {
|
|
|
|
if !opts.all {
|
|
|
|
cStats.remove(e.ID[:12])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
eventChan := make(chan events.Message)
|
|
|
|
go eh.Watch(eventChan)
|
2022-05-10 06:10:12 -04:00
|
|
|
stopped := make(chan struct{})
|
|
|
|
go monitorContainerEvents(started, eventChan, stopped)
|
|
|
|
defer close(stopped)
|
2016-09-08 13:11:39 -04:00
|
|
|
<-started
|
|
|
|
|
|
|
|
// Start a short-lived goroutine to retrieve the initial list of
|
|
|
|
// containers.
|
|
|
|
getContainerList()
|
2020-11-10 11:46:53 -05:00
|
|
|
|
2020-10-12 04:32:53 -04:00
|
|
|
// make sure each container get at least one valid stat data
|
|
|
|
waitFirst.Wait()
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
|
|
|
// 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 opts.containers {
|
2018-10-23 11:05:44 -04:00
|
|
|
s := NewStats(name)
|
2016-09-08 13:11:39 -04:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
2016-11-12 01:14:34 -05:00
|
|
|
go collect(ctx, s, dockerCli.Client(), !opts.noStream, waitFirst)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't expect any asynchronous errors: closeChan can be closed.
|
|
|
|
close(closeChan)
|
|
|
|
|
2020-10-12 04:32:53 -04:00
|
|
|
// make sure each container get at least one valid stat data
|
|
|
|
waitFirst.Wait()
|
2020-11-10 11:46:53 -05:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
var errs []string
|
|
|
|
cStats.mu.Lock()
|
|
|
|
for _, c := range cStats.cs {
|
2016-12-16 15:17:39 -05:00
|
|
|
if err := c.GetError(); err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cStats.mu.Unlock()
|
|
|
|
if len(errs) > 0 {
|
2016-12-24 19:05:37 -05:00
|
|
|
return errors.New(strings.Join(errs, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 14:48:25 -04:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
if len(dockerCli.ConfigFile().StatsFormat) > 0 {
|
|
|
|
format = dockerCli.ConfigFile().StatsFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
|
|
|
statsCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2018-10-23 11:05:44 -04:00
|
|
|
Format: NewStatsFormat(format, daemonOSType),
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
2016-09-22 08:54:41 -04:00
|
|
|
cleanScreen := func() {
|
2016-09-08 13:11:39 -04:00
|
|
|
if !opts.noStream {
|
|
|
|
fmt.Fprint(dockerCli.Out(), "\033[2J")
|
|
|
|
fmt.Fprint(dockerCli.Out(), "\033[H")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 14:30:15 -04:00
|
|
|
var err error
|
2019-04-02 05:11:36 -04:00
|
|
|
ticker := time.NewTicker(500 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for range ticker.C {
|
2016-09-22 08:54:41 -04:00
|
|
|
cleanScreen()
|
2018-10-23 11:05:44 -04:00
|
|
|
ccstats := []StatsEntry{}
|
2016-09-22 08:54:41 -04:00
|
|
|
cStats.mu.Lock()
|
|
|
|
for _, c := range cStats.cs {
|
|
|
|
ccstats = append(ccstats, c.GetStatistics())
|
|
|
|
}
|
|
|
|
cStats.mu.Unlock()
|
2018-10-23 11:05:44 -04:00
|
|
|
if err = statsFormatWrite(statsCtx, ccstats, daemonOSType, !opts.noTrunc); err != nil {
|
2016-07-18 14:30:15 -04:00
|
|
|
break
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-22 08:54:41 -04:00
|
|
|
if len(cStats.cs) == 0 && !showAll {
|
2016-07-18 14:30:15 -04:00
|
|
|
break
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
if opts.noStream {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case err, ok := <-closeChan:
|
|
|
|
if ok {
|
|
|
|
if err != nil {
|
|
|
|
// this is suppressing "unexpected EOF" in the cli when the
|
|
|
|
// daemon restarts so it shutdowns cleanly
|
|
|
|
if err == io.ErrUnexpectedEOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// just skip
|
|
|
|
}
|
|
|
|
}
|
2016-07-18 14:30:15 -04:00
|
|
|
return err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|