-
Notifications
You must be signed in to change notification settings - Fork 32
/
commands.go
328 lines (290 loc) · 9.16 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
package main
import (
"os"
"strings"
"time"
"github.com/Songmu/prompter"
"github.com/mackerelio/mackerel-client-go"
"github.com/mackerelio/mkr/channels"
"github.com/mackerelio/mkr/checks"
"github.com/mackerelio/mkr/format"
"github.com/mackerelio/mkr/hosts"
"github.com/mackerelio/mkr/logger"
"github.com/mackerelio/mkr/mackerelclient"
"github.com/mackerelio/mkr/org"
"github.com/mackerelio/mkr/plugin"
"github.com/mackerelio/mkr/services"
"github.com/mackerelio/mkr/wrap"
"github.com/urfave/cli"
)
// Commands cli.Command object list
var Commands = []cli.Command{
commandStatus,
hosts.CommandHosts,
hosts.CommandCreate,
commandUpdate,
commandThrow,
commandMetrics,
commandFetch,
commandRetire,
services.Command,
commandMonitors,
channels.Command,
commandAlerts,
commandDashboards,
commandAnnotations,
org.Command,
plugin.CommandPlugin,
checks.Command,
wrap.Command,
}
var commandStatus = cli.Command{
Name: "status",
Usage: "Show the host",
ArgsUsage: "[--verbose | -v] <hostId>",
Description: `
Show the information of the host identified with <hostId>.
Requests "GET /api/v0/hosts/<hostId>". See https://mackerel.io/api-docs/entry/hosts#get .
`,
Action: doStatus,
Flags: []cli.Flag{
cli.BoolFlag{Name: "verbose, v", Usage: "Verbose output mode"},
},
}
var commandUpdate = cli.Command{
Name: "update",
Usage: "Update the host",
ArgsUsage: "[--name | -n <name>] [--displayName <displayName>] [--status | -st <status>] [--roleFullname | -R <service:role>] [--overwriteRoles | -o] [<hostIds...>]",
Description: `
Update the host identified with <hostId>.
Requests "PUT /api/v0/hosts/<hostId>". See https://mackerel.io/api-docs/entry/hosts#update-information .
`,
Action: doUpdate,
Flags: []cli.Flag{
cli.StringFlag{Name: "name, n", Value: "", Usage: "Update hostname."},
cli.StringFlag{Name: "displayName", Value: "", Usage: "Update displayName."},
cli.StringFlag{Name: "status, st", Value: "", Usage: "Update status."},
cli.StringSliceFlag{
Name: "roleFullname, R",
Value: &cli.StringSlice{},
Usage: "Update rolefullname.",
},
cli.BoolFlag{Name: "overwriteRoles, o", Usage: "Overwrite roles instead of adding specified roles."},
},
}
var commandMetrics = cli.Command{
Name: "metrics",
Usage: "Fetch metric values",
ArgsUsage: "[--host | -H <hostId>] [--service | -s <service>] [--name | -n <metricName>] --from int --to int",
Description: `
Fetch metric values of 'host metric' or 'service metric'.
Requests "/api/v0/hosts/<hostId>/metrics" or "/api/v0/services/<serviceName>/tsdb".
See https://mackerel.io/api-docs/entry/host-metrics#get, https://mackerel.io/api-docs/entry/service-metrics#get.
`,
Action: doMetrics,
Flags: []cli.Flag{
cli.StringFlag{Name: "host, H", Value: "", Usage: "Fetch host metric values of <hostID>."},
cli.StringFlag{Name: "service, s", Value: "", Usage: "Fetch service metric values of <service>."},
cli.StringFlag{Name: "name, n", Value: "", Usage: "The name of the metric for which you want to obtain the metric."},
cli.Int64Flag{Name: "from", Usage: "The first of the period for which you want to obtain the metric. (epoch seconds)"},
cli.Int64Flag{Name: "to", Usage: "The end of the period for which you want to obtain the metric. (epoch seconds)"},
},
}
var commandFetch = cli.Command{
Name: "fetch",
Usage: "Fetch latest metric values",
ArgsUsage: "[--name | -n <metricName>] hostIds...",
Description: `
Fetch latest metric values about the hosts.
Requests "GET /api/v0/tsdb/latest". See https://mackerel.io/api-docs/entry/host-metrics#get-latest .
`,
Action: doFetch,
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "name, n",
Value: &cli.StringSlice{},
Usage: "Fetch metric values identified with <name>. Required. Multiple choices are allowed. ",
},
},
}
var commandRetire = cli.Command{
Name: "retire",
Usage: "Retire hosts",
ArgsUsage: "[--force] hostIds...",
Description: `
Retire host identified by <hostId>. Be careful because this is an irreversible operation.
Requests POST /api/v0/hosts/<hostId>/retire parallelly. See https://mackerel.io/api-docs/entry/hosts#retire .
`,
Action: doRetire,
Flags: []cli.Flag{
cli.BoolFlag{Name: "force", Usage: "Force retirement without confirmation."},
},
}
func doStatus(c *cli.Context) error {
confFile := c.GlobalString("conf")
argHostID := c.Args().Get(0)
isVerbose := c.Bool("verbose")
if argHostID == "" {
if argHostID = mackerelclient.LoadHostIDFromConfig(confFile); argHostID == "" {
cli.ShowCommandHelp(c, "status")
os.Exit(1)
}
}
host, err := mackerelclient.NewFromContext(c).FindHost(argHostID)
logger.DieIf(err)
if isVerbose {
format.PrettyPrintJSON(os.Stdout, host)
} else {
format.PrettyPrintJSON(os.Stdout, &format.Host{
ID: host.ID,
Name: host.Name,
DisplayName: host.DisplayName,
Status: host.Status,
RoleFullnames: host.GetRoleFullnames(),
IsRetired: host.IsRetired,
CreatedAt: format.ISO8601Extended(host.DateFromCreatedAt()),
IPAddresses: host.IPAddresses(),
})
}
return nil
}
func doUpdate(c *cli.Context) error {
confFile := c.GlobalString("conf")
argHostIDs := c.Args()
optName := c.String("name")
optDisplayName := c.String("displayName")
optStatus := c.String("status")
optRoleFullnames := c.StringSlice("roleFullname")
overwriteRoles := c.Bool("overwriteRoles")
if len(argHostIDs) < 1 {
argHostIDs = make([]string, 1)
if argHostIDs[0] = mackerelclient.LoadHostIDFromConfig(confFile); argHostIDs[0] == "" {
cli.ShowCommandHelp(c, "update")
os.Exit(1)
}
}
needUpdateHostStatus := optStatus != ""
needUpdateRolesInHostUpdate := !overwriteRoles && len(optRoleFullnames) > 0
needUpdateHost := (optName != "" || optDisplayName != "" || overwriteRoles || needUpdateRolesInHostUpdate)
if !needUpdateHostStatus && !needUpdateHost {
logger.Log("update", "at least one argumet is required.")
cli.ShowCommandHelp(c, "update")
os.Exit(1)
}
client := mackerelclient.NewFromContext(c)
for _, hostID := range argHostIDs {
if needUpdateHostStatus {
err := client.UpdateHostStatus(hostID, optStatus)
logger.DieIf(err)
}
if overwriteRoles {
err := client.UpdateHostRoleFullnames(hostID, optRoleFullnames)
logger.DieIf(err)
}
if needUpdateHost {
host, err := client.FindHost(hostID)
logger.DieIf(err)
name := ""
if optName == "" {
name = host.Name
} else {
name = optName
}
displayname := ""
if optDisplayName == "" {
displayname = host.DisplayName
} else {
displayname = optDisplayName
}
param := &mackerel.UpdateHostParam{
Name: name,
DisplayName: displayname,
Meta: host.Meta,
Interfaces: host.Interfaces,
}
if needUpdateRolesInHostUpdate {
param.RoleFullnames = optRoleFullnames
}
_, err = client.UpdateHost(hostID, param)
logger.DieIf(err)
}
logger.Log("updated", hostID)
}
return nil
}
func split(ids []string, count int) [][]string {
xs := make([][]string, 0, (len(ids)+count-1)/count)
for i, name := range ids {
if i%count == 0 {
xs = append(xs, []string{})
}
xs[len(xs)-1] = append(xs[len(xs)-1], name)
}
return xs
}
func doMetrics(c *cli.Context) error {
optHostID := c.String("host")
optService := c.String("service")
optMetricName := c.String("name")
from := c.Int64("from")
to := c.Int64("to")
if to == 0 {
to = time.Now().Unix()
}
client := mackerelclient.NewFromContext(c)
if optHostID != "" {
metricValue, err := client.FetchHostMetricValues(optHostID, optMetricName, from, to)
logger.DieIf(err)
format.PrettyPrintJSON(os.Stdout, metricValue)
} else if optService != "" {
metricValue, err := client.FetchServiceMetricValues(optService, optMetricName, from, to)
logger.DieIf(err)
format.PrettyPrintJSON(os.Stdout, metricValue)
} else {
cli.ShowCommandHelp(c, "metrics")
os.Exit(1)
}
return nil
}
func doFetch(c *cli.Context) error {
argHostIDs := c.Args()
optMetricNames := c.StringSlice("name")
if len(argHostIDs) < 1 || len(optMetricNames) < 1 {
cli.ShowCommandHelp(c, "fetch")
os.Exit(1)
}
allMetricValues := make(mackerel.LatestMetricValues)
// Fetches 100 hosts per one request (to avoid URL maximum length).
for _, hostIds := range split(argHostIDs, 100) {
metricValues, err := mackerelclient.NewFromContext(c).FetchLatestMetricValues(hostIds, optMetricNames)
logger.DieIf(err)
for key := range metricValues {
allMetricValues[key] = metricValues[key]
}
}
format.PrettyPrintJSON(os.Stdout, allMetricValues)
return nil
}
func doRetire(c *cli.Context) error {
confFile := c.GlobalString("conf")
force := c.Bool("force")
argHostIDs := c.Args()
if len(argHostIDs) < 1 {
argHostIDs = make([]string, 1)
if argHostIDs[0] = mackerelclient.LoadHostIDFromConfig(confFile); argHostIDs[0] == "" {
cli.ShowCommandHelp(c, "retire")
os.Exit(1)
}
}
if !force && !prompter.YN("Retire following hosts.\n "+strings.Join(argHostIDs, "\n ")+"\nAre you sure?", true) {
logger.Log("", "retirement is canceled.")
return nil
}
client := mackerelclient.NewFromContext(c)
for _, hostID := range argHostIDs {
err := client.RetireHost(hostID)
logger.DieIf(err)
logger.Log("retired", hostID)
}
return nil
}