-
Notifications
You must be signed in to change notification settings - Fork 32
/
commands.go
466 lines (398 loc) · 11.7 KB
/
commands.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"text/template"
"github.com/codegangsta/cli"
mkr "github.com/mackerelio/mackerel-client-go"
"github.com/mackerelio/mkr/logger"
)
// cli.Commad object list
var Commands = []cli.Command{
commandStatus,
commandHosts,
commandCreate,
commandUpdate,
commandThrow,
commandFetch,
commandRetire,
}
var commandStatus = cli.Command{
Name: "status",
Usage: "Show the host",
Description: `
Show the information of the host identified with <hostId>.
Request "GET /api/v0/hosts/<hostId>". See http://help-ja.mackerel.io/entry/spec/api/v0#host-get.
`,
Action: doStatus,
Flags: []cli.Flag{
cli.BoolFlag{Name: "verbose, v", Usage: "Verbose output mode"},
},
}
var commandHosts = cli.Command{
Name: "hosts",
Usage: "List hosts",
Description: `
List the information of the hosts refined by host name, service name, role name and/or status.
Request "GET /api/v0/hosts.json". See http://help-ja.mackerel.io/entry/spec/api/v0#hosts-list.
`,
Action: doHosts,
Flags: []cli.Flag{
cli.StringFlag{Name: "name, n", Value: "", Usage: "List hosts only matched with <name>"},
cli.StringFlag{Name: "service, s", Value: "", Usage: "List hosts only belongs to <service>"},
cli.StringSliceFlag{
Name: "role, r",
Value: &cli.StringSlice{},
Usage: "List hosts only belongs to <role>. Multiple choice allow. Required --service",
},
cli.StringSliceFlag{
Name: "status, st",
Value: &cli.StringSlice{},
Usage: "List hosts only matched <status>. Multiple choice allow.",
},
cli.StringFlag{Name: "format, f", Value: "", Usage: "Output format template"},
cli.BoolFlag{Name: "verbose, v", Usage: "Verbose output mode"},
},
}
var commandCreate = cli.Command{
Name: "create",
Usage: "Create a new host",
Description: `
Create a new host with staus and/or roleFullname.
Request "POST /api/v0/hosts". See http://help-ja.mackerel.io/entry/spec/api/v0#host-create.
`,
Action: doCreate,
Flags: []cli.Flag{
cli.StringFlag{Name: "status, st", Value: "", Usage: "Host status ('working', 'standby', 'meintenance')"},
cli.StringSliceFlag{
Name: "roleFullname, R",
Value: &cli.StringSlice{},
Usage: "Multiple choice allow. ex. My-Service:proxy, My-Service:db-master",
},
},
}
var commandUpdate = cli.Command{
Name: "update",
Usage: "Update the host",
Description: `
Update the hosts identified with <hostIds>.
Request "PUT /api/v0/hosts/<hostId>". See http://help-ja.mackerel.io/entry/spec/api/v0#host-update.
`,
Action: doUpdate,
Flags: []cli.Flag{
cli.StringFlag{Name: "name, n", Value: "", Usage: "Update hostname."},
cli.StringFlag{Name: "status, st", Value: "", Usage: "Update status."},
cli.StringSliceFlag{
Name: "roleFullname, R",
Value: &cli.StringSlice{},
Usage: "Update rolefullname.",
},
},
}
var commandThrow = cli.Command{
Name: "throw",
Usage: "Post metric values",
Description: `
Post metric values to 'host metric' or 'service metric'.
Output format of metric value is compatibled with that of Sensu plugin.
Request "POST /api/v0/tsdb". See http://help-ja.mackerel.io/entry/spec/api/v0#metric-value-post.
`,
Action: doThrow,
Flags: []cli.Flag{
cli.StringFlag{Name: "host, H", Value: "", Usage: "Post host metric values to <hostID>."},
cli.StringFlag{Name: "service, s", Value: "", Usage: "Post service metric values to <service>."},
},
}
var commandFetch = cli.Command{
Name: "fetch",
Usage: "Fetch latest metric values",
Description: `
Fetch latest metric values about the hosts.
Request "GET /api/v0/tsdb/latest". See http://help-ja.mackerel.io/entry/spec/api/v0#tsdb-latest.
`,
Action: doFetch,
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "name, n",
Value: &cli.StringSlice{},
Usage: "Fetch metric values identified with <name>. Required. Multiple choise allow. ",
},
},
}
var commandRetire = cli.Command{
Name: "retire",
Usage: "Retire hosts",
Description: `
Retire host identified by hostIds. Be careful because this is a irreversible operation.
Request POST /api/v0/hosts/<hostId>/retire parallelly. See http://help-ja.mackerel.io/entry/spec/api/v0#host-retire.
`,
Action: doRetire,
}
func debug(v ...interface{}) {
if os.Getenv("DEBUG") != "" {
log.Println(v...)
}
}
func assert(err error) {
if err != nil {
log.Fatal(err)
}
}
func newMackerel() *mkr.Client {
apiKey := LoadApikeyFromEnvOrConfig()
if apiKey == "" {
logger.Log("error", `
Not set MACKEREL_APIKEY environment variable. (Try "export MACKEREL_APIKEY='<Your apikey>'")
`)
os.Exit(1)
}
if os.Getenv("DEBUG") != "" {
mackerel, err := mkr.NewClientWithOptions(apiKey, "https://mackerel.io/api/v0", true)
logger.DieIf(err)
return mackerel
}
return mkr.NewClient(apiKey)
}
type commandDoc struct {
Parent string
Arguments string
}
var commandDocs = map[string]commandDoc{
"status": {"", "[-v|verbose] <hostId>"},
"hosts": {"", "[--verbose | -v] [--name | -n <name>] [--service | -s <service>] [[--role | -r <role>]...] [[--status | --st <status>]...]"},
"create": {"", "[--status | -st <status>] [--roleFullname | -R <service:role>] <hostName>"},
"update": {"", "[--name | -n <name>] [--status | -st <status>] [--roleFullname | -R <service:role>] <hostIds...> ]"},
"throw": {"", "[--host | -h <hostId>] [--service | -s <service>] stdin"},
"fetch": {"", "[--name | -n <metricName>] hostIds..."},
"retire": {"", "hostIds..."},
}
// Makes template conditionals to generate per-command documents.
func mkCommandsTemplate(genTemplate func(commandDoc) string) string {
template := "{{if false}}"
for _, command := range append(Commands) {
template = template + fmt.Sprintf("{{else if (eq .Name %q)}}%s", command.Name, genTemplate(commandDocs[command.Name]))
}
return template + "{{end}}"
}
func init() {
argsTemplate := mkCommandsTemplate(func(doc commandDoc) string { return doc.Arguments })
parentTemplate := mkCommandsTemplate(func(doc commandDoc) string { return string(strings.TrimLeft(doc.Parent+" ", " ")) })
cli.CommandHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
mkr ` + parentTemplate + `{{.Name}} ` + argsTemplate + `
{{if (len .Description)}}
DESCRIPTION: {{.Description}}
{{end}}{{if (len .Flags)}}
OPTIONS:
{{range .Flags}}{{.}}
{{end}}
{{end}}`
}
func doStatus(c *cli.Context) {
argHostID := c.Args().Get(0)
isVerbose := c.Bool("verbose")
if argHostID == "" {
if argHostID = LoadHostIDFromConfig(); argHostID == "" {
cli.ShowCommandHelp(c, "status")
os.Exit(1)
}
}
host, err := newMackerel().FindHost(argHostID)
logger.DieIf(err)
if isVerbose {
PrettyPrintJSON(host)
} else {
format := &HostFormat{
ID: host.ID,
Name: host.Name,
Status: host.Status,
RoleFullnames: host.GetRoleFullnames(),
IsRetired: host.IsRetired,
CreatedAt: host.DateStringFromCreatedAt(),
IPAddresses: host.IPAddresses(),
}
PrettyPrintJSON(format)
}
}
func doHosts(c *cli.Context) {
isVerbose := c.Bool("verbose")
hosts, err := newMackerel().FindHosts(&mkr.FindHostsParam{
Name: c.String("name"),
Service: c.String("service"),
Roles: c.StringSlice("role"),
Statuses: c.StringSlice("status"),
})
logger.DieIf(err)
format := c.String("format")
if format != "" {
t := template.Must(template.New("format").Parse(format))
err := t.Execute(os.Stdout, hosts)
logger.DieIf(err)
} else if isVerbose {
PrettyPrintJSON(hosts)
} else {
var hostsFormat []*HostFormat
for _, host := range hosts {
format := &HostFormat{
ID: host.ID,
Name: host.Name,
Status: host.Status,
RoleFullnames: host.GetRoleFullnames(),
IsRetired: host.IsRetired,
CreatedAt: host.DateStringFromCreatedAt(),
IPAddresses: host.IPAddresses(),
}
hostsFormat = append(hostsFormat, format)
}
PrettyPrintJSON(hostsFormat)
}
}
func doCreate(c *cli.Context) {
argHostName := c.Args().Get(0)
optRoleFullnames := c.StringSlice("roleFullname")
optStatus := c.String("status")
if argHostName == "" {
cli.ShowCommandHelp(c, "create")
os.Exit(1)
}
client := newMackerel()
hostID, err := client.CreateHost(&mkr.CreateHostParam{
Name: argHostName,
RoleFullnames: optRoleFullnames,
})
logger.DieIf(err)
logger.Log("created", hostID)
if optStatus != "" {
err := client.UpdateHostStatus(hostID, optStatus)
logger.DieIf(err)
logger.Log("updated", fmt.Sprintf("%s %s", hostID, optStatus))
}
}
func doUpdate(c *cli.Context) {
argHostIDs := c.Args()
optName := c.String("name")
optStatus := c.String("status")
optRoleFullnames := c.StringSlice("roleFullname")
if len(argHostIDs) < 1 {
argHostIDs = make([]string, 1)
if argHostIDs[0] = LoadHostIDFromConfig(); argHostIDs[0] == "" {
cli.ShowCommandHelp(c, "update")
os.Exit(1)
}
}
needUpdateHostStatus := optStatus != ""
needUpdateHost := (optName != "" || len(optRoleFullnames) > 0)
if !needUpdateHostStatus && !needUpdateHost {
cli.ShowCommandHelp(c, "update")
os.Exit(1)
}
client := newMackerel()
var wg sync.WaitGroup
for _, hostID := range argHostIDs {
wg.Add(1)
go func(hostID string) {
defer wg.Done()
if needUpdateHostStatus {
err := client.UpdateHostStatus(hostID, optStatus)
logger.DieIf(err)
}
if needUpdateHost {
_, err := client.UpdateHost(hostID, &mkr.UpdateHostParam{
Name: optName,
RoleFullnames: optRoleFullnames,
})
logger.DieIf(err)
}
logger.Log("updated", hostID)
}(hostID)
}
wg.Wait()
}
func doThrow(c *cli.Context) {
optHostID := c.String("host")
optService := c.String("service")
var metricValues []*(mkr.MetricValue)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
// name, value, timestamp
// ex.) tcp.CLOSING 0 1397031808
items := strings.Fields(line)
if len(items) != 3 {
continue
}
value, err := strconv.ParseFloat(items[1], 64)
if err != nil {
logger.Log("warning", fmt.Sprintf("Failed to parse values: %s", err))
continue
}
time, err := strconv.ParseInt(items[2], 10, 64)
if err != nil {
logger.Log("warning", fmt.Sprintf("Failed to parse values: %s", err))
continue
}
metricValue := &mkr.MetricValue{
Name: items[0],
Value: value,
Time: time,
}
metricValues = append(metricValues, metricValue)
}
logger.ErrorIf(scanner.Err())
client := newMackerel()
if optHostID != "" {
err := client.PostHostMetricValuesByHostID(optHostID, metricValues)
logger.DieIf(err)
for _, metric := range metricValues {
logger.Log("thrown", fmt.Sprintf("%s '%s\t%f\t%d'", optHostID, metric.Name, metric.Value, metric.Time))
}
} else if optService != "" {
err := client.PostServiceMetricValues(optService, metricValues)
logger.DieIf(err)
for _, metric := range metricValues {
logger.Log("thrown", fmt.Sprintf("%s '%s\t%f\t%d'", optService, metric.Name, metric.Value, metric.Time))
}
} else {
cli.ShowCommandHelp(c, "throw")
os.Exit(1)
}
}
func doFetch(c *cli.Context) {
argHostIDs := c.Args()
optMetricNames := c.StringSlice("name")
if len(argHostIDs) < 1 || len(optMetricNames) < 1 {
cli.ShowCommandHelp(c, "fetch")
os.Exit(1)
}
metricValues, err := newMackerel().FetchLatestMetricValues(argHostIDs, optMetricNames)
logger.DieIf(err)
PrettyPrintJSON(metricValues)
}
func doRetire(c *cli.Context) {
argHostIDs := c.Args()
if len(argHostIDs) < 1 {
argHostIDs = make([]string, 1)
if argHostIDs[0] = LoadHostIDFromConfig(); argHostIDs[0] == "" {
cli.ShowCommandHelp(c, "retire")
os.Exit(1)
}
}
client := newMackerel()
var wg sync.WaitGroup
for _, hostID := range argHostIDs {
wg.Add(1)
go func(hostID string) {
defer wg.Done()
err := client.RetireHost(hostID)
logger.DieIf(err)
logger.Log("retired", hostID)
}(hostID)
}
wg.Wait()
}