Rename state to app and furhter cleanups
[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 const (
21         sql_driver = "mysql"
22         db         = "performance_schema"
23 )
24
25 var (
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")
36 )
37
38 func usage() {
39         fmt.Println(lib.MyName() + " - " + lib.Copyright())
40         fmt.Println("")
41         fmt.Println("Top-like program to show MySQL activity by using information collected")
42         fmt.Println("from performance_schema.")
43         fmt.Println("")
44         fmt.Println("Usage: " + lib.MyName() + " <options>")
45         fmt.Println("")
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")
55 }
56
57 func main() {
58         var defaults_file string = ""
59         flag.Parse()
60
61         // clean me up
62         if *cpuprofile != "" {
63                 f, err := os.Create(*cpuprofile)
64                 if err != nil {
65                         log.Fatal(err)
66                 }
67                 pprof.StartCPUProfile(f)
68                 defer pprof.StopCPUProfile()
69         }
70
71         if *flag_debug {
72                 lib.Logger.EnableLogging(true)
73         }
74         if *flag_version {
75                 fmt.Println(lib.MyName() + " version " + version.Version())
76                 return
77         }
78         if *flag_help {
79                 usage()
80                 return
81         }
82
83         lib.Logger.Println("Starting " + lib.MyName())
84
85         var connector connector.Connector
86
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")
92                         os.Exit(1)
93                 }
94                 if *flag_host != "" {
95                         components["host"] = *flag_host
96                 }
97                 if *flag_port != 0 {
98                         if *flag_socket == "" {
99                                 components["port"] = fmt.Sprintf("%d", *flag_port)
100                         } else {
101                                 fmt.Println(lib.MyName() + ": Do not specify --socket and --port together")
102                                 os.Exit(1)
103                         }
104                 }
105                 if *flag_socket != "" {
106                         components["socket"] = *flag_socket
107                 }
108                 if *flag_user != "" {
109                         components["user"] = *flag_user
110                 }
111                 if *flag_password != "" {
112                         components["password"] = *flag_password
113                 }
114                 connector.ConnectByComponents(components)
115         } else {
116                 if flag_defaults_file != nil && *flag_defaults_file != "" {
117                         lib.Logger.Println("--defaults-file defined")
118                         defaults_file = *flag_defaults_file
119                 } else {
120                         lib.Logger.Println("connecting by implicit defaults file")
121                 }
122                 connector.ConnectByDefaultsFile(defaults_file)
123         }
124
125         var app app.App
126
127         app.Setup(connector.Handle())
128         app.Run()
129         app.Cleanup()
130         lib.Logger.Println("Terminating " + lib.MyName())
131 }