/
main.go
504 lines (419 loc) · 9.73 KB
/
main.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package main
import (
"database/sql"
"flag"
"fmt"
"os"
"reflect"
"strconv"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/nsf/termbox-go"
"github.com/ttacon/innotop"
"golang.org/x/crypto/ssh/terminal"
)
var (
db *sql.DB
// flags
username = flag.String("u", "", "username")
host = flag.String("h", "localhost", "host to connect to")
password = flag.String("password", "", "password")
port = flag.Int("P", 3306, "port database server is bound to")
promptForPassword = flag.Bool("p", false, "prompt for password")
)
func main() {
flag.Parse()
passwd := *password
if *promptForPassword {
fmt.Print("password:")
passwdBytes, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
fmt.Println("failed to read password:", err)
os.Exit(1)
}
passwd = string(passwdBytes)
}
var err error
if db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/", *username, passwd, *host, *port)); err != nil {
fmt.Println("failed to connect to db:", err)
os.Exit(1)
} else if err = db.Ping(); err != nil {
fmt.Println("failed to connect to db:", err)
os.Exit(1)
}
events := make(chan keyEvent)
if err = termbox.Init(); err != nil {
panic(err)
}
defer termbox.Close()
go keyListener(events)
monitor(db, events)
}
type mon func(*sql.DB, chan keyEvent) mon
func monitor(db *sql.DB, events chan keyEvent) {
var todo = monitorTxs
for {
if todo = todo(db, events); todo == nil {
break
}
}
}
type columnInfo struct {
name string
width int
}
// ID, State, Started, Mem, Op State, RowsLocked, ROnly, Query
var txColumns = []columnInfo{
columnInfo{"ID", 10},
columnInfo{"State", 14},
columnInfo{"Started", 20},
columnInfo{"Mem", 11},
columnInfo{"Op State", 10},
columnInfo{"RowsLocked", 11},
columnInfo{"ROnly", 6},
columnInfo{"Query", 15},
}
// CXN Cmd ID User Host DB Time Query
var qList = []columnInfo{
columnInfo{"ID", 11},
columnInfo{"Cmd", 7},
columnInfo{"CXN", 11},
columnInfo{"User", 8},
columnInfo{"Host", 16},
columnInfo{"DB", 8},
columnInfo{"Time", 8},
columnInfo{"Query", 30},
}
func drawTitles(typ keyEvent, row int) {
xCursor := 2
columns := txColumns
w, _ := termbox.Size()
for i := 0; i < w; i++ {
termbox.SetCell(i, 1, ' ', colWhite, colDef)
}
if typ == queryList {
columns = qList
}
for _, col := range columns {
colWidth := 0
for _, run := range col.name {
termbox.SetCell(
xCursor,
row,
run,
termbox.ColorWhite,
termbox.ColorDefault,
)
xCursor++
colWidth++
}
// after every column add padding
xCursor += (1 + (col.width - colWidth))
}
for i := 0; i < xCursor; i++ {
termbox.SetCell(i, row+1, '_', termbox.ColorWhite, termbox.ColorDefault)
}
termbox.Flush()
}
func keyListener(events chan keyEvent) {
for {
event := termbox.PollEvent()
if event.Key == termbox.KeyCtrlC {
go func() {
events <- quitEvent
}()
} else if event.Type == termbox.EventKey {
if event.Ch == 'Q' {
go func() {
events <- queryList
}()
} else if event.Ch == 'T' {
go func() {
events <- txList
}()
}
}
}
}
type keyEvent int
const (
quitEvent keyEvent = iota
queryList
txList
)
func monitorTxs(db *sql.DB, events chan keyEvent) mon {
drawTitles(txList, 1)
oldRowNum := 0
rowStart := 4 // incase we want to move stuff later?
ww, _ := termbox.Size()
ticker := time.Tick(time.Second)
for {
select {
case eve := <-events:
if eve == quitEvent {
return nil
} else if eve == queryList {
clearRows(rowStart, rowStart+oldRowNum, ww)
return monitorQueries
}
case <-ticker:
}
rows, err := db.Query("select * from information_schema.INNODB_TRX")
if err != nil {
fmt.Println("failed to query transactions:", err)
continue
}
rowNum := rowStart
clearRows(rowStart, rowStart+oldRowNum, ww)
colNames, err := rows.Columns()
if err != nil {
// well that sucks
// TODO(ttacon): make a way to report errors using termbox
// most likely "flash params" above chart/table
continue
}
fieldsFor := columnRetrieveFunction(colNames, innotop.InnoDBTransaction{})
// ID, State, Started, MemoryUsed, Op State, RowsLocked, ROnly, Query
for rows.Next() {
var i innotop.InnoDBTransaction
if err = rows.Scan(fieldsFor(&i)...); err != nil {
fmt.Println("failed to read transaction:", err)
break
}
// add the Transaction ID
cursor := 2
for _, run := range i.ID {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 10 {
break
}
}
cursor = 13
for _, run := range i.State {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 26 {
break
}
}
cursor = 28
for _, run := range string(i.Started) {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 49 {
break
}
}
cursor = 49
for _, run := range strconv.FormatUint(i.LockMemoryBytes, 10) {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 59 {
break
}
}
cursor = 61
for _, run := range i.OperationState.String {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 70 {
break
}
}
cursor = 72
for _, run := range strconv.FormatUint(i.RowsLocked, 10) {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 76 {
break
}
}
cursor = 84
isReadOnly := 'Y'
if i.IsReadOnly == 0 {
isReadOnly = 'N'
}
termbox.SetCell(cursor, rowNum, isReadOnly, colWhite, colDef)
cursor = 91
for _, run := range i.Query.String {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == ww-1 {
break
}
}
rowNum++
}
oldRowNum = rowNum - rowStart
rows.Close()
termbox.Flush()
}
return nil
}
func clearRows(i0, iN, width int) {
for i := i0; i < iN; i++ {
for x := 0; x < width; x++ {
termbox.SetCell(x, i, ' ', colWhite, colDef)
}
}
}
// term colors
var (
colWhite = termbox.ColorWhite
colDef = termbox.ColorDefault
)
func monitorQueries(db *sql.DB, events chan keyEvent) mon {
drawTitles(queryList, 1)
oldRowNum := 0
rowStart := 4 // incase we want to move stuff later?
ww, _ := termbox.Size()
ticker := time.Tick(time.Second)
for {
select {
case eve := <-events:
if eve == quitEvent {
return nil
} else if eve == txList {
clearRows(rowStart, rowStart+oldRowNum, ww)
return monitorTxs
}
case <-ticker:
}
rows, err := db.Query("select * from information_schema.PROCESSLIST")
if err != nil {
fmt.Println("failed to query transactions:", err)
continue
}
rowNum := rowStart
// clear old rows
clearRows(rowStart, rowStart+oldRowNum, ww)
colNames, err := rows.Columns()
if err != nil {
// well that sucks
// TODO(ttacon): make a way to report errors using termbox
// most likely "flash params" above chart/table
continue
}
fieldsFor := columnRetrieveFunction(colNames, innotop.ProcessInfo{})
// ID, State, Started, MemoryUsed
for rows.Next() {
var i innotop.ProcessInfo
if err = rows.Scan(fieldsFor(&i)...); err != nil {
fmt.Println("failed to read transaction:", err)
break
}
if i.Info.String == "select * from information_schema.PROCESSLIST" {
continue
}
// add the Transaction ID
cursor := 2
for _, run := range strconv.FormatInt(i.QueryID, 10) {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 12 {
break
}
}
cursor = 14
for _, run := range i.Command {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 20 {
break
}
}
cursor = 22
for _, run := range i.State.String {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 32 {
break
}
}
cursor = 34
for _, run := range i.User {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 31 {
break
}
}
cursor = 43
for _, run := range i.Host {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 58 {
break
}
}
cursor = 60
for _, run := range i.DB.String {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 67 {
break
}
}
cursor = 69
for _, run := range tim(i.Time) {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
if cursor == 77 {
break
}
}
cursor = 79
for _, run := range i.Info.String {
termbox.SetCell(cursor, rowNum, run, colWhite, colDef)
cursor++
}
rowNum++
}
oldRowNum = rowNum - rowStart
rows.Close()
termbox.Flush()
}
return nil
}
func tim(sec32 int32) string {
sec := int(sec32)
var seconds, mins, hours int
if sec > 0 {
seconds = sec % 60
sec = sec / 60
}
if sec > 0 {
mins = sec % 60
sec = sec / 60
}
if sec > 0 {
hours = sec % 60
}
return fmt.Sprintf("%0.2d:%0.2d:%0.2d", hours, mins, seconds)
}
func columnRetrieveFunction(cols []string, i interface{}) func(i interface{}) []interface{} {
// TODO(ttacon): make innotop structs not need reflection to get
// desired fields
typ := reflect.TypeOf(i)
var ordering = make([]int, len(cols))
for k, col := range cols {
for j := 0; j < typ.NumField(); j++ {
field := typ.Field(j)
if field.Tag.Get("mysql") == col {
ordering[k] = j
break
}
}
}
// THIS IS ONLY A PROTOTYPE FUNCTION
// TODO(ttacon): lots of cleanup...
return func(i interface{}) []interface{} {
val := reflect.ValueOf(i)
var toSet = make([]interface{}, len(ordering))
for j, fieldIndex := range ordering {
toSet[j] = val.Elem().Field(fieldIndex).Addr().Interface()
}
return toSet
}
}