/
processes.go
235 lines (191 loc) · 5.24 KB
/
processes.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package talos
import (
"context"
"fmt"
"path/filepath"
"sort"
"strings"
"time"
"github.com/dustin/go-humanize"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
"golang.org/x/term"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"
"github.com/siderolabs/talos/pkg/cli"
machineapi "github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/client"
)
var (
sortMethod string
watchProcesses bool
)
// processesCmd represents the processes command.
var processesCmd = &cobra.Command{
Use: "processes",
Aliases: []string{"p", "ps"},
Short: "List running processes",
Long: ``,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return WithClient(func(ctx context.Context, c *client.Client) error {
var err error
switch {
case watchProcesses:
if err = ui.Init(); err != nil {
return fmt.Errorf("failed to initialize termui: %w", err)
}
defer ui.Close()
processesUI(ctx, c)
default:
var output string
output, err = processesOutput(ctx, c)
if err != nil {
return err
}
// Note this is unlimited output of process lines
// we arent artificially limited by the box we would otherwise draw
fmt.Println(output)
}
return nil
})
},
}
func init() {
processesCmd.Flags().StringVarP(&sortMethod, "sort", "s", "rss", "Column to sort output by. [rss|cpu]")
processesCmd.Flags().BoolVarP(&watchProcesses, "watch", "w", false, "Stream running processes")
addCommand(processesCmd)
}
func processesUI(ctx context.Context, c *client.Client) {
l := widgets.NewParagraph()
l.Border = false
l.WrapText = false
l.PaddingTop = 0
l.PaddingBottom = 0
var processOutput string
draw := func() {
// Attempt to get terminal dimensions
// Since we're getting this data on each call
// we'll be able to handle terminal window resizing
w, h, err := term.GetSize(0)
cli.Should(err)
// x, y, w, h
l.SetRect(0, 0, w, h)
l.WrapText = false
processOutput, err = processesOutput(ctx, c)
if err != nil {
l.Text = err.Error()
l.WrapText = true
ui.Render(l)
return
}
// Dont refresh if we dont have any output
if processOutput == "" {
return
}
// Truncate our output based on terminal size
l.Text = processOutput
ui.Render(l)
}
draw()
uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second).C
for {
select {
case <-ctx.Done():
return
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
case "r", "m":
sortMethod = "rss"
case "c":
sortMethod = "cpu"
}
case <-ticker:
draw()
}
}
}
type by func(p1, p2 *machineapi.ProcessInfo) bool
func (b by) sort(procs []*machineapi.ProcessInfo) {
ps := &procSorter{
procs: procs,
by: b, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Sort(ps)
}
type procSorter struct {
procs []*machineapi.ProcessInfo
by func(p1, p2 *machineapi.ProcessInfo) bool // Closure used in the Less method.
}
// Len is part of sort.Interface.
func (s *procSorter) Len() int {
return len(s.procs)
}
// Swap is part of sort.Interface.
func (s *procSorter) Swap(i, j int) {
s.procs[i], s.procs[j] = s.procs[j], s.procs[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *procSorter) Less(i, j int) bool {
return s.by(s.procs[i], s.procs[j])
}
// Sort Methods.
var rss = func(p1, p2 *machineapi.ProcessInfo) bool {
// Reverse sort ( Descending )
return p1.ResidentMemory > p2.ResidentMemory
}
var cpu = func(p1, p2 *machineapi.ProcessInfo) bool {
// Reverse sort ( Descending )
return p1.CpuTime > p2.CpuTime
}
func processesOutput(ctx context.Context, c *client.Client) (output string, err error) {
var remotePeer peer.Peer
resp, err := c.Processes(ctx, grpc.Peer(&remotePeer))
if err != nil {
return output, err
}
defaultNode := client.AddrFromPeer(&remotePeer)
s := []string{}
s = append(s, "NODE | PID | STATE | THREADS | CPU-TIME | VIRTMEM | RESMEM | COMMAND")
for _, msg := range resp.Messages {
procs := msg.Processes
switch sortMethod {
case "cpu":
by(cpu).sort(procs)
default:
by(rss).sort(procs)
}
var args string
for _, p := range procs {
switch {
case p.Executable == "":
args = p.Command
case p.Args != "" && strings.Fields(p.Args)[0] == filepath.Base(strings.Fields(p.Executable)[0]):
args = strings.Replace(p.Args, strings.Fields(p.Args)[0], p.Executable, 1)
default:
args = p.Args
}
node := defaultNode
if msg.Metadata != nil {
node = msg.Metadata.Hostname
}
s = append(s,
fmt.Sprintf("%12s | %6d | %1s | %4d | %8.2f | %7s | %7s | %s",
node, p.Pid, p.State, p.Threads, p.CpuTime, humanize.Bytes(p.VirtualMemory), humanize.Bytes(p.ResidentMemory), args))
}
}
res := columnize.SimpleFormat(s)
if err != nil {
return res, err
}
return res, helpers.CheckErrors(resp.Messages...)
}