forked from nats-io/nats-top
/
nats-top.go
447 lines (362 loc) · 11 KB
/
nats-top.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Copyright (c) 2015 NATS Messaging System
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"time"
ui "github.com/gizak/termui"
gnatsd "github.com/nats-io/gnatsd/server"
. "github.com/nats-io/nats-top/util"
)
const version = "0.1.0"
var (
host = flag.String("s", "127.0.0.1", "The nats server host.")
port = flag.Int("m", 8222, "The nats server monitoring port.")
conns = flag.Int("n", 1024, "Maximum number of connections to poll.")
delay = flag.Int("d", 1, "Refresh interval in seconds.")
sortBy = flag.String("sort", "cid", "Value for which to sort by the connections.")
showVersion = flag.Bool("v", false, "Show nats-top version.")
)
func usage() {
log.Fatalf("Usage: nats-top [-s server] [-m monitor_port] [-n num_connections] [-d delay_secs] [-sort by]\n")
}
func init() {
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
}
func main() {
if *showVersion {
log.Printf("nats-top v%s", version)
os.Exit(0)
}
opts := map[string]interface{}{}
opts["host"] = *host
opts["port"] = *port
opts["conns"] = *conns
opts["delay"] = *delay
if opts["host"] == nil || opts["port"] == nil {
log.Fatalf("Please specify the monitoring port for NATS.\n")
usage()
}
sortOpt := gnatsd.SortOpt(*sortBy)
switch sortOpt {
case SortByCid, SortBySubs, SortByPending, SortByOutMsgs, SortByInMsgs, SortByOutBytes, SortByInBytes:
opts["sort"] = sortOpt
default:
log.Printf("nats-top: not a valid option to sort by: %s\n", sortOpt)
usage()
}
err := ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
statsCh := make(chan *Stats)
shutdownCh := make(chan struct{})
go MonitorStats(opts, statsCh, shutdownCh)
StartUI(opts, statsCh, shutdownCh)
}
// clearScreen tries to ensure resetting original state of screen
func clearScreen() {
fmt.Print("\033[2J\033[1;1H\033[?25l")
}
func cleanExit() {
clearScreen()
ui.Close()
// Show cursor once again
fmt.Print("\033[?25h")
os.Exit(0)
}
func exitWithError() {
ui.Close()
os.Exit(1)
}
// generateParagraph takes an options map and latest Stats
// then returns a formatted paragraph ready to be rendered
func generateParagraph(
opts map[string]interface{},
stats *Stats,
) string {
// Snapshot current stats
cpu := stats.Varz.CPU
memVal := stats.Varz.Mem
uptime := stats.Varz.Uptime
numConns := stats.Connz.NumConns
inMsgsVal := stats.Varz.InMsgs
outMsgsVal := stats.Varz.OutMsgs
inBytesVal := stats.Varz.InBytes
outBytesVal := stats.Varz.OutBytes
slowConsumers := stats.Varz.SlowConsumers
var serverVersion string
if stats.Varz.Info != nil {
serverVersion = stats.Varz.Info.Version
}
mem := Psize(memVal)
inMsgs := Psize(inMsgsVal)
outMsgs := Psize(outMsgsVal)
inBytes := Psize(inBytesVal)
outBytes := Psize(outBytesVal)
inMsgsRate := stats.Rates.InMsgsRate
outMsgsRate := stats.Rates.OutMsgsRate
inBytesRate := Psize(int64(stats.Rates.InBytesRate))
outBytesRate := Psize(int64(stats.Rates.OutBytesRate))
info := "gnatsd version %s (uptime: %s)"
info += "\nServer:\n Load: CPU: %.1f%% Memory: %s Slow Consumers: %d\n"
info += " In: Msgs: %s Bytes: %s Msgs/Sec: %.1f Bytes/Sec: %s\n"
info += " Out: Msgs: %s Bytes: %s Msgs/Sec: %.1f Bytes/Sec: %s"
text := fmt.Sprintf(info, serverVersion, uptime,
cpu, mem, slowConsumers,
inMsgs, inBytes, inMsgsRate, inBytesRate,
outMsgs, outBytes, outMsgsRate, outBytesRate)
text += fmt.Sprintf("\n\nConnections: %d\n", numConns)
var displaySubs bool
if val, ok := opts["subs"]; ok {
displaySubs = val.(bool)
}
connHeader := " %-20s %-8s %-6s %-10s %-10s %-10s %-10s %-10s %-7s %-7s "
if displaySubs {
connHeader += "%13s"
}
connHeader += "\n"
var connRows string
var connValues string
if displaySubs {
connRows = fmt.Sprintf(connHeader, "HOST", "CID", "SUBS", "PENDING",
"MSGS_TO", "MSGS_FROM", "BYTES_TO", "BYTES_FROM",
"LANG", "VERSION", "SUBSCRIPTIONS")
} else {
connRows = fmt.Sprintf(connHeader, "HOST", "CID", "SUBS", "PENDING",
"MSGS_TO", "MSGS_FROM", "BYTES_TO", "BYTES_FROM",
"LANG", "VERSION")
}
text += connRows
connValues = " %-20s %-8d %-6d %-10s %-10s %-10s %-10s %-10s %-7s %-7s "
if displaySubs {
connValues += "%s"
}
connValues += "\n"
switch opts["sort"] {
case SortByCid:
sort.Sort(ByCid(stats.Connz.Conns))
case SortBySubs:
sort.Sort(sort.Reverse(BySubs(stats.Connz.Conns)))
case SortByPending:
sort.Sort(sort.Reverse(ByPending(stats.Connz.Conns)))
case SortByOutMsgs:
sort.Sort(sort.Reverse(ByMsgsTo(stats.Connz.Conns)))
case SortByInMsgs:
sort.Sort(sort.Reverse(ByMsgsFrom(stats.Connz.Conns)))
case SortByOutBytes:
sort.Sort(sort.Reverse(ByBytesTo(stats.Connz.Conns)))
case SortByInBytes:
sort.Sort(sort.Reverse(ByBytesFrom(stats.Connz.Conns)))
}
for _, conn := range stats.Connz.Conns {
host := fmt.Sprintf("%s:%d", conn.IP, conn.Port)
var connLine string
if displaySubs {
subs := strings.Join(conn.Subs, ", ")
connLine = fmt.Sprintf(connValues, host, conn.Cid, conn.NumSubs, Psize(int64(conn.Pending)),
Psize(conn.OutMsgs), Psize(conn.InMsgs), Psize(conn.OutBytes), Psize(conn.InBytes),
conn.Lang, conn.Version, subs)
} else {
connLine = fmt.Sprintf(connValues, host, conn.Cid, conn.NumSubs, Psize(int64(conn.Pending)),
Psize(conn.OutMsgs), Psize(conn.InMsgs), Psize(conn.OutBytes), Psize(conn.InBytes),
conn.Lang, conn.Version)
}
text += connLine
}
return text
}
type ViewMode int
const (
TopViewMode ViewMode = iota
HelpViewMode
)
// StartUI periodically refreshes the screen using recent data
func StartUI(
opts map[string]interface{},
statsCh chan *Stats,
shutdownCh chan struct{},
) {
cleanStats := &Stats{
Varz: &gnatsd.Varz{},
Connz: &gnatsd.Connz{},
Rates: &Rates{},
}
// Show empty values on first display
text := generateParagraph(opts, cleanStats)
par := ui.NewPar(text)
par.Height = ui.TermHeight()
par.Width = ui.TermWidth()
par.HasBorder = false
helpText := generateHelp()
helpPar := ui.NewPar(helpText)
helpPar.Height = ui.TermHeight()
helpPar.Width = ui.TermWidth()
helpPar.HasBorder = false
// Top like view
paraRow := ui.NewRow(ui.NewCol(ui.TermWidth(), 0, par))
// Help view
helpParaRow := ui.NewRow(ui.NewCol(ui.TermWidth(), 0, helpPar))
// Create grids that we'll be using to toggle what to render
topViewGrid := ui.NewGrid(paraRow)
helpViewGrid := ui.NewGrid(helpParaRow)
// Start with the topviewGrid by default
ui.Body.Rows = topViewGrid.Rows
ui.Body.Align()
// Used to toggle back to previous mode
viewMode := TopViewMode
// Used for pinging the IU to refresh the screen with new values
redraw := make(chan struct{})
update := func() {
for {
stats := <-statsCh
// Update top view text
text = generateParagraph(opts, stats)
par.Text = text
redraw <- struct{}{}
}
}
// Flags for capturing options
waitingSortOption := false
waitingLimitOption := false
displaySubscriptions := false
optionBuf := ""
refreshOptionHeader := func() {
// Need to mask what was typed before
clrline := "\033[1;1H\033[6;1H "
clrline += " "
for i := 0; i < len(optionBuf); i++ {
clrline += " "
}
fmt.Printf(clrline)
}
evt := ui.EventCh()
ui.Render(ui.Body)
go update()
for {
select {
case e := <-evt:
if waitingSortOption {
if e.Type == ui.EventKey && e.Key == ui.KeyEnter {
sortOpt := gnatsd.SortOpt(optionBuf)
switch sortOpt {
case SortByCid, SortBySubs, SortByPending, SortByOutMsgs, SortByInMsgs, SortByOutBytes, SortByInBytes:
opts["sort"] = sortOpt
default:
go func() {
// Has to be at least of the same length as sort by header
emptyPadding := " "
fmt.Printf("\033[1;1H\033[6;1Hinvalid order: %s%s", optionBuf, emptyPadding)
waitingSortOption = false
time.Sleep(1 * time.Second)
refreshOptionHeader()
optionBuf = ""
}()
continue
}
refreshOptionHeader()
waitingSortOption = false
optionBuf = ""
continue
}
// Handle backspace
if e.Type == ui.EventKey && len(optionBuf) > 0 && (e.Key == ui.KeyBackspace || e.Key == ui.KeyBackspace2) {
optionBuf = optionBuf[:len(optionBuf)-1]
refreshOptionHeader()
} else {
optionBuf += string(e.Ch)
}
fmt.Printf("\033[1;1H\033[6;1Hsort by [%s]: %s", opts["sort"], optionBuf)
}
if waitingLimitOption {
if e.Type == ui.EventKey && e.Key == ui.KeyEnter {
var n int
_, err := fmt.Sscanf(optionBuf, "%d", &n)
if err == nil {
opts["conns"] = n
}
waitingLimitOption = false
optionBuf = ""
refreshOptionHeader()
continue
}
// Handle backspace
if e.Type == ui.EventKey && len(optionBuf) > 0 && (e.Key == ui.KeyBackspace || e.Key == ui.KeyBackspace2) {
optionBuf = optionBuf[:len(optionBuf)-1]
refreshOptionHeader()
} else {
optionBuf += string(e.Ch)
}
fmt.Printf("\033[1;1H\033[6;1Hlimit [%d]: %s", opts["conns"], optionBuf)
}
if e.Type == ui.EventKey && (e.Ch == 'q' || e.Key == ui.KeyCtrlC) {
close(shutdownCh)
cleanExit()
}
if e.Type == ui.EventKey && e.Ch == 's' && !(waitingLimitOption || waitingSortOption) {
if displaySubscriptions {
displaySubscriptions = false
opts["subs"] = false
} else {
displaySubscriptions = true
opts["subs"] = true
}
}
if e.Type == ui.EventKey && viewMode == HelpViewMode {
ui.Body.Rows = topViewGrid.Rows
viewMode = TopViewMode
continue
}
if e.Type == ui.EventKey && e.Ch == 'o' && !waitingLimitOption && viewMode == TopViewMode {
fmt.Printf("\033[1;1H\033[6;1Hsort by [%s]:", opts["sort"])
waitingSortOption = true
}
if e.Type == ui.EventKey && e.Ch == 'n' && !waitingSortOption && viewMode == TopViewMode {
fmt.Printf("\033[1;1H\033[6;1Hlimit [%d]:", opts["conns"])
waitingLimitOption = true
}
if e.Type == ui.EventKey && (e.Ch == '?' || e.Ch == 'h') && !(waitingSortOption || waitingLimitOption) {
if viewMode == TopViewMode {
refreshOptionHeader()
optionBuf = ""
}
ui.Body.Rows = helpViewGrid.Rows
viewMode = HelpViewMode
waitingLimitOption = false
waitingSortOption = false
}
if e.Type == ui.EventResize {
ui.Body.Width = ui.TermWidth()
ui.Body.Align()
go func() { redraw <- struct{}{} }()
}
case <-redraw:
ui.Render(ui.Body)
}
}
}
func generateHelp() string {
text := `
Command Description
o<option> Set primary sort key to <option>.
Option can be one of: {cid|subs|pending|msgs_to|msgs_from|
bytes_to, bytes_from}
This can be set in the command line too with -sort flag.
n<limit> Set sample size of connections to request from the server.
This can be set in the command line as well via -n flag.
Note that if used in conjunction with sort, the server
would respect both options allowing queries like 'connection
with largest number of subscriptions': -n 1 -sort subs
s Toggle displaying connection subscriptions.
q Quit nats-top.
Press any key to continue...
`
return text
}