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"
21 cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
22 flag_debug = flag.Bool("debug", false, "Enabling debug logging")
23 flag_defaults_file = flag.String("defaults-file", "", "Provide a defaults-file to use to connect to MySQL")
24 flag_help = flag.Bool("help", false, "Provide some help for "+lib.MyName())
25 flag_host = flag.String("host", "", "Provide the hostname of the MySQL to connect to")
26 flag_password = flag.String("password", "", "Provide the password when connecting to the MySQL server")
27 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 */
28 flag_socket = flag.String("socket", "", "Provide the path to the local MySQL server to connect to")
29 flag_user = flag.String("user", "", "Provide the username to connect with to MySQL (default: $USER)")
30 flag_version = flag.Bool("version", false, "Show the version of "+lib.MyName())
34 fmt.Println(lib.MyName() + " - " + lib.Copyright())
36 fmt.Println("Top-like program to show MySQL activity by using information collected")
37 fmt.Println("from performance_schema.")
39 fmt.Println("Usage: " + lib.MyName() + " <options>")
41 fmt.Println("Options:")
42 fmt.Println("--defaults-file=/path/to/defaults.file Connect to MySQL using given defaults-file")
43 fmt.Println("--help Show this help message")
44 fmt.Println("--version Show the version")
45 fmt.Println("--host=<hostname> MySQL host to connect to")
46 fmt.Println("--password=<password> Password to use when connecting")
47 fmt.Println("--port=<port> MySQL port to connect to")
48 fmt.Println("--socket=<path> MySQL path of the socket to connect to")
49 fmt.Println("--user=<user> User to connect with")
53 var connector connector.Connector
54 var defaults_file string = ""
59 if *cpuprofile != "" {
60 f, err := os.Create(*cpuprofile)
64 pprof.StartCPUProfile(f)
65 defer pprof.StopCPUProfile()
69 lib.Logger.EnableLogging(true)
72 fmt.Println(lib.MyName() + " version " + version.Version())
80 lib.Logger.Println("Starting " + lib.MyName())
82 if *flag_host != "" || *flag_socket != "" {
83 lib.Logger.Println("--host= or --socket= defined")
84 var components = make(map[string]string)
85 if *flag_host != "" && *flag_socket != "" {
86 fmt.Println(lib.MyName() + ": Do not specify --host and --socket together")
90 components["host"] = *flag_host
93 if *flag_socket == "" {
94 components["port"] = fmt.Sprintf("%d", *flag_port)
96 fmt.Println(lib.MyName() + ": Do not specify --socket and --port together")
100 if *flag_socket != "" {
101 components["socket"] = *flag_socket
103 if *flag_user != "" {
104 components["user"] = *flag_user
106 if *flag_password != "" {
107 components["password"] = *flag_password
109 connector.ConnectByComponents(components)
111 if flag_defaults_file != nil && *flag_defaults_file != "" {
112 lib.Logger.Println("--defaults-file defined")
113 defaults_file = *flag_defaults_file
115 lib.Logger.Println("connecting by implicit defaults file")
117 connector.ConnectByDefaultsFile(defaults_file)
122 app.Setup(connector.Handle())
125 lib.Logger.Println("Terminating " + lib.MyName())