Skip to content

Commit

Permalink
Fix support top net JSON (#4948)
Browse files Browse the repository at this point in the history
JSON output was completely mangled.
Add switch to limit the number of responses.
  • Loading branch information
klauspost authored May 31, 2024
1 parent 3774027 commit 5040e61
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions cmd/support-top-net.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ var supportTopNetFlags = []cli.Flag{
Usage: "interval between requests in seconds",
Value: 1,
},
cli.IntFlag{
Name: "n",
Usage: "number of requests to run before exiting. 0 for endless (default)",
Value: 0,
},
}

var supportTopNetCmd = cli.Command{
Expand Down Expand Up @@ -94,42 +99,42 @@ func mainSupportTopNet(ctx *cli.Context) error {
opts := madmin.MetricsOptions{
Type: madmin.MetricNet,
Interval: time.Duration(ctx.Int("interval")) * time.Second,
N: ctx.Int("n"),
ByHost: true,
Hosts: hosts,
}

if globalJSON {
e := client.Metrics(ctxt, opts, func(metrics madmin.RealtimeMetrics) {
printMsg(metricsMessage{RealtimeMetrics: metrics})
})
if e != nil && !errors.Is(e, context.Canceled) {
fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to fetch scanner metrics")
}
return nil
}
p := tea.NewProgram(initTopNetUI())
go func() {
if globalJSON {
e := client.Metrics(ctxt, opts, func(metrics madmin.RealtimeMetrics) {
printMsg(metricsMessage{RealtimeMetrics: metrics})
})
if e != nil && !errors.Is(e, context.Canceled) {
fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to fetch scanner metrics")
}
} else {
out := func(m madmin.RealtimeMetrics) {
for endPoint, metric := range m.ByHost {
if metric.Net != nil {
p.Send(topNetResult{
endPoint: endPoint,
stats: *metric.Net,
})
}
}
if len(m.Errors) != 0 && len(m.Hosts) != 0 {
out := func(m madmin.RealtimeMetrics) {
for endPoint, metric := range m.ByHost {
if metric.Net != nil {
p.Send(topNetResult{
endPoint: m.Hosts[0],
error: m.Errors[0],
endPoint: endPoint,
stats: *metric.Net,
})
}
}

e := client.Metrics(ctxt, opts, out)
if e != nil {
fatalIf(probe.NewError(e), "Unable to fetch top net events")
if len(m.Errors) != 0 && len(m.Hosts) != 0 {
p.Send(topNetResult{
endPoint: m.Hosts[0],
error: m.Errors[0],
})
}
}

e := client.Metrics(ctxt, opts, out)
if e != nil {
fatalIf(probe.NewError(e), "Unable to fetch top net events")
}
p.Quit()
}()

Expand Down

0 comments on commit 5040e61

Please sign in to comment.