/
get.go
258 lines (224 loc) · 6.57 KB
/
get.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
package commands
import (
"encoding/json"
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/messagebird/mysql-monitor/internal/data"
"log"
"net/http"
"os"
"strconv"
)
func BuildGetCommand() cli.Command {
return cli.Command{
Name: "get",
Aliases: []string{"g"},
Usage: "view the gathered data",
Flags: []cli.Flag{
cli.StringFlag{
Name: "api-url, u",
Usage: "the url to where the mysql monitor api is hosted",
Value: "http://localhost:9090",
},
},
Subcommands: cli.Commands{
cli.Command{
Name: "cycles",
Aliases: []string{"c"},
Description: "get the available cycles",
Action: getCycles,
},
cli.Command{
Name: "mysql",
Aliases: []string{"m"},
Subcommands: cli.Commands{
cli.Command{
Name: "process-list",
Usage: "ps {cycle_id}",
Aliases: []string{"pl"},
Action: getMysqlProcessList,
},
cli.Command{
Name: "engine-inno-db",
Usage: "eid {cycle_id}",
Aliases: []string{"eid"},
Action: getMysqlEngineInnoDb,
},
cli.Command{
Name: "slave-status",
Usage: "ss {cycle_id}",
Aliases: []string{"ss"},
Action: getMysqlSlaveStatus,
},
cli.Command{
Name: "threads",
Usage: "tr {cycle_id}",
Aliases: []string{"tr"},
Action: getMysqlThreads,
},
},
},
cli.Command{
Name: "system",
Aliases: []string{"s"},
Subcommands: cli.Commands{
cli.Command{
Name: "top",
Aliases: []string{"t"},
Action: getSystemTop,
},
cli.Command{
Name: "process-list",
Aliases: []string{"pl"},
Action: getSystemProcessList,
},
},
},
},
}
}
func getCycles(ctx *cli.Context) {
res, err := http.Get(fmt.Sprintf("%s/api/v1/cycles", ctx.Parent().String("u")))
if err != nil {
logrus.WithError(err).Error("could not get cycles")
return
}
var b []data.Cycle
err = json.NewDecoder(res.Body).Decode(&b)
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Id", "Ran at", "Took"})
table.SetColWidth(40)
for _, e := range b {
table.Append([]string{e.ID.String(), e.RanAt.String(), e.Took})
}
table.Render()
}
func getMysqlProcessList(ctx *cli.Context) {
if ctx.Args().Get(0) == "" || uuid.FromStringOrNil(ctx.Args().Get(0)) == uuid.Nil {
logrus.Fatalf("%q is not a valid cycle id", ctx.Args().Get(0))
}
res, err := http.Get(fmt.Sprintf("%s/api/v1/cycles/%s/mysql/process-list", ctx.Parent().Parent().String("u"), ctx.Args().Get(0)))
if err != nil {
logrus.WithError(err).Error("could not mysql process list")
return
}
var b []data.MysqlProcessList
err = json.NewDecoder(res.Body).Decode(&b)
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Id", "user", "host", "db", "command", "time", "state", "info", "rows sent", "rows examined"})
table.SetRowLine(true)
for _, e := range b {
table.Append([]string{strconv.Itoa(int(e.ID)), e.User, e.Host, e.DB.String, e.Command, strconv.Itoa(int(e.Time)), e.State.String, e.Info.String, strconv.Itoa(int(e.RowsSent)), strconv.Itoa(int(e.RowsExamined))})
}
table.Render()
}
func getMysqlEngineInnoDb(ctx *cli.Context) {
if ctx.Args().Get(0) == "" || uuid.FromStringOrNil(ctx.Args().Get(0)) == uuid.Nil {
logrus.Fatalf("%q is not a valid cycle id", ctx.Args().Get(0))
}
res, err := http.Get(fmt.Sprintf("%s/api/v1/cycles/%s/mysql/engine-innodb-status", ctx.Parent().Parent().String("u"), ctx.Args().Get(0)))
if err != nil {
logrus.WithError(err).Error("could not mysql engine inno db")
return
}
var b []data.EngineINNODBStatus
err = json.NewDecoder(res.Body).Decode(&b)
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
for _, e := range b {
log.SetFlags(0)
log.Println(e.Status)
}
}
func getMysqlSlaveStatus(ctx *cli.Context) {
if ctx.Args().Get(0) == "" || uuid.FromStringOrNil(ctx.Args().Get(0)) == uuid.Nil {
logrus.Fatalf("%q is not a valid cycle id", ctx.Args().Get(0))
}
res, err := http.Get(fmt.Sprintf("%s/api/v1/cycles/%s/mysql/slave-status", ctx.Parent().Parent().String("u"), ctx.Args().Get(0)))
if err != nil {
logrus.WithError(err).Error("could not mysql engine inno db")
return
}
var b []data.SlaveStatus
err = json.NewDecoder(res.Body).Decode(&b)
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
err = json.NewEncoder(os.Stdout).Encode(&b)
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
}
func getMysqlThreads(ctx *cli.Context) {
if ctx.Args().Get(0) == "" || uuid.FromStringOrNil(ctx.Args().Get(0)) == uuid.Nil {
logrus.Fatalf("%q is not a valid cycle id", ctx.Args().Get(0))
}
res, err := http.Get(fmt.Sprintf("%s/api/v1/cycles/%s/mysql/threads", ctx.Parent().Parent().String("u"), ctx.Args().Get(0)))
if err != nil {
logrus.WithError(err).Error("could not mysql threads")
return
}
var b []data.Thread
err = json.NewDecoder(res.Body).Decode(&b)
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
err = json.NewEncoder(os.Stdout).Encode(&b)
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
}
func getSystemTop(ctx *cli.Context) {
if ctx.Args().Get(0) == "" || uuid.FromStringOrNil(ctx.Args().Get(0)) == uuid.Nil {
logrus.Fatalf("%q is not a valid cycle id", ctx.Args().Get(0))
}
res, err := http.Get(fmt.Sprintf("%s/api/v1/cycles/%s/system/top", ctx.Parent().Parent().String("u"), ctx.Args().Get(0)))
if err != nil {
logrus.WithError(err).Error("could not get system top")
return
}
var b []data.Top
err = json.NewDecoder(res.Body).Decode(&b)
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
for _, e := range b {
log.SetFlags(0)
log.Println(e.Text)
}
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
}
func getSystemProcessList(ctx *cli.Context) {
if ctx.Args().Get(0) == "" || uuid.FromStringOrNil(ctx.Args().Get(0)) == uuid.Nil {
logrus.Fatalf("%q is not a valid cycle id", ctx.Args().Get(0))
}
res, err := http.Get(fmt.Sprintf("%s/api/v1/cycles/%s/system/process-list", ctx.Parent().Parent().String("u"), ctx.Args().Get(0)))
if err != nil {
logrus.WithError(err).Error("could not get system process list")
return
}
var b []data.UnixProcessList
err = json.NewDecoder(res.Body).Decode(&b)
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
for _, e := range b {
log.SetFlags(0)
log.Println(e.Text)
}
if err != nil {
logrus.WithError(err).Fatal(err.Error())
}
}