/
stats.go
246 lines (224 loc) · 6.79 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package containers
import (
"fmt"
"os"
"strings"
"sync"
"text/tabwriter"
"text/template"
tm "github.com/buger/goterm"
"github.com/containers/libpod/v2/cmd/podman/registry"
"github.com/containers/libpod/v2/cmd/podman/validate"
"github.com/containers/libpod/v2/libpod/define"
"github.com/containers/libpod/v2/pkg/cgroups"
"github.com/containers/libpod/v2/pkg/domain/entities"
"github.com/containers/libpod/v2/pkg/rootless"
"github.com/containers/libpod/v2/utils"
"github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var (
statsDescription = "Display percentage of CPU, memory, network I/O, block I/O and PIDs for one or more containers."
statsCommand = &cobra.Command{
Use: "stats [flags] [CONTAINER...]",
Short: "Display a live stream of container resource usage statistics",
Long: statsDescription,
RunE: stats,
Args: checkStatOptions,
Example: `podman stats --all --no-stream
podman stats ctrID
podman stats --no-stream --format "table {{.ID}} {{.Name}} {{.MemUsage}}" ctrID`,
}
containerStatsCommand = &cobra.Command{
Use: statsCommand.Use,
Short: statsCommand.Short,
Long: statsCommand.Long,
RunE: statsCommand.RunE,
Args: checkStatOptions,
Example: `podman container stats --all --no-stream
podman container stats ctrID
podman container stats --no-stream --format "table {{.ID}} {{.Name}} {{.MemUsage}}" ctrID`,
}
)
var (
statsOptions entities.ContainerStatsOptions
defaultStatsRow = "{{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDS}}\n"
defaultStatsHeader = "ID\tNAME\tCPU %\tMEM USAGE / LIMIT\tMEM %\tNET IO\tBLOCK IO\tPIDS\n"
)
func statFlags(flags *pflag.FlagSet) {
flags.BoolVarP(&statsOptions.All, "all", "a", false, "Show all containers. Only running containers are shown by default. The default is false")
flags.StringVar(&statsOptions.Format, "format", "", "Pretty-print container statistics to JSON or using a Go template")
flags.BoolVar(&statsOptions.NoReset, "no-reset", false, "Disable resetting the screen between intervals")
flags.BoolVar(&statsOptions.NoStream, "no-stream", false, "Disable streaming stats and only pull the first result, default setting is false")
}
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: statsCommand,
})
statFlags(statsCommand.Flags())
validate.AddLatestFlag(statsCommand, &statsOptions.Latest)
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: containerStatsCommand,
Parent: containerCmd,
})
statFlags(containerStatsCommand.Flags())
validate.AddLatestFlag(containerStatsCommand, &statsOptions.Latest)
}
// stats is different in that it will assume running containers if
// no input is given, so we need to validate differently
func checkStatOptions(cmd *cobra.Command, args []string) error {
opts := 0
if statsOptions.All {
opts++
}
if statsOptions.Latest {
opts++
}
if len(args) > 0 {
opts++
}
if opts > 1 {
return errors.Errorf("--all, --latest and containers cannot be used together")
}
return nil
}
func stats(cmd *cobra.Command, args []string) error {
if rootless.IsRootless() {
unified, err := cgroups.IsCgroup2UnifiedMode()
if err != nil {
return err
}
if !unified {
return errors.New("stats is not supported in rootless mode without cgroups v2")
}
}
statsOptions.StatChan = make(chan []*define.ContainerStats, 1)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
for reports := range statsOptions.StatChan {
if err := outputStats(reports); err != nil {
logrus.Error(err)
}
}
wg.Done()
}()
err := registry.ContainerEngine().ContainerStats(registry.Context(), args, statsOptions)
wg.Wait()
return err
}
func outputStats(reports []*define.ContainerStats) error {
if len(statsOptions.Format) < 1 && !statsOptions.NoReset {
tm.Clear()
tm.MoveCursor(1, 1)
tm.Flush()
}
stats := make([]*containerStats, 0, len(reports))
for _, r := range reports {
stats = append(stats, &containerStats{r})
}
if statsOptions.Format == "json" {
return outputJSON(stats)
}
format := defaultStatsRow
if len(statsOptions.Format) > 0 {
format = statsOptions.Format
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
}
format = "{{range . }}" + format + "{{end}}"
if len(statsOptions.Format) < 1 {
format = defaultStatsHeader + format
}
tmpl, err := template.New("stats").Parse(format)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0)
if err := tmpl.Execute(w, stats); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
return nil
}
type containerStats struct {
*define.ContainerStats
}
func (s *containerStats) ID() string {
return s.ContainerID[0:12]
}
func (s *containerStats) CPUPerc() string {
return floatToPercentString(s.CPU)
}
func (s *containerStats) MemPerc() string {
return floatToPercentString(s.ContainerStats.MemPerc)
}
func (s *containerStats) NetIO() string {
return combineHumanValues(s.NetInput, s.NetOutput)
}
func (s *containerStats) BlockIO() string {
return combineHumanValues(s.BlockInput, s.BlockOutput)
}
func (s *containerStats) PIDS() string {
if s.PIDs == 0 {
// If things go bazinga, return a safe value
return "--"
}
return fmt.Sprintf("%d", s.PIDs)
}
func (s *containerStats) MemUsage() string {
return combineHumanValues(s.ContainerStats.MemUsage, s.ContainerStats.MemLimit)
}
func floatToPercentString(f float64) string {
strippedFloat, err := utils.RemoveScientificNotationFromFloat(f)
if err != nil || strippedFloat == 0 {
// If things go bazinga, return a safe value
return "--"
}
return fmt.Sprintf("%.2f", strippedFloat) + "%"
}
func combineHumanValues(a, b uint64) string {
if a == 0 && b == 0 {
return "-- / --"
}
return fmt.Sprintf("%s / %s", units.HumanSize(float64(a)), units.HumanSize(float64(b)))
}
func outputJSON(stats []*containerStats) error {
type jstat struct {
Id string `json:"id"` //nolint
Name string `json:"name"`
CpuPercent string `json:"cpu_percent"` //nolint
MemUsage string `json:"mem_usage"`
MemPerc string `json:"mem_percent"`
NetIO string `json:"net_io"`
BlockIO string `json:"block_io"`
Pids string `json:"pids"`
}
jstats := make([]jstat, 0, len(stats))
for _, j := range stats {
jstats = append(jstats, jstat{
Id: j.ID(),
Name: j.Name,
CpuPercent: j.CPUPerc(),
MemUsage: j.MemUsage(),
MemPerc: j.MemPerc(),
NetIO: j.NetIO(),
BlockIO: j.BlockIO(),
Pids: j.PIDS(),
})
}
b, err := json.MarshalIndent(jstats, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}