move var connector to top of main()
[pstop.git] / main.go
1 // pstop - Top like progream which collects information from MySQL's
2 // performance_schema database.
3 package main
4
5 import (
6         "flag"
7         "fmt"
8         "log"
9         "os"
10         "runtime/pprof"
11
12         _ "github.com/go-sql-driver/mysql"
13
14         "github.com/sjmudd/pstop/app"
15         "github.com/sjmudd/pstop/connector"
16         "github.com/sjmudd/pstop/lib"
17         "github.com/sjmudd/pstop/version"
18 )
19
20 var (
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())
31 )
32
33 func usage() {
34         fmt.Println(lib.MyName() + " - " + lib.Copyright())
35         fmt.Println("")
36         fmt.Println("Top-like program to show MySQL activity by using information collected")
37         fmt.Println("from performance_schema.")
38         fmt.Println("")
39         fmt.Println("Usage: " + lib.MyName() + " <options>")
40         fmt.Println("")
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")
50 }
51
52 func main() {
53         var connector connector.Connector
54         var defaults_file string = ""
55
56         flag.Parse()
57
58         // clean me up
59         if *cpuprofile != "" {
60                 f, err := os.Create(*cpuprofile)
61                 if err != nil {
62                         log.Fatal(err)
63                 }
64                 pprof.StartCPUProfile(f)
65                 defer pprof.StopCPUProfile()
66         }
67
68         if *flag_debug {
69                 lib.Logger.EnableLogging(true)
70         }
71         if *flag_version {
72                 fmt.Println(lib.MyName() + " version " + version.Version())
73                 return
74         }
75         if *flag_help {
76                 usage()
77                 return
78         }
79
80         lib.Logger.Println("Starting " + lib.MyName())
81
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")
87                         os.Exit(1)
88                 }
89                 if *flag_host != "" {
90                         components["host"] = *flag_host
91                 }
92                 if *flag_port != 0 {
93                         if *flag_socket == "" {
94                                 components["port"] = fmt.Sprintf("%d", *flag_port)
95                         } else {
96                                 fmt.Println(lib.MyName() + ": Do not specify --socket and --port together")
97                                 os.Exit(1)
98                         }
99                 }
100                 if *flag_socket != "" {
101                         components["socket"] = *flag_socket
102                 }
103                 if *flag_user != "" {
104                         components["user"] = *flag_user
105                 }
106                 if *flag_password != "" {
107                         components["password"] = *flag_password
108                 }
109                 connector.ConnectByComponents(components)
110         } else {
111                 if flag_defaults_file != nil && *flag_defaults_file != "" {
112                         lib.Logger.Println("--defaults-file defined")
113                         defaults_file = *flag_defaults_file
114                 } else {
115                         lib.Logger.Println("connecting by implicit defaults file")
116                 }
117                 connector.ConnectByDefaultsFile(defaults_file)
118         }
119
120         var app app.App
121
122         app.Setup(connector.Handle())
123         app.Run()
124         app.Cleanup()
125         lib.Logger.Println("Terminating " + lib.MyName())
126 }