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"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-04-17 18:07:56 -04:00
|
|
|
"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"
|
2023-10-13 14:34:32 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-09-08 13:11:39 -04:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2023-11-30 03:37:18 -05:00
|
|
|
// StatsOptions defines options for [RunStats].
|
|
|
|
type StatsOptions struct {
|
|
|
|
// All allows including both running and stopped containers. The default
|
2023-12-20 10:17:21 -05:00
|
|
|
// is to only include running containers.
|
2023-11-30 03:37:18 -05:00
|
|
|
All bool
|
2023-12-20 10:17:21 -05:00
|
|
|
|
2023-11-30 03:37:18 -05:00
|
|
|
// NoStream disables streaming stats. If enabled, stats are collected once,
|
2023-12-20 10:17:21 -05:00
|
|
|
// and the result is printed.
|
2023-11-30 03:37:18 -05:00
|
|
|
NoStream bool
|
2023-12-20 10:17:21 -05:00
|
|
|
|
2023-11-30 03:37:18 -05:00
|
|
|
// NoTrunc disables truncating the output. The default is to truncate
|
2023-12-20 10:17:21 -05:00
|
|
|
// output such as container-IDs.
|
2023-11-30 03:37:18 -05:00
|
|
|
NoTrunc bool
|
2023-12-20 10:17:21 -05:00
|
|
|
|
2023-11-30 03:37:18 -05:00
|
|
|
// Format is a custom template to use for presenting the stats.
|
2023-12-20 10:17:21 -05:00
|
|
|
// Refer to [flagsHelper.FormatHelp] for accepted formats.
|
2023-11-30 03:37:18 -05:00
|
|
|
Format string
|
2023-12-20 10:17:21 -05:00
|
|
|
|
2023-11-30 03:37:18 -05:00
|
|
|
// Containers is the list of container names or IDs to include in the stats.
|
|
|
|
// If empty, all containers are included. It is mutually exclusive with the
|
|
|
|
// Filters option, and an error is produced if both are set.
|
|
|
|
Containers []string
|
|
|
|
|
|
|
|
// Filters provides optional filters to filter the list of containers and their
|
|
|
|
// associated container-events to include in the stats if no list of containers
|
|
|
|
// is set. If no filter is provided, all containers are included. Filters and
|
|
|
|
// Containers are currently mutually exclusive, and setting both options
|
|
|
|
// produces an error.
|
|
|
|
//
|
|
|
|
// These filters are used both to collect the initial list of containers and
|
|
|
|
// to refresh the list of containers based on container-events, accepted
|
|
|
|
// filters are limited to the intersection of filters accepted by "events"
|
|
|
|
// and "container list".
|
|
|
|
//
|
|
|
|
// Currently only "label" / "label=value" filters are accepted. Additional
|
|
|
|
// filter options may be added in future (within the constraints described
|
|
|
|
// above), but may require daemon-side validation as the list of accepted
|
|
|
|
// filters can differ between daemon- and API versions.
|
|
|
|
Filters *filters.Args
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2023-12-20 05:23:05 -05:00
|
|
|
// NewStatsCommand creates a new [cobra.Command] for "docker stats".
|
|
|
|
func NewStatsCommand(dockerCLI command.Cli) *cobra.Command {
|
2023-11-30 03:37:18 -05:00
|
|
|
options := StatsOptions{}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
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 {
|
2023-11-30 03:37:18 -05:00
|
|
|
options.Containers = args
|
|
|
|
return RunStats(cmd.Context(), dockerCLI, &options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"aliases": "docker container stats, docker stats",
|
|
|
|
},
|
2023-12-20 05:23:05 -05:00
|
|
|
ValidArgsFunction: completion.ContainerNames(dockerCLI, false),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2023-11-30 03:37:18 -05:00
|
|
|
flags.BoolVarP(&options.All, "all", "a", false, "Show all containers (default shows just running)")
|
|
|
|
flags.BoolVar(&options.NoStream, "no-stream", false, "Disable streaming stats and only pull the first result")
|
|
|
|
flags.BoolVar(&options.NoTrunc, "no-trunc", false, "Do not truncate output")
|
|
|
|
flags.StringVar(&options.Format, "format", "", flagsHelper.FormatHelp)
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-11-30 03:37:18 -05:00
|
|
|
// acceptedStatsFilters is the list of filters accepted by [RunStats] (through
|
|
|
|
// the [StatsOptions.Filters] option).
|
|
|
|
//
|
|
|
|
// TODO(thaJeztah): don't hard-code the list of accept filters, and expand
|
|
|
|
// to the intersection of filters accepted by both "container list" and
|
|
|
|
// "system events". Validating filters may require an initial API call
|
|
|
|
// to both endpoints ("container list" and "system events").
|
|
|
|
var acceptedStatsFilters = map[string]bool{
|
|
|
|
"label": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunStats displays a live stream of resource usage statistics for one or more containers.
|
2016-09-08 13:11:39 -04:00
|
|
|
// This shows real-time information on CPU usage, memory usage, and network I/O.
|
2022-07-13 06:29:49 -04:00
|
|
|
//
|
|
|
|
//nolint:gocyclo
|
2023-11-30 03:37:18 -05:00
|
|
|
func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions) error {
|
2023-12-20 05:23:05 -05:00
|
|
|
apiClient := dockerCLI.Client()
|
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{}
|
2023-11-30 03:37:18 -05:00
|
|
|
// closeChan is a non-buffered channel used to collect errors from goroutines.
|
2023-12-20 05:54:12 -05:00
|
|
|
closeChan := make(chan error)
|
2016-09-08 13:11:39 -04:00
|
|
|
cStats := stats{}
|
|
|
|
|
2023-11-30 03:37:18 -05:00
|
|
|
showAll := len(options.Containers) == 0
|
2016-09-08 13:11:39 -04:00
|
|
|
if showAll {
|
2023-12-20 05:23:05 -05:00
|
|
|
// If no names were specified, start a long-running goroutine which
|
2016-09-08 13:11:39 -04:00
|
|
|
// 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{})
|
2023-11-30 03:37:18 -05:00
|
|
|
|
|
|
|
if options.Filters == nil {
|
|
|
|
f := filters.NewArgs()
|
|
|
|
options.Filters = &f
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := options.Filters.Validate(acceptedStatsFilters); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:04:34 -04:00
|
|
|
eh := command.InitEventHandler()
|
2023-11-30 03:37:18 -05:00
|
|
|
if options.All {
|
2023-12-20 05:34:34 -05:00
|
|
|
eh.Handle(events.ActionCreate, func(e events.Message) {
|
2023-10-13 14:34:32 -04:00
|
|
|
s := NewStats(e.Actor.ID[:12])
|
2016-09-08 13:11:39 -04:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
2023-11-30 03:37:18 -05:00
|
|
|
go collect(ctx, s, apiClient, !options.NoStream, waitFirst)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2023-12-20 05:34:34 -05:00
|
|
|
})
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2023-09-05 07:43:35 -04:00
|
|
|
eh.Handle(events.ActionStart, func(e events.Message) {
|
2023-10-13 14:34:32 -04:00
|
|
|
s := NewStats(e.Actor.ID[:12])
|
2016-09-08 13:11:39 -04:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
2023-11-30 03:37:18 -05:00
|
|
|
go collect(ctx, s, apiClient, !options.NoStream, waitFirst)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-11-30 03:37:18 -05:00
|
|
|
if !options.All {
|
2023-12-20 05:34:34 -05:00
|
|
|
eh.Handle(events.ActionDie, func(e events.Message) {
|
2023-10-13 14:34:32 -04:00
|
|
|
cStats.remove(e.Actor.ID[:12])
|
2023-12-20 05:34:34 -05:00
|
|
|
})
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2023-12-20 05:54:12 -05:00
|
|
|
// monitorContainerEvents watches for container creation and removal (only
|
|
|
|
// used when calling `docker stats` without arguments).
|
|
|
|
monitorContainerEvents := func(started chan<- struct{}, c chan events.Message, stopped <-chan struct{}) {
|
2023-11-30 03:37:18 -05:00
|
|
|
// Create a copy of the custom filters so that we don't mutate
|
|
|
|
// the original set of filters. Custom filters are used both
|
|
|
|
// to list containers and to filter events, but the "type" filter
|
|
|
|
// is not valid for filtering containers.
|
|
|
|
f := options.Filters.Clone()
|
2023-12-20 05:54:12 -05:00
|
|
|
f.Add("type", string(events.ContainerEventType))
|
|
|
|
eventChan, errChan := apiClient.Events(ctx, types.EventsOptions{
|
|
|
|
Filters: f,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Whether we successfully subscribed to eventChan or not, we can now
|
|
|
|
// unblock the main goroutine.
|
|
|
|
close(started)
|
|
|
|
defer close(c)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopped:
|
|
|
|
return
|
|
|
|
case event := <-eventChan:
|
|
|
|
c <- event
|
|
|
|
case err := <-errChan:
|
|
|
|
closeChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
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
|
|
|
|
|
2023-12-20 10:01:08 -05:00
|
|
|
// Fetch the initial list of containers and collect stats for them.
|
|
|
|
// After the initial list was collected, we start listening for events
|
|
|
|
// to refresh the list of containers.
|
|
|
|
cs, err := apiClient.ContainerList(ctx, container.ListOptions{
|
2023-11-30 03:37:18 -05:00
|
|
|
All: options.All,
|
|
|
|
Filters: *options.Filters,
|
2023-12-20 10:01:08 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, ctr := range cs {
|
|
|
|
s := NewStats(ctr.ID[:12])
|
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
2023-11-30 03:37:18 -05:00
|
|
|
go collect(ctx, s, apiClient, !options.NoStream, waitFirst)
|
2023-12-20 10:01:08 -05:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2023-11-30 03:37:18 -05:00
|
|
|
// TODO(thaJeztah): re-implement options.Containers as a filter so that
|
|
|
|
// only a single code-path is needed, and custom filters can be combined
|
|
|
|
// with a list of container names/IDs.
|
|
|
|
|
|
|
|
if options.Filters != nil && options.Filters.Len() > 0 {
|
|
|
|
return fmt.Errorf("filtering is not supported when specifying a list of containers")
|
|
|
|
}
|
|
|
|
|
2023-12-20 10:01:08 -05:00
|
|
|
// Create the list of containers, and start collecting stats for all
|
|
|
|
// containers passed.
|
2023-11-30 03:37:18 -05:00
|
|
|
for _, ctr := range options.Containers {
|
2023-12-20 10:01:08 -05:00
|
|
|
s := NewStats(ctr)
|
2016-09-08 13:11:39 -04:00
|
|
|
if cStats.add(s) {
|
|
|
|
waitFirst.Add(1)
|
2023-11-30 03:37:18 -05:00
|
|
|
go collect(ctx, s, apiClient, !options.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
|
2022-03-28 05:29:06 -04:00
|
|
|
cStats.mu.RLock()
|
2016-09-08 13:11:39 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-03-28 05:29:06 -04:00
|
|
|
cStats.mu.RUnlock()
|
2016-09-08 13:11:39 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 03:37:18 -05:00
|
|
|
format := options.Format
|
2016-10-28 14:48:25 -04:00
|
|
|
if len(format) == 0 {
|
2023-12-20 05:23:05 -05:00
|
|
|
if len(dockerCLI.ConfigFile().StatsFormat) > 0 {
|
|
|
|
format = dockerCLI.ConfigFile().StatsFormat
|
2016-10-28 14:48:25 -04:00
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
2016-07-18 14:30:15 -04:00
|
|
|
}
|
2024-03-11 06:05:56 -04:00
|
|
|
if daemonOSType == "" {
|
|
|
|
// Get the daemonOSType if not set already. The daemonOSType variable
|
|
|
|
// should already be set when collecting stats as part of "collect()",
|
|
|
|
// so we unlikely hit this code in practice.
|
|
|
|
daemonOSType = dockerCLI.ServerInfo().OSType
|
|
|
|
}
|
2016-07-18 14:30:15 -04:00
|
|
|
statsCtx := formatter.Context{
|
2023-12-20 05:23:05 -05:00
|
|
|
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() {
|
2023-11-30 03:37:18 -05:00
|
|
|
if !options.NoStream {
|
2023-12-20 05:23:05 -05:00
|
|
|
_, _ = fmt.Fprint(dockerCLI.Out(), "\033[2J")
|
|
|
|
_, _ = fmt.Fprint(dockerCLI.Out(), "\033[H")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
2023-12-20 05:23:05 -05:00
|
|
|
var ccStats []StatsEntry
|
2022-03-28 05:29:06 -04:00
|
|
|
cStats.mu.RLock()
|
2016-09-22 08:54:41 -04:00
|
|
|
for _, c := range cStats.cs {
|
2023-12-20 05:23:05 -05:00
|
|
|
ccStats = append(ccStats, c.GetStatistics())
|
2016-09-22 08:54:41 -04:00
|
|
|
}
|
2022-03-28 05:29:06 -04:00
|
|
|
cStats.mu.RUnlock()
|
2023-11-30 03:37:18 -05:00
|
|
|
if err = statsFormatWrite(statsCtx, ccStats, daemonOSType, !options.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
|
|
|
}
|
2023-11-30 03:37:18 -05:00
|
|
|
if options.NoStream {
|
2016-09-08 13:11:39 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case err, ok := <-closeChan:
|
|
|
|
if ok {
|
|
|
|
if err != nil {
|
2023-12-20 05:23:05 -05:00
|
|
|
// Suppress "unexpected EOF" errors in the CLI so that
|
|
|
|
// it shuts down cleanly when the daemon restarts.
|
|
|
|
if errors.Is(err, io.ErrUnexpectedEOF) {
|
2016-09-08 13:11:39 -04:00
|
|
|
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
|
|
|
}
|