Skip to content

Commit 4164db5

Browse files
committed
Add CPU command
1 parent ca4fa81 commit 4164db5

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (r *RootCmd) Core() []*clibase.Cmd {
106106
r.scaletest(),
107107
r.gitssh(),
108108
r.vscodeSSH(),
109+
r.stat(),
109110
}
110111
}
111112

cli/stat.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/shirou/gopsutil/cpu"
8+
9+
"github.com/coder/coder/cli/clibase"
10+
)
11+
12+
type statCmd struct {
13+
watch time.Duration
14+
}
15+
16+
func (*RootCmd) stat() *clibase.Cmd {
17+
c := &clibase.Cmd{
18+
Use: "stat <type> [flags...]",
19+
Short: "Display system resource usage statistics",
20+
Long: "stat can be used as the script for agent metadata blocks.",
21+
Hidden: true,
22+
}
23+
var statCmd statCmd
24+
c.Options.Add(
25+
clibase.Option{
26+
Flag: "watch",
27+
FlagShorthand: "w",
28+
Description: "Continuously display the statistic on the given interval.",
29+
Value: clibase.DurationOf(&statCmd.watch),
30+
},
31+
)
32+
c.AddSubcommands(
33+
statCmd.cpu(),
34+
)
35+
return c
36+
}
37+
38+
func (sc *statCmd) watchLoop(fn clibase.HandlerFunc) clibase.HandlerFunc {
39+
return func(inv *clibase.Invocation) error {
40+
if sc.watch == 0 {
41+
return fn(inv)
42+
}
43+
44+
ticker := time.NewTicker(sc.watch)
45+
defer ticker.Stop()
46+
47+
for range ticker.C {
48+
if err := fn(inv); err != nil {
49+
_, _ = fmt.Fprintf(inv.Stderr, "error: %v", err)
50+
}
51+
}
52+
panic("unreachable")
53+
}
54+
}
55+
56+
//nolint:revive
57+
func (sc *statCmd) cpu() *clibase.Cmd {
58+
var interval time.Duration
59+
c := &clibase.Cmd{
60+
Use: "cpu",
61+
Short: "Display the system's cpu usage",
62+
Long: "Display the system's load average.",
63+
Handler: sc.watchLoop(func(inv *clibase.Invocation) error {
64+
r, err := cpu.Percent(0, false)
65+
if err != nil {
66+
return err
67+
}
68+
_, _ = fmt.Fprintf(inv.Stdout, "%02.0f\n", r[0])
69+
return nil
70+
}),
71+
Options: []clibase.Option{
72+
{
73+
Flag: "interval",
74+
FlagShorthand: "i",
75+
Description: "The sample collection interval.",
76+
Default: "1s",
77+
Value: clibase.DurationOf(&interval),
78+
},
79+
},
80+
}
81+
return c
82+
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ require (
127127
github.com/prometheus/client_golang v1.14.0
128128
github.com/quasilyte/go-ruleguard/dsl v0.3.21
129129
github.com/robfig/cron/v3 v3.0.1
130+
github.com/shirou/gopsutil v3.21.11+incompatible
130131
github.com/spf13/afero v1.9.3
131132
github.com/spf13/pflag v1.0.5
132133
github.com/stretchr/testify v1.8.1
@@ -187,6 +188,9 @@ require (
187188
github.com/modern-go/reflect2 v1.0.2 // indirect
188189
github.com/muesli/cancelreader v0.2.2 // indirect
189190
github.com/swaggo/files/v2 v2.0.0 // indirect
191+
github.com/tklauser/go-sysconf v0.3.9 // indirect
192+
github.com/tklauser/numcpus v0.3.0 // indirect
193+
github.com/yusufpapurcu/wmi v1.2.2 // indirect
190194
golang.org/x/text v0.8.0 // indirect
191195
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230215201556-9c5414ab4bde // indirect
192196
)

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
bazil.org/fuse v0.0.0-20160811212531-371fbbdaa898/go.mod h1:Xbm+BRKSBEpa4q4hTSxohYNQpsxXPbPry4JJWOB3LB8=
33
bazil.org/fuse v0.0.0-20200407214033-5883e5a4b512/go.mod h1:FbcW6z/2VytnFDhZfumh8Ss8zxHE6qpMP5sHTRe0EaM=
44
bitbucket.org/creachadair/shell v0.0.6/go.mod h1:8Qqi/cYk7vPnsOePHroKXDJYmb5x7ENhtiFtfZq8K+M=
5-
cdr.dev/slog v1.4.2-0.20230228204227-60d22dceaf04 h1:d5MQ+iI2zk7t0HrHwBP9p7k2XfRsXnRclSe8Kpp3xOo=
6-
cdr.dev/slog v1.4.2-0.20230228204227-60d22dceaf04/go.mod h1:YPVZsUbRMaLaPgme0RzlPWlC7fI7YmDj/j/kZLuvICs=
75
cdr.dev/slog v1.4.2 h1:fIfiqASYQFJBZiASwL825atyzeA96NsqSxx2aL61P8I=
86
cdr.dev/slog v1.4.2/go.mod h1:0EkH+GkFNxizNR+GAXUEdUHanxUH5t9zqPILmPM/Vn8=
97
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
@@ -1717,6 +1715,8 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm
17171715
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
17181716
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
17191717
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c/go.mod h1:/PevMnwAxekIXwN8qQyfc5gl2NlkB3CQlkizAbOkeBs=
1718+
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
1719+
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
17201720
github.com/shirou/gopsutil/v3 v3.21.10/go.mod h1:t75NhzCZ/dYyPQjyQmrAYP6c8+LCdFANeBMdLPCNnew=
17211721
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
17221722
github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
@@ -1842,7 +1842,9 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:
18421842
github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8=
18431843
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
18441844
github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
1845+
github.com/tklauser/go-sysconf v0.3.9 h1:JeUVdAOWhhxVcU6Eqr/ATFHgXk/mmiItdKeJPev3vTo=
18451846
github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=
1847+
github.com/tklauser/numcpus v0.3.0 h1:ILuRUQBtssgnxw0XXIjKUC56fgnOrFoQQ/4+DeU2biQ=
18461848
github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcyE6boqnA8=
18471849
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
18481850
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@@ -1944,6 +1946,8 @@ github.com/yuin/goldmark v1.5.3 h1:3HUJmBFbQW9fhQOzMgseU134xfi6hU+mjWywx5Ty+/M=
19441946
github.com/yuin/goldmark v1.5.3/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
19451947
github.com/yuin/goldmark-emoji v1.0.1 h1:ctuWEyzGBwiucEqxzwe0SOYDXPAucOrE9NQC18Wa1os=
19461948
github.com/yuin/goldmark-emoji v1.0.1/go.mod h1:2w1E6FEWLcDQkoTE+7HU6QF1F6SLlNGjRIBbIZQFqkQ=
1949+
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
1950+
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
19471951
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
19481952
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=
19491953
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=

0 commit comments

Comments
 (0)