1 // pstop - Top like progream which collects information from MySQL's
2 // performance_schema database.
18 _ "github.com/go-sql-driver/mysql"
19 "github.com/nsf/termbox-go"
21 "github.com/sjmudd/mysql_defaults_file"
22 "github.com/sjmudd/pstop/lib"
23 "github.com/sjmudd/pstop/state"
24 "github.com/sjmudd/pstop/version"
25 "github.com/sjmudd/pstop/wait_info"
30 db = "performance_schema"
34 flag_version = flag.Bool("version", false, "Show the version of "+lib.MyName())
35 flag_debug = flag.Bool("debug", false, "Enabling debug logging")
36 flag_help = flag.Bool("help", false, "Provide some help for "+lib.MyName())
37 cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
39 re_valid_version = regexp.MustCompile(`^(5\.[67]\.|10\.[01])`)
42 func get_db_handle() *sql.DB {
45 lib.Logger.Println("get_db_handle() connecting to database")
47 dbh, err = mysql_defaults_file.OpenUsingDefaultsFile(sql_driver, "", "performance_schema")
51 if err = dbh.Ping(); err != nil {
58 // make chan for termbox events and run a poller to send events to the channel
59 // - return the channel
60 func new_tb_chan() chan termbox.Event {
61 termboxChan := make(chan termbox.Event)
64 termboxChan <- termbox.PollEvent()
71 fmt.Println(lib.MyName() + " - " + lib.Copyright())
73 fmt.Println("Top-like program to show MySQL activity by using information collected")
74 fmt.Println("from performance_schema.")
76 fmt.Println("Usage: " + lib.MyName() + " <options>")
78 fmt.Println("Options:")
79 fmt.Println("-help show this help message")
80 fmt.Println("-version show the version")
83 // pstop requires MySQL 5.6+ or MariaDB 10.0+. Check the version
84 // rather than giving an error message if the requires P_S tables can't
86 func validate_mysql_version(dbh *sql.DB) error {
87 var tables = [...]string{
88 "performance_schema.file_summary_by_instance",
89 "performance_schema.table_io_waits_summary_by_table",
90 "performance_schema.table_lock_waits_summary_by_table",
93 lib.Logger.Println("validate_mysql_version()")
95 lib.Logger.Println("- Getting MySQL version")
96 err, mysql_version := lib.SelectGlobalVariableByVariableName(dbh, "VERSION")
100 lib.Logger.Println("- mysql_version: '" + mysql_version + "'")
102 if !re_valid_version.MatchString(mysql_version) {
103 err := errors.New(lib.MyName() + " does not work with MySQL version " + mysql_version)
106 lib.Logger.Println("OK: MySQL version is valid, continuing")
108 lib.Logger.Println("Checking access to required tables:")
109 for i := range tables {
110 if err := lib.CheckTableAccess(dbh, tables[i]); err == nil {
111 lib.Logger.Println("OK: " + tables[i] + " found")
116 lib.Logger.Println("OK: all table checks passed")
125 if *cpuprofile != "" {
126 f, err := os.Create(*cpuprofile)
130 pprof.StartCPUProfile(f)
131 defer pprof.StopCPUProfile()
135 lib.Logger.EnableLogging(true)
138 fmt.Println(lib.MyName() + " version " + version.Version())
146 lib.Logger.Println("Starting " + lib.MyName())
148 dbh := get_db_handle()
149 if err := validate_mysql_version(dbh); err != nil {
153 var state state.State
154 var wi wait_info.WaitInfo
155 wi.SetWaitInterval(time.Second)
157 sigChan := make(chan os.Signal, 1)
158 done := make(chan struct{})
160 termboxChan := new_tb_chan()
162 signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
170 fmt.Println("exiting")
172 case sig := <-sigChan:
173 fmt.Println("Caught a signal", sig)
175 case <-wi.WaitNextPeriod():
179 case event := <-termboxChan:
180 // switch on event type
182 case termbox.EventKey: // actions depend on key
184 case termbox.KeyCtrlZ, termbox.KeyCtrlC, termbox.KeyEsc:
186 case termbox.KeyTab: // tab - change display modes
191 case '-': // decrease the interval if > 1
192 if wi.WaitInterval() > time.Second {
193 wi.SetWaitInterval(wi.WaitInterval() - time.Second)
195 case '+': // increase interval by creating a new ticker
196 wi.SetWaitInterval(wi.WaitInterval() + time.Second)
198 state.SetHelp(!state.Help())
201 case 't': // toggle between absolute/relative statistics
202 state.SetWantRelativeStats(!state.WantRelativeStats())
204 case 'z': // reset the statistics to now by taking a query of current values
205 state.ResetDBStatistics()
208 case termbox.EventResize: // set sizes
209 state.ScreenSetSize(event.Width, event.Height)
211 case termbox.EventError: // quit
212 log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
217 lib.Logger.Println("Terminating " + lib.MyName())