/
jfr.go
384 lines (337 loc) · 10.5 KB
/
jfr.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
/*
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"encoding/json"
"errors"
"fmt"
"github.com/oracle/coherence-cli/pkg/config"
"github.com/oracle/coherence-cli/pkg/fetcher"
"github.com/oracle/coherence-cli/pkg/utils"
"github.com/spf13/cobra"
"strings"
"time"
)
var (
NodeID string
duration int32
jfrRoleName string
jfrDumpFileName string
settingsFile = "default"
)
const (
jfrNameUse = "jfr name"
supplyJFR = "you must provide a JFR name"
)
// getJfrsCmd represents the get jfrs command.
var getJfrsCmd = &cobra.Command{
Use: "jfrs",
Short: "display Java Flight Recordings for a cluster",
Long: `The 'get jfrs' command displays the Java Flight Recordings for a cluster.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var (
dataFetcher fetcher.Fetcher
connection string
err error
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
return executeJFROperation(cmd, "", fetcher.GetJFRs, dataFetcher, "", connection)
},
}
// describeJfrCmd represents the describe jfr command.
var describeJfrCmd = &cobra.Command{
Use: jfrNameUse,
Short: "describe a Java Flight Recording (JFR)",
Long: `The 'describe jfr' command shows information related to a Java Flight Recording (JFR).`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, supplyJFR)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
dataFetcher fetcher.Fetcher
connection string
err error
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
return executeJFROperation(cmd, args[0], fetcher.CheckJFR, dataFetcher, "", connection)
},
}
// startJfrCmd represents the start jfr command.
var startJfrCmd = &cobra.Command{
Use: jfrNameUse,
Short: "start a Java Flight Recording (JFR) for all or selected members",
Long: `The 'start jfr' command starts a Java Flight Recording all or selected members.
You can specify either a node id or role. If you do not specify either, then the JFR will
be run for all members. The default duration is 60 seconds and you can specify a value
of 0 to make the recording continuous.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, supplyJFR)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
dataFetcher fetcher.Fetcher
connection string
err error
nodeIds []string
jfrName = args[0]
jfrType string
jfrMessage string
target = ""
data []byte
finalResult string
)
if duration < 0 {
return errors.New("duration must be greater or equal to zero")
}
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
cmd.Println(FormatCurrentCluster(connection))
// check for node id
if NodeID != "" {
jfrType = fetcher.JfrTypeNode
if !utils.IsValidInt(NodeID) {
return fmt.Errorf("invalid node id %s", NodeID)
}
nodeIds, err = GetNodeIds(dataFetcher)
if err != nil {
return err
}
if !utils.SliceContains(nodeIds, NodeID) {
return fmt.Errorf("node id %s does not exist on this cluster", NodeID)
}
jfrMessage = "node id " + NodeID
target = NodeID
} else if jfrRoleName != all {
jfrType = fetcher.JfrTypeRole
jfrMessage = "role " + jfrRoleName
target = jfrRoleName
} else {
// must be cluster wide
jfrType = fetcher.JfrTypeCluster
nodeIds, err = GetNodeIds(dataFetcher)
if err != nil {
return err
}
jfrMessage = fmt.Sprintf("all %d members", len(nodeIds))
}
// confirm the operation
if !confirmOperation(cmd, fmt.Sprintf("Are you sure you want to start a JFR named %s for %s of duration: %d seconds using settings file %s? (y/n) ",
jfrName, jfrMessage, duration, settingsFile)) {
return nil
}
data, err = dataFetcher.StartJFR(jfrName, outputDirectory, jfrType, target, duration, settingsFile)
if err != nil {
return err
}
finalResult, err = getJFResults(jfrType, data)
if err != nil {
return err
}
cmd.Println(finalResult)
return nil
},
}
// stopJfrCmd represents the start jfr command.
var stopJfrCmd = &cobra.Command{
Use: jfrNameUse,
Short: "stop a Java Flight Recording (JFR) for all or selected members",
Long: `The 'stop jfr' command stops a Java Flight Recording all or selected members.
You can specify either a node or leave the node blank to stop for all nodes.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, supplyJFR)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
dataFetcher fetcher.Fetcher
connection string
err error
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
return executeJFROperation(cmd, args[0], fetcher.StopJFR, dataFetcher, "", connection)
},
}
// dumpJfrCmd represents the dump jfr command.
var dumpJfrCmd = &cobra.Command{
Use: jfrNameUse,
Short: "dump a Java Flight Recording (JFR) for all or selected members",
Long: `The 'dump jfr' command dumps a Java Flight Recording all or selected members.
A JFR command mut be in progress for this to succeed.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, supplyJFR)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
dataFetcher fetcher.Fetcher
connection string
err error
)
// retrieve the current context or the value from "-c"
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
return executeJFROperation(cmd, args[0], fetcher.DumpJFR, dataFetcher, jfrDumpFileName, connection)
},
}
// executeJFROperation executes a jfrStop, jfrDump, jfrCheck or command.
func executeJFROperation(cmd *cobra.Command, jfrName, operation string, dataFetcher fetcher.Fetcher, filename, connection string) error {
var (
err error
NodeIds []string
jfrType string
jfrMessage string
target = ""
data []byte
finalResult string
)
// check for node id
if NodeID != "" {
jfrType = fetcher.JfrTypeNode
if !utils.IsValidInt(NodeID) {
return fmt.Errorf("invalid node id %s", NodeID)
}
NodeIds, err = GetNodeIds(dataFetcher)
if err != nil {
return err
}
if !utils.SliceContains(NodeIds, NodeID) {
return fmt.Errorf("node id %s does not exist on this cluster", NodeID)
}
jfrMessage = "node id " + NodeID
target = NodeID
} else {
// must be cluster wide
jfrType = fetcher.JfrTypeCluster
NodeIds, err = GetNodeIds(dataFetcher)
if err != nil {
return err
}
jfrMessage = fmt.Sprintf("all %d members", len(NodeIds))
}
if operation != fetcher.CheckJFR && !automaticallyConfirm {
// confirm the operation
if !confirmOperation(cmd, fmt.Sprintf("Are you sure you want to run %s on a JFR named %s for %s ? (y/n) ", operation, jfrName, jfrMessage)) {
return nil
}
}
// ensure watch enabled cannot be set for anything other than fetcher.GetJFRs
if operation != fetcher.GetJFRs && isWatchEnabled() {
watchEnabled = false
}
for {
if operation == fetcher.StopJFR {
data, err = dataFetcher.StopJFR(jfrName, jfrType, target)
} else if operation == fetcher.DumpJFR {
data, err = dataFetcher.DumpJFR(jfrName, jfrType, target, filename)
} else if operation == fetcher.CheckJFR {
data, err = dataFetcher.CheckJFR(jfrName, jfrType, target)
} else if operation == fetcher.GetJFRs {
data, err = dataFetcher.CheckJFR("", jfrType, "")
}
if err != nil {
return err
}
finalResult, err = getJFResults(jfrType, data)
if err != nil {
return err
}
// format the result
printWatchHeader(cmd)
cmd.Println(FormatCurrentCluster(connection))
cmd.Println(formatJFROutput(finalResult))
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// we are watching so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return nil
}
// getJFResults returns the JFR result.
func getJFResults(jfrType string, data []byte) (string, error) {
var (
status config.StatusValues
singleStatus config.SingleStatusValue
err error
)
if jfrType == fetcher.JfrTypeNode {
// single value
err = json.Unmarshal(data, &singleStatus)
if err != nil {
return "", err
}
return singleStatus.Status, nil
}
// multiple values
err = json.Unmarshal(data, &status)
if err != nil {
return "", err
}
finalResult := ""
for _, value := range status.Status {
finalResult += value
}
return formatJFROutput(finalResult), nil
}
// formatJFROutput formats the output of a JFR result.
func formatJFROutput(result string) string {
var (
finalResult = strings.Replace(strings.Replace(result, "\n\n", "\n", -1), "->", "->\n", -1)
sb strings.Builder
)
for _, line := range strings.Split(finalResult, "\n") {
if line == "" {
continue
}
if !strings.Contains(line, "->") {
sb.WriteString(" ")
}
sb.WriteString(line + "\n")
}
return sb.String()
}
func init() {
nodeIDDesc := "node id to target"
describeJfrCmd.Flags().StringVarP(&NodeID, "node", "n", "", nodeIDDesc)
startJfrCmd.Flags().StringVarP(&outputDirectory, "output-dir", "O", "", "directory on servers to output JFR's to")
_ = startJfrCmd.MarkFlagRequired("output-dir")
startJfrCmd.Flags().BoolVarP(&automaticallyConfirm, "yes", "y", false, confirmOptionMessage)
startJfrCmd.Flags().StringVarP(&jfrRoleName, "role", "r", all, "role name to target")
startJfrCmd.Flags().StringVarP(&settingsFile, "settings-file", "s", "default", "settings file to use, options are \"profile\" or a specific file")
startJfrCmd.Flags().StringVarP(&NodeID, "node", "n", "", nodeIDDesc)
startJfrCmd.Flags().Int32VarP(&duration, "duration", "D", 60, "duration for JFR in seconds. Use 0 for continuous")
stopJfrCmd.Flags().BoolVarP(&automaticallyConfirm, "yes", "y", false, confirmOptionMessage)
stopJfrCmd.Flags().StringVarP(&NodeID, "node", "n", "", nodeIDDesc)
dumpJfrCmd.Flags().BoolVarP(&automaticallyConfirm, "yes", "y", false, confirmOptionMessage)
dumpJfrCmd.Flags().StringVarP(&NodeID, "node", "n", "", nodeIDDesc)
dumpJfrCmd.Flags().StringVarP(&jfrDumpFileName, "filename", "f", "", "filename for jfr dump")
_ = dumpJfrCmd.MarkFlagRequired("filename")
}