1// Copyright (C) MongoDB, Inc. 2014-present. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may 4// not use this file except in compliance with the License. You may obtain 5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7package main 8 9import ( 10 "github.com/jessevdk/go-flags" 11 "github.com/mongodb/mongo-tools/legacy/options" 12 "github.com/mongodb/mongo-tools/mongoreplay" 13 14 "fmt" 15 "os" 16 "runtime" 17) 18 19const ( 20 ExitOk = 0 21 ExitError = 1 22 ExitNonFatal = 3 23 // Go reserves exit code 2 for its own use 24) 25 26var ( 27 VersionStr = "built-without-version-string" 28 GitCommit = "build-without-git-commit" 29) 30 31func main() { 32 versionOpts := mongoreplay.VersionOptions{} 33 versionFlagParser := flags.NewParser(&versionOpts, flags.Default) 34 versionFlagParser.Options = flags.IgnoreUnknown 35 _, err := versionFlagParser.Parse() 36 if err != nil { 37 os.Exit(ExitError) 38 } 39 40 if versionOpts.PrintVersion(VersionStr, GitCommit) { 41 os.Exit(ExitOk) 42 } 43 44 if runtime.NumCPU() == 1 { 45 fmt.Fprint(os.Stderr, "mongoreplay must be run with multiple threads") 46 os.Exit(ExitError) 47 } 48 49 opts := mongoreplay.Options{VersionStr: VersionStr, GitCommit: GitCommit} 50 51 var parser = flags.NewParser(&opts, flags.Default) 52 53 playCmd := &mongoreplay.PlayCommand{GlobalOpts: &opts} 54 playCmdParser, err := parser.AddCommand("play", "Play captured traffic against a mongodb instance", "", playCmd) 55 if err != nil { 56 panic(err) 57 } 58 if options.BuiltWithSSL { 59 playCmd.SSLOpts = &options.SSL{} 60 _, err := playCmdParser.AddGroup("ssl", "", playCmd.SSLOpts) 61 if err != nil { 62 panic(err) 63 } 64 } 65 66 _, err = parser.AddCommand("record", "Convert network traffic into mongodb queries", "", 67 &mongoreplay.RecordCommand{GlobalOpts: &opts}) 68 if err != nil { 69 panic(err) 70 } 71 72 _, err = parser.AddCommand("monitor", "Inspect live or pre-recorded mongodb traffic", "", 73 &mongoreplay.MonitorCommand{GlobalOpts: &opts}) 74 if err != nil { 75 panic(err) 76 } 77 78 _, err = parser.AddCommand("filter", "Filter playback file", "", 79 &mongoreplay.FilterCommand{GlobalOpts: &opts}) 80 if err != nil { 81 panic(err) 82 } 83 84 _, err = parser.Parse() 85 86 if err != nil { 87 switch err.(type) { 88 case mongoreplay.ErrPacketsDropped: 89 os.Exit(ExitNonFatal) 90 default: 91 os.Exit(ExitError) 92 } 93 } 94} 95