From 48e9183957fce660dcc32a36bbee6f3ee4a9d0e9 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Fri, 15 Jul 2022 02:20:55 -0700 Subject: [PATCH] Move mc admin top scanner -> mc admin scanner info (#4151) Also, add `mc admin scanner trace` which gives a simplified trace with only relevant options. --- cmd/admin-main.go | 1 + ...n-top-scanner.go => admin-scanner-info.go} | 18 +-- cmd/admin-scanner-trace.go | 136 ++++++++++++++++++ cmd/admin-scanner.go | 41 ++++++ cmd/admin-top-api.go | 2 +- cmd/admin-top.go | 1 - cmd/admin-trace.go | 5 +- cmd/auto-complete.go | 18 +-- 8 files changed, 200 insertions(+), 22 deletions(-) rename cmd/{admin-top-scanner.go => admin-scanner-info.go} (96%) create mode 100644 cmd/admin-scanner-trace.go create mode 100644 cmd/admin-scanner.go diff --git a/cmd/admin-main.go b/cmd/admin-main.go index e0741985..2ae898f5 100644 --- a/cmd/admin-main.go +++ b/cmd/admin-main.go @@ -49,6 +49,7 @@ var adminCmdSubcommands = []cli.Command{ adminTierCmd, adminSpeedtestCmd, adminProfileCmd, + adminScannerCmd, adminTopCmd, adminTraceCmd, adminConsoleCmd, diff --git a/cmd/admin-top-scanner.go b/cmd/admin-scanner-info.go similarity index 96% rename from cmd/admin-top-scanner.go rename to cmd/admin-scanner-info.go index da256226..4cb2bb6b 100644 --- a/cmd/admin-top-scanner.go +++ b/cmd/admin-scanner-info.go @@ -39,7 +39,7 @@ import ( "github.com/olekukonko/tablewriter" ) -var adminTopScannerFlags = []cli.Flag{ +var adminScannerInfoFlags = []cli.Flag{ cli.StringFlag{ Name: "nodes", Usage: "show only on matching servers, comma separate multiple", @@ -61,13 +61,13 @@ var adminTopScannerFlags = []cli.Flag{ }, } -var adminTopScannerCmd = cli.Command{ - Name: "scanner", +var adminScannerInfo = cli.Command{ + Name: "info", Usage: "summarize scanner events on MinIO server in real-time", - Action: mainAdminTopScanner, + Action: mainAdminScannerInfo, OnUsageError: onUsageError, Before: setGlobalsFromContext, - Flags: append(adminTopScannerFlags, globalFlags...), + Flags: append(adminScannerInfoFlags, globalFlags...), HideHelpCommand: true, CustomHelpTemplate: `NAME: {{.HelpName}} - {{.Usage}} @@ -85,14 +85,14 @@ EXAMPLES: } // checkAdminTopAPISyntax - validate all the passed arguments -func checkAdminTopScannerSyntax(ctx *cli.Context) { +func checkAdminScannerInfoSyntax(ctx *cli.Context) { if len(ctx.Args()) == 0 || len(ctx.Args()) > 1 { - cli.ShowCommandHelpAndExit(ctx, "scanner", 1) // last argument is exit code + cli.ShowCommandHelpAndExit(ctx, "info", 1) // last argument is exit code } } -func mainAdminTopScanner(ctx *cli.Context) error { - checkAdminTopScannerSyntax(ctx) +func mainAdminScannerInfo(ctx *cli.Context) error { + checkAdminScannerInfoSyntax(ctx) aliasedURL := ctx.Args().Get(0) diff --git a/cmd/admin-scanner-trace.go b/cmd/admin-scanner-trace.go new file mode 100644 index 00000000..c35cb51a --- /dev/null +++ b/cmd/admin-scanner-trace.go @@ -0,0 +1,136 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "context" + "fmt" + + "github.com/fatih/color" + "github.com/minio/cli" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +var adminScannerTraceFlags = []cli.Flag{ + cli.StringFlag{ + Name: "response-threshold", + Usage: "trace calls only with response duration greater than this threshold (e.g. `5ms`)", + }, + cli.StringSliceFlag{ + Name: "funcname", + Usage: "trace only matching func name (eg 'scanner.ScanObject')", + }, + cli.StringSliceFlag{ + Name: "node", + Usage: "trace only matching servers", + }, +} + +var adminScannerTraceCmd = cli.Command{ + Name: "trace", + Usage: "show trace for MinIO scanner operations", + Action: mainAdminScannerTrace, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(adminScannerTraceFlags, globalFlags...), + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Show scanner trace for MinIO server + {{.Prompt}} {{.HelpName}} myminio + + 2. Show scanner trace for a specific path + {{.Prompt}} {{.HelpName}} --path my-bucket/my-prefix/* myminio + + 3. Show trace for only ScanObject operations + {{.Prompt}} {{.HelpName}} --funcname=scanner.ScanObject myminio +`, +} + +func checkAdminScannerTraceSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "trace", 1) // last argument is exit code + } +} + +// mainAdminScannerTrace - the entry function of trace command +func mainAdminScannerTrace(ctx *cli.Context) error { + // Check for command syntax + checkAdminScannerTraceSyntax(ctx) + + verbose := ctx.Bool("verbose") + aliasedURL := ctx.Args().Get(0) + + console.SetColor("Stat", color.New(color.FgYellow)) + + console.SetColor("Request", color.New(color.FgCyan)) + console.SetColor("Method", color.New(color.Bold, color.FgWhite)) + console.SetColor("Host", color.New(color.Bold, color.FgGreen)) + console.SetColor("FuncName", color.New(color.Bold, color.FgGreen)) + + console.SetColor("ReqHeaderKey", color.New(color.Bold, color.FgWhite)) + console.SetColor("RespHeaderKey", color.New(color.Bold, color.FgCyan)) + console.SetColor("HeaderValue", color.New(color.FgWhite)) + console.SetColor("RespStatus", color.New(color.Bold, color.FgYellow)) + console.SetColor("ErrStatus", color.New(color.Bold, color.FgRed)) + + console.SetColor("Response", color.New(color.FgGreen)) + console.SetColor("Body", color.New(color.FgYellow)) + for _, c := range colors { + console.SetColor(fmt.Sprintf("Node%d", c), color.New(c)) + } + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + if err != nil { + fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.") + return nil + } + + ctxt, cancel := context.WithCancel(globalContext) + defer cancel() + + opts, e := tracingOpts(ctx, []string{"scanner"}) + fatalIf(probe.NewError(e), "Unable to start tracing") + + mopts := matchOpts{ + funcNames: ctx.StringSlice("funcname"), + apiPaths: ctx.StringSlice("path"), + nodes: ctx.StringSlice("node"), + } + + // Start listening on all trace activity. + traceCh := client.ServiceTrace(ctxt, opts) + for traceInfo := range traceCh { + if traceInfo.Err != nil { + fatalIf(probe.NewError(traceInfo.Err), "Unable to listen to http trace") + } + if matchTrace(mopts, traceInfo) { + printTrace(verbose, traceInfo) + } + } + return nil +} diff --git a/cmd/admin-scanner.go b/cmd/admin-scanner.go new file mode 100644 index 00000000..ad95ff95 --- /dev/null +++ b/cmd/admin-scanner.go @@ -0,0 +1,41 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import "github.com/minio/cli" + +var adminScannerSubcommands = []cli.Command{ + adminScannerInfo, + adminScannerTraceCmd, +} + +var adminScannerCmd = cli.Command{ + Name: "scanner", + Usage: "provide MinIO scanner info", + Action: mainAdminScanner, + Before: setGlobalsFromContext, + Flags: globalFlags, + Subcommands: adminScannerSubcommands, + HideHelpCommand: true, +} + +// mainAdminScanner is the handle for "mc admin scanner" command. +func mainAdminScanner(ctx *cli.Context) error { + commandNotFound(ctx, adminScannerSubcommands) + return nil +} diff --git a/cmd/admin-top-api.go b/cmd/admin-top-api.go index bfe77c76..85f2737e 100644 --- a/cmd/admin-top-api.go +++ b/cmd/admin-top-api.go @@ -94,7 +94,7 @@ func mainAdminTopAPI(ctx *cli.Context) error { ctxt, cancel := context.WithCancel(globalContext) defer cancel() - opts, e := tracingOpts(ctx) + opts, e := tracingOpts(ctx, ctx.StringSlice("call")) fatalIf(probe.NewError(e), "Unable to start tracing") mopts := matchOpts{ diff --git a/cmd/admin-top.go b/cmd/admin-top.go index 920b2413..8c036877 100644 --- a/cmd/admin-top.go +++ b/cmd/admin-top.go @@ -22,7 +22,6 @@ import "github.com/minio/cli" var adminTopSubcommands = []cli.Command{ adminTopAPICmd, adminTopLocksCmd, - adminTopScannerCmd, } var adminTopCmd = cli.Command{ diff --git a/cmd/admin-trace.go b/cmd/admin-trace.go index 44119482..71720e5e 100644 --- a/cmd/admin-trace.go +++ b/cmd/admin-trace.go @@ -217,7 +217,7 @@ func matchTrace(opts matchOpts, traceInfo madmin.ServiceTraceInfo) bool { } // Calculate tracing options for command line flags -func tracingOpts(ctx *cli.Context) (opts madmin.ServiceTraceOpts, e error) { +func tracingOpts(ctx *cli.Context, apis []string) (opts madmin.ServiceTraceOpts, e error) { if t := ctx.String("response-threshold"); t != "" { d, e := time.ParseDuration(t) if e != nil { @@ -237,7 +237,6 @@ func tracingOpts(ctx *cli.Context) (opts madmin.ServiceTraceOpts, e error) { return } - apis := ctx.StringSlice("call") if len(apis) == 0 { // If api flag is not specified, then we will // trace only S3 requests by default. @@ -299,7 +298,7 @@ func mainAdminTrace(ctx *cli.Context) error { ctxt, cancel := context.WithCancel(globalContext) defer cancel() - opts, e := tracingOpts(ctx) + opts, e := tracingOpts(ctx, ctx.StringSlice("call")) fatalIf(probe.NewError(e), "Unable to start tracing") mopts := matchOpts{ diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 3d659c2a..3a1ff680 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -323,14 +323,16 @@ var completeCmds = map[string]complete.Predictor{ "/admin/decommission/status": aliasCompleter, "/admin/decommission/cancel": aliasCompleter, - "/admin/trace": aliasCompleter, - "/admin/speedtest": aliasCompleter, - "/admin/console": aliasCompleter, - "/admin/update": aliasCompleter, - "/admin/inspect": s3Completer, - "/admin/top/locks": aliasCompleter, - "/admin/top/api": aliasCompleter, - "/admin/top/scanner": aliasCompleter, + "/admin/trace": aliasCompleter, + "/admin/speedtest": aliasCompleter, + "/admin/console": aliasCompleter, + "/admin/update": aliasCompleter, + "/admin/inspect": s3Completer, + "/admin/top/locks": aliasCompleter, + "/admin/top/api": aliasCompleter, + + "/admin/scanner/info": aliasCompleter, + "/admin/scanner/trace": aliasCompleter, "/admin/service/stop": aliasCompleter, "/admin/service/restart": aliasCompleter,