forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
260 lines (240 loc) · 5.98 KB
/
stat.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package cli
import (
"fmt"
"os"
"github.com/spf13/afero"
"golang.org/x/xerrors"
"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/cli/clistat"
"github.com/coder/coder/cli/cliui"
)
func (r *RootCmd) stat() *clibase.Cmd {
fs := afero.NewReadOnlyFs(afero.NewOsFs())
defaultCols := []string{
"host_cpu",
"host_memory",
"home_disk",
"container_cpu",
"container_memory",
}
formatter := cliui.NewOutputFormatter(
cliui.TableFormat([]statsRow{}, defaultCols),
cliui.JSONFormat(),
)
st, err := clistat.New(clistat.WithFS(fs))
if err != nil {
panic(xerrors.Errorf("initialize workspace stats collector: %w", err))
}
cmd := &clibase.Cmd{
Use: "stat",
Short: "Show resource usage for the current workspace.",
Children: []*clibase.Cmd{
r.statCPU(st, fs),
r.statMem(st, fs),
r.statDisk(st),
},
Handler: func(inv *clibase.Invocation) error {
var sr statsRow
// Get CPU measurements first.
hostErr := make(chan error, 1)
containerErr := make(chan error, 1)
go func() {
defer close(hostErr)
cs, err := st.HostCPU()
if err != nil {
hostErr <- err
return
}
sr.HostCPU = cs
}()
go func() {
defer close(containerErr)
if ok, _ := clistat.IsContainerized(fs); !ok {
// don't error if we're not in a container
return
}
cs, err := st.ContainerCPU()
if err != nil {
containerErr <- err
return
}
sr.ContainerCPU = cs
}()
if err := <-hostErr; err != nil {
return err
}
if err := <-containerErr; err != nil {
return err
}
// Host-level stats
ms, err := st.HostMemory(clistat.PrefixGibi)
if err != nil {
return err
}
sr.HostMemory = ms
home, err := os.UserHomeDir()
if err != nil {
return err
}
ds, err := st.Disk(clistat.PrefixGibi, home)
if err != nil {
return err
}
sr.Disk = ds
// Container-only stats.
if ok, err := clistat.IsContainerized(fs); err == nil && ok {
cs, err := st.ContainerCPU()
if err != nil {
return err
}
sr.ContainerCPU = cs
ms, err := st.ContainerMemory(clistat.PrefixGibi)
if err != nil {
return err
}
sr.ContainerMemory = ms
}
out, err := formatter.Format(inv.Context(), []statsRow{sr})
if err != nil {
return err
}
_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}
func (*RootCmd) statCPU(s *clistat.Statter, fs afero.Fs) *clibase.Cmd {
var hostArg bool
formatter := cliui.NewOutputFormatter(cliui.TextFormat(), cliui.JSONFormat())
cmd := &clibase.Cmd{
Use: "cpu",
Short: "Show CPU usage, in cores.",
Options: clibase.OptionSet{
{
Flag: "host",
Value: clibase.BoolOf(&hostArg),
Description: "Force host CPU measurement.",
},
},
Handler: func(inv *clibase.Invocation) error {
var cs *clistat.Result
var err error
if ok, _ := clistat.IsContainerized(fs); ok && !hostArg {
cs, err = s.ContainerCPU()
} else {
cs, err = s.HostCPU()
}
if err != nil {
return err
}
out, err := formatter.Format(inv.Context(), cs)
if err != nil {
return err
}
_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}
func (*RootCmd) statMem(s *clistat.Statter, fs afero.Fs) *clibase.Cmd {
var hostArg bool
var prefixArg string
formatter := cliui.NewOutputFormatter(cliui.TextFormat(), cliui.JSONFormat())
cmd := &clibase.Cmd{
Use: "mem",
Short: "Show memory usage, in gigabytes.",
Options: clibase.OptionSet{
{
Flag: "host",
Value: clibase.BoolOf(&hostArg),
Description: "Force host memory measurement.",
},
{
Description: "SI Prefix for memory measurement.",
Default: clistat.PrefixHumanGibi,
Flag: "prefix",
Value: clibase.EnumOf(&prefixArg,
clistat.PrefixHumanKibi,
clistat.PrefixHumanMebi,
clistat.PrefixHumanGibi,
clistat.PrefixHumanTebi,
),
},
},
Handler: func(inv *clibase.Invocation) error {
pfx := clistat.ParsePrefix(prefixArg)
var ms *clistat.Result
var err error
if ok, _ := clistat.IsContainerized(fs); ok && !hostArg {
ms, err = s.ContainerMemory(pfx)
} else {
ms, err = s.HostMemory(pfx)
}
if err != nil {
return err
}
out, err := formatter.Format(inv.Context(), ms)
if err != nil {
return err
}
_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}
func (*RootCmd) statDisk(s *clistat.Statter) *clibase.Cmd {
var pathArg string
var prefixArg string
formatter := cliui.NewOutputFormatter(cliui.TextFormat(), cliui.JSONFormat())
cmd := &clibase.Cmd{
Use: "disk",
Short: "Show disk usage, in gigabytes.",
Options: clibase.OptionSet{
{
Flag: "path",
Value: clibase.StringOf(&pathArg),
Description: "Path for which to check disk usage.",
Default: "/",
},
{
Flag: "prefix",
Default: clistat.PrefixHumanGibi,
Description: "SI Prefix for disk measurement.",
Value: clibase.EnumOf(&prefixArg,
clistat.PrefixHumanKibi,
clistat.PrefixHumanMebi,
clistat.PrefixHumanGibi,
clistat.PrefixHumanTebi,
),
},
},
Handler: func(inv *clibase.Invocation) error {
pfx := clistat.ParsePrefix(prefixArg)
ds, err := s.Disk(pfx, pathArg)
if err != nil {
return err
}
out, err := formatter.Format(inv.Context(), ds)
if err != nil {
return err
}
_, err = fmt.Fprintln(inv.Stdout, out)
return err
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}
type statsRow struct {
HostCPU *clistat.Result `json:"host_cpu" table:"host_cpu,default_sort"`
HostMemory *clistat.Result `json:"host_memory" table:"host_memory"`
Disk *clistat.Result `json:"home_disk" table:"home_disk"`
ContainerCPU *clistat.Result `json:"container_cpu" table:"container_cpu"`
ContainerMemory *clistat.Result `json:"container_memory" table:"container_memory"`
}