2019-02-08 04:04:22 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package crictl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
"github.com/docker/go-units"
|
2019-02-08 04:04:22 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
"golang.org/x/net/context"
|
2019-08-30 18:33:25 +00:00
|
|
|
pb "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
|
2019-02-08 04:04:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type statsOptions struct {
|
|
|
|
// all containers
|
|
|
|
all bool
|
|
|
|
// id of container
|
|
|
|
id string
|
|
|
|
// podID of container
|
|
|
|
podID string
|
|
|
|
// sample is the duration for sampling cpu usage.
|
|
|
|
sample time.Duration
|
|
|
|
// labels are selectors for the sandbox
|
|
|
|
labels map[string]string
|
|
|
|
// output format
|
|
|
|
output string
|
2019-08-30 18:33:25 +00:00
|
|
|
// live watch
|
|
|
|
watch bool
|
2019-02-08 04:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var statsCommand = cli.Command{
|
2019-08-30 18:33:25 +00:00
|
|
|
Name: "stats",
|
2019-02-08 04:04:22 +00:00
|
|
|
Usage: "List container(s) resource usage statistics",
|
|
|
|
SkipArgReorder: true,
|
|
|
|
UseShortOptionHandling: true,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "all, a",
|
|
|
|
Usage: "Show all containers (default shows just running)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "id",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Filter by container id",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pod, p",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Filter by pod id",
|
|
|
|
},
|
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "label",
|
|
|
|
Usage: "Filter by key=value label",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "output, o",
|
|
|
|
Usage: "Output format, One of: json|yaml|table",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "seconds, s",
|
|
|
|
Value: 1,
|
|
|
|
Usage: "Sample duration for CPU usage in seconds",
|
|
|
|
},
|
2019-08-30 18:33:25 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "watch, w",
|
|
|
|
Usage: "Watch pod resources",
|
|
|
|
},
|
2019-02-08 04:04:22 +00:00
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
var err error
|
|
|
|
if err = getRuntimeClient(context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := statsOptions{
|
|
|
|
all: context.Bool("all"),
|
|
|
|
id: context.String("id"),
|
|
|
|
podID: context.String("pod"),
|
|
|
|
sample: time.Duration(context.Int("seconds")) * time.Second,
|
|
|
|
output: context.String("output"),
|
2019-08-30 18:33:25 +00:00
|
|
|
watch: context.Bool("watch"),
|
2019-02-08 04:04:22 +00:00
|
|
|
}
|
|
|
|
opts.labels, err = parseLabelStringSlice(context.StringSlice("label"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ContainerStats(runtimeClient, opts); err != nil {
|
|
|
|
return fmt.Errorf("get container stats failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type containerStatsByID []*pb.ContainerStats
|
|
|
|
|
|
|
|
func (c containerStatsByID) Len() int { return len(c) }
|
|
|
|
func (c containerStatsByID) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
|
|
|
func (c containerStatsByID) Less(i, j int) bool {
|
|
|
|
return c[i].Attributes.Id < c[j].Attributes.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerStats sends a ListContainerStatsRequest to the server, and
|
|
|
|
// parses the returned ListContainerStatsResponse.
|
|
|
|
func ContainerStats(client pb.RuntimeServiceClient, opts statsOptions) error {
|
|
|
|
filter := &pb.ContainerStatsFilter{}
|
|
|
|
if opts.id != "" {
|
|
|
|
filter.Id = opts.id
|
|
|
|
}
|
|
|
|
if opts.podID != "" {
|
|
|
|
filter.PodSandboxId = opts.podID
|
|
|
|
}
|
|
|
|
if opts.labels != nil {
|
|
|
|
filter.LabelSelector = opts.labels
|
|
|
|
}
|
|
|
|
request := &pb.ListContainerStatsRequest{
|
|
|
|
Filter: filter,
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
|
|
|
|
display := newTableDisplay(20, 1, 3, ' ', 0)
|
|
|
|
if !opts.watch {
|
|
|
|
if err := displayStats(client, request, display, opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for range time.Tick(500 * time.Millisecond) {
|
|
|
|
if err := displayStats(client, request, display, opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContainerStats(client pb.RuntimeServiceClient, request *pb.ListContainerStatsRequest) (*pb.ListContainerStatsResponse, error) {
|
2019-02-08 04:04:22 +00:00
|
|
|
logrus.Debugf("ListContainerStatsRequest: %v", request)
|
|
|
|
r, err := client.ListContainerStats(context.Background(), request)
|
|
|
|
logrus.Debugf("ListContainerResponse: %v", r)
|
|
|
|
if err != nil {
|
2019-08-30 18:33:25 +00:00
|
|
|
return nil, err
|
2019-02-08 04:04:22 +00:00
|
|
|
}
|
|
|
|
sort.Sort(containerStatsByID(r.Stats))
|
2019-08-30 18:33:25 +00:00
|
|
|
return r, nil
|
|
|
|
}
|
2019-02-08 04:04:22 +00:00
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
func displayStats(client pb.RuntimeServiceClient, request *pb.ListContainerStatsRequest, display *display, opts statsOptions) error {
|
|
|
|
r, err := getContainerStats(client, request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-08 04:04:22 +00:00
|
|
|
switch opts.output {
|
|
|
|
case "json":
|
|
|
|
return outputProtobufObjAsJSON(r)
|
|
|
|
case "yaml":
|
|
|
|
return outputProtobufObjAsYAML(r)
|
|
|
|
}
|
|
|
|
oldStats := make(map[string]*pb.ContainerStats)
|
|
|
|
for _, s := range r.GetStats() {
|
|
|
|
oldStats[s.Attributes.Id] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(opts.sample)
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
r, err = getContainerStats(client, request)
|
2019-02-08 04:04:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:33:25 +00:00
|
|
|
display.AddRow([]string{columnContainer, columnCPU, columnMemory, columnDisk, columnInodes})
|
2019-02-08 04:04:22 +00:00
|
|
|
for _, s := range r.GetStats() {
|
|
|
|
id := getTruncatedID(s.Attributes.Id, "")
|
|
|
|
cpu := s.GetCpu().GetUsageCoreNanoSeconds().GetValue()
|
|
|
|
mem := s.GetMemory().GetWorkingSetBytes().GetValue()
|
|
|
|
disk := s.GetWritableLayer().GetUsedBytes().GetValue()
|
|
|
|
inodes := s.GetWritableLayer().GetInodesUsed().GetValue()
|
|
|
|
if !opts.all && cpu == 0 && mem == 0 {
|
|
|
|
// Skip non-running container
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
old, ok := oldStats[s.Attributes.Id]
|
|
|
|
if !ok {
|
|
|
|
// Skip new container
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var cpuPerc float64
|
|
|
|
if cpu != 0 {
|
|
|
|
// Only generate cpuPerc for running container
|
|
|
|
duration := s.GetCpu().GetTimestamp() - old.GetCpu().GetTimestamp()
|
|
|
|
if duration == 0 {
|
|
|
|
return fmt.Errorf("cpu stat is not updated during sample")
|
|
|
|
}
|
|
|
|
cpuPerc = float64(cpu-old.GetCpu().GetUsageCoreNanoSeconds().GetValue()) / float64(duration) * 100
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
display.AddRow([]string{id, fmt.Sprintf("%.2f", cpuPerc), units.HumanSize(float64(mem)),
|
|
|
|
units.HumanSize(float64(disk)), fmt.Sprintf("%d", inodes)})
|
|
|
|
|
2019-02-08 04:04:22 +00:00
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
display.ClearScreen()
|
|
|
|
display.Flush()
|
2019-02-08 04:04:22 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|