1 // pstop - Top like progream which collects information from MySQL's
2 // performance_schema database.
12 _ "github.com/go-sql-driver/mysql"
14 "github.com/sjmudd/pstop/app"
15 "github.com/sjmudd/pstop/connector"
16 "github.com/sjmudd/pstop/lib"
17 "github.com/sjmudd/pstop/version"
22 db = "performance_schema"
26 flag_debug = flag.Bool("debug", false, "Enabling debug logging")
27 flag_defaults_file = flag.String("defaults-file", "", "Provide a defaults-file to use to connect to MySQL")
28 flag_help = flag.Bool("help", false, "Provide some help for "+lib.MyName())
29 flag_host = flag.String("host", "", "Provide the hostname of the MySQL to connect to")
30 flag_port = flag.Int("port", 0, "Provide the port number of the MySQL to connect to (default: 3306)") /* deliberately 0 here, defaults to 3306 elsewhere */
31 flag_socket = flag.String("socket", "", "Provide the path to the local MySQL server to connect to")
32 flag_password = flag.String("password", "", "Provide the password when connecting to the MySQL server")
33 flag_user = flag.String("user", "", "Provide the username to connect with to MySQL (default: $USER)")
34 flag_version = flag.Bool("version", false, "Show the version of "+lib.MyName())
35 cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
39 fmt.Println(lib.MyName() + " - " + lib.Copyright())
41 fmt.Println("Top-like program to show MySQL activity by using information collected")
42 fmt.Println("from performance_schema.")
44 fmt.Println("Usage: " + lib.MyName() + " <options>")
46 fmt.Println("Options:")
47 fmt.Println("--defaults-file=/path/to/defaults.file Connect to MySQL using given defaults-file")
48 fmt.Println("--help Show this help message")
49 fmt.Println("--version Show the version")
50 fmt.Println("--host=<hostname> MySQL host to connect to")
51 fmt.Println("--port=<port> MySQL port to connect to")
52 fmt.Println("--socket=<path> MySQL path of the socket to connect to")
53 fmt.Println("--user=<user> User to connect with")
54 fmt.Println("--password=<password> Password to use when connecting")
58 var defaults_file string = ""
62 if *cpuprofile != "" {
63 f, err := os.Create(*cpuprofile)
67 pprof.StartCPUProfile(f)
68 defer pprof.StopCPUProfile()
72 lib.Logger.EnableLogging(true)
75 fmt.Println(lib.MyName() + " version " + version.Version())
83 lib.Logger.Println("Starting " + lib.MyName())
85 var connector connector.Connector
87 if *flag_host != "" || *flag_socket != "" {
88 lib.Logger.Println("--host= or --socket= defined")
89 var components = make(map[string]string)
90 if *flag_host != "" && *flag_socket != "" {
91 fmt.Println(lib.MyName() + ": Do not specify --host and --socket together")
95 components["host"] = *flag_host
98 if *flag_socket == "" {
99 components["port"] = fmt.Sprintf("%d", *flag_port)
101 fmt.Println(lib.MyName() + ": Do not specify --socket and --port together")
105 if *flag_socket != "" {
106 components["socket"] = *flag_socket
108 if *flag_user != "" {
109 components["user"] = *flag_user
111 if *flag_password != "" {
112 components["password"] = *flag_password
114 connector.ConnectByComponents(components)
116 if flag_defaults_file != nil && *flag_defaults_file != "" {
117 lib.Logger.Println("--defaults-file defined")
118 defaults_file = *flag_defaults_file
120 lib.Logger.Println("connecting by implicit defaults file")
122 connector.ConnectByDefaultsFile(defaults_file)
127 app.Setup(connector.Handle())
130 lib.Logger.Println("Terminating " + lib.MyName())