-
Notifications
You must be signed in to change notification settings - Fork 950
/
Copy pathstats.go
138 lines (117 loc) · 3.7 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/spf13/cobra"
)
const (
containerHeader = "CONTAINER ID"
containerNameHeader = "NAME"
cpuPercHeader = "CPU %"
netIOHeader = "NET I/O"
blockIOHeader = "BLOCK I/O"
memPercHeader = "MEM %"
memUseHeader = "MEM USAGE / LIMIT"
pidsHeader = "PIDS"
)
// statsDescription is used to describe stats command in detail and auto generate command doc.
var statsDescription = "stats command is to display a live stream of container(s) resource usage statistics"
// StatsCommand use to implement 'stats' command
type StatsCommand struct {
baseCommand
noStream bool
//TODO: add more flags support
}
// Init initialize stats command.
func (stats *StatsCommand) Init(c *Cli) {
stats.cli = c
stats.cmd = &cobra.Command{
Use: "stats [OPTIONS] CONTAINER [CONTAINER...]",
Short: "Display a live stream of container(s) resource usage statistics",
Long: statsDescription,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return stats.runStats(args)
},
Example: statsExample(),
}
stats.addFlags()
}
// addFlags adds flags for specific command.
func (stats *StatsCommand) addFlags() {
flagSet := stats.cmd.Flags()
flagSet.BoolVar(&stats.noStream, "no-stream", false, "Disable streaming stats and only pull the first result")
}
// runStats is the entry of stats command.
func (stats *StatsCommand) runStats(args []string) error {
ctx := context.Background()
apiClient := stats.cli.Client()
containers := args
cStats := []*StatsEntryWithLock{}
waitFirst := &sync.WaitGroup{}
for _, name := range containers {
s := &StatsEntryWithLock{StatsEntry: StatsEntry{container: name}}
cStats = append(cStats, s)
waitFirst.Add(1)
go collect(ctx, s, apiClient, !stats.noStream, waitFirst)
}
// before print to screen, make sure each container get at least one valid stat data
waitFirst.Wait()
// do a quick scan in case container not found and so on
var errs []string
for _, c := range cStats {
err := c.GetError()
if err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
cleanScreen := func() {
if !stats.noStream {
fmt.Fprint(os.Stdout, "\033[2J")
fmt.Fprint(os.Stdout, "\033[H")
}
}
display := stats.cli.NewTableDisplay()
displayHead := []string{containerHeader, containerNameHeader, cpuPercHeader, memPercHeader,
memUseHeader, netIOHeader, blockIOHeader, pidsHeader}
for range time.Tick(500 * time.Millisecond) {
cleanScreen()
ccstats := []StatsEntry{}
// get snapshot of each container stats
for _, c := range cStats {
err := c.GetError()
if err != nil {
return err
}
ccstats = append(ccstats, c.GetStatsEntry())
}
display.AddRow(displayHead)
// display the stats of each container
for _, c := range ccstats {
displayLine := []string{c.ID(), c.Name(), c.CPUPerc(), c.MemPerc(),
c.MemUsage(), c.NetIO(), c.BlockIO(), c.PIDs()}
display.AddRow(displayLine)
}
display.Flush()
if stats.noStream {
break
}
}
return nil
}
// statsExample shows examples in stats command, and is used in auto-generated cli docs.
func statsExample() string {
return `$ pouch stats b25ae a0067
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
b25ae88e5b70 naughty_goldwasser 0.11% 2.559MiB / 15.23GiB 0.02% 7.32kB / 0B 0B / 0B 4
a00670c2bdff xenodochial_varahamihira 0.11% 2.887MiB / 15.23GiB 0.02% 13.3kB / 0B 14.7MB / 0B 4
`
}