Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions src/go/pt-mongodb-summary/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,68 @@ For better results, host must be a **mongos** server.
Options
-------

``-a``, ``--auth-db``
``-a``, ``--authenticationDatabase``
Specifies the database used to establish credentials and privileges
with a MongoDB server.
By default, the ``admin`` database is used.

``-c``, ``--no-version-check``
Disables checking the version of MongoDB before running the report.

``-f``, ``--output-format``
Specifies the report output format. Valid options are: ``text``, ``json``.
The default value is ``text``.

``-h``, ``--help``
Show help message and exit.

``--host``
Specifies the hostname or IP address of the MongoDB server to connect to.

``-i``, ``--running-ops-interval``
Interval in milliseconds to wait between samples of running operations.
Default: 1000 milliseconds.

``-l``, ``--log-level``
Specifies the logging level. Valid options: ``panic``, ``fatal``, ``error``,
``warn``, ``info``, ``debug``.
Default: ``error``.

``-p``, ``--password``
Specifies the password to use when connecting to a server
with authentication enabled.

Do not add a space between the option and its value: ``-p<password>``.

If you specify the option without any value,
``pt-mongodb-summary`` will ask for password interactively.
``pt-mongodb-summary`` will ask for the password interactively.

``--port``
Specifies the port of the MongoDB server to connect to.

``-u``, ``--user``
Specifies the user name for connecting to a server
``-s``, ``--running-ops-samples``
Number of samples to collect for running operations.
Default: 5.

``--sslCAFile``
Path to the SSL CA certificate file used for authentication.

``--sslPEMKeyFile``
Path to the SSL client PEM file used for authentication.

``--uri``
Full MongoDB URI describing hosts and options.
Command-line flags have higher priority than URI settings.
If a full URI is provided, you cannot also specify ``--host`` or ``--port``.
Comment thread
svetasmirnova marked this conversation as resolved.
Example: ``mongodb://admin:secret@localhost:27017``

``-u``, ``--username``
Specifies the username to use when connecting to a server
with authentication enabled.

``-v``, ``--version``
Show version information and exit.

Output example
==============

Expand Down
75 changes: 56 additions & 19 deletions src/go/pt-mongodb-summary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const (
toolname = "pt-mongodb-summary"

DefaultAuthDB = "admin"
DefaultHost = "mongodb://localhost:27017"
DefaultHost = "localhost"
DefaultPort = "27017"
DefaultLogLevel = "warn"
DefaultRunningOpsInterval = 1000 // milliseconds
DefaultRunningOpsSamples = 5
Expand Down Expand Up @@ -161,6 +162,7 @@ type clusterwideInfo struct {

type cliOptions struct {
Host string
Port string
User string
Password string
AuthDB string
Expand All @@ -170,6 +172,7 @@ type cliOptions struct {
SSLPEMKeyFile string
RunningOpsSamples int
RunningOpsInterval int
URI string
Help bool
Comment thread
svetasmirnova marked this conversation as resolved.
Version bool
NoVersionCheck bool
Expand Down Expand Up @@ -252,13 +255,11 @@ func main() {
log.Errorf("Cannot get hostnames: %s", err)
}

log.Debugf("hostnames: %v", hostnames)

ci := &collectedInfo{}

ci.HostInfo, err = getHostInfo(ctx, client)
if err != nil {
log.Errorf("Cannot get host info for %q: %s", opts.Host, err)
log.Errorf("Cannot get host info for %q: %s", clientOptions.Hosts, err)
os.Exit(cannotGetHostInfo) //nolint:gocritic
}

Expand Down Expand Up @@ -920,7 +921,6 @@ func externalIP() (string, error) {

func parseFlags() (*cliOptions, error) {
opts := &cliOptions{
Host: DefaultHost,
LogLevel: DefaultLogLevel,
RunningOpsSamples: DefaultRunningOpsSamples,
RunningOpsInterval: DefaultRunningOpsInterval, // milliseconds
Expand All @@ -933,6 +933,10 @@ func parseFlags() (*cliOptions, error) {
gop.BoolVarLong(&opts.Version, "version", 'v', "", "Show version & exit")
gop.BoolVarLong(&opts.NoVersionCheck, "no-version-check", 'c', "", "Default: Don't check for updates")

gop.StringVarLong(&opts.URI, "uri", 0, `URI describes the hosts to be used and options. Flags has higher priority. If a full URI is provided, you cannot also specify "--host" or "--port".`)
gop.StringVarLong(&opts.Host, "host", 0, "Host")
gop.StringVarLong(&opts.Port, "port", 0, "Port")

gop.StringVarLong(&opts.User, "username", 'u', "", "Username to use for optional MongoDB authentication")
gop.StringVarLong(&opts.Password, "password", 'p', "", "Password to use for optional MongoDB authentication").
SetOptional()
Expand All @@ -958,7 +962,14 @@ func parseFlags() (*cliOptions, error) {
gop.Parse(os.Args)

if gop.NArgs() > 0 {
opts.Host = gop.Arg(0)
if gop.IsSet("host") || gop.IsSet("port") || gop.IsSet("uri") {
return nil, fmt.Errorf(`parameter host[:port] is not compatible with "--uri", "--host" and "--port" flags set`)
}
var err error
opts.Host, opts.Port, err = net.SplitHostPort(gop.Arg(0))
if err != nil {
return nil, err
}
gop.Parse(gop.Args())
}

Expand All @@ -973,8 +984,8 @@ func parseFlags() (*cliOptions, error) {
opts.Password = string(pass)
}

if !strings.HasPrefix(opts.Host, "mongodb://") {
opts.Host = "mongodb://" + opts.Host
if gop.IsSet("uri") && (gop.IsSet("host") || gop.IsSet("port")) {
return nil, fmt.Errorf("If a full URI is provided, you cannot also specify --host or --port")
}

if opts.Help {
Expand Down Expand Up @@ -1015,20 +1026,38 @@ func getChunksCount(ctx context.Context, client *mongo.Client) ([]proto.ChunksBy
}

func getClientOptions(opts *cliOptions) (*options.ClientOptions, error) {
clientOptions := options.Client().ApplyURI(opts.Host)
var clientOptions *options.ClientOptions

clientOptions.ServerSelectionTimeout = &defaultConnectionTimeout
clientOptions.Direct = &directConnection
credential := options.Credential{}
if opts.User != "" {
credential.Username = opts.User
clientOptions.SetAuth(credential)
if opts.URI != "" {
clientOptions = options.Client().ApplyURI(opts.URI)
} else {
host := opts.Host
if host == "" {
host = DefaultHost
}
port := opts.Port
if port == "" {
port = DefaultPort
}

clientOptions = options.Client().ApplyURI("mongodb://" + net.JoinHostPort(host, port))
}

auth := clientOptions.Auth
if auth == nil {
auth = &options.Credential{}
}

if opts.User != "" {
auth.Username = opts.User
}
if opts.Password != "" {
credential.Password = opts.Password
credential.PasswordSet = true
clientOptions.SetAuth(credential)
auth.Password = opts.Password
auth.PasswordSet = true
}

if auth.Username != "" {
clientOptions.SetAuth(*auth)
}

if opts.SSLPEMKeyFile != "" || opts.SSLCAFile != "" {
Expand All @@ -1040,7 +1069,15 @@ func getClientOptions(opts *cliOptions) (*options.ClientOptions, error) {
clientOptions.TLSConfig = tlsConfig
}

return clientOptions, nil
// Defaults
if clientOptions.ServerSelectionTimeout == nil {
clientOptions.ServerSelectionTimeout = &defaultConnectionTimeout
}
if clientOptions.Direct == nil {
clientOptions.Direct = &directConnection
}

return clientOptions, clientOptions.Validate()
}

func getTLSConfig(sslPEMKeyFile, sslCAFile string) (*tls.Config, error) {
Expand Down
Loading
Loading