1 // 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"
29 db = "performance_schema"
33 flag_version = flag.Bool("version", false, "Show the version of "+lib.MyName())
34 flag_debug = flag.Bool("debug", false, "Enabling debug logging")
35 flag_help = flag.Bool("help", false, "Provide some help for "+lib.MyName())
36 cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
38 re_valid_version = regexp.MustCompile(`^(5\.[67]\.|10\.[01])`)
41 func get_db_handle() *sql.DB {
44 lib.Logger.Println("get_db_handle() connecting to database")
46 dbh, err = mysql_defaults_file.OpenUsingDefaultsFile(sql_driver, "", "performance_schema")
50 if err = dbh.Ping(); err != nil {
57 // make chan for termbox events and run a poller to send events to the channel
58 // - return the channel
59 func new_tb_chan() chan termbox.Event {
60 termboxChan := make(chan termbox.Event)
63 termboxChan <- termbox.PollEvent()
70 fmt.Println(lib.MyName() + " - " + lib.Copyright())
72 fmt.Println("Top-like program to show MySQL activity by using information collected")
73 fmt.Println("from performance_schema.")
75 fmt.Println("Usage: " + lib.MyName() + " <options>")
77 fmt.Println("Options:")
78 fmt.Println("-help show this help message")
79 fmt.Println("-version show the version")
82 // pstop requires MySQL 5.6+ or MariaDB 10.0+. Check the version
83 // rather than giving an error message if the requires P_S tables can't
85 func validate_mysql_version(dbh *sql.DB) error {
86 lib.Logger.Println("validate_mysql_version()")
88 _, mysql_version := lib.SelectGlobalVariableByVariableName(dbh, "VERSION")
90 lib.Logger.Println("- mysql_version: '" + mysql_version + "'")
91 if !re_valid_version.MatchString(mysql_version) {
92 err := errors.New(lib.MyName() + " does not work with MySQL version " + mysql_version)
95 lib.Logger.Println("- MySQL version is valid, continuing")
104 if *cpuprofile != "" {
105 f, err := os.Create(*cpuprofile)
109 pprof.StartCPUProfile(f)
110 defer pprof.StopCPUProfile()
114 lib.Logger.EnableLogging(true)
117 fmt.Println(lib.MyName() + " version " + version.Version())
125 lib.Logger.Println("Starting " + lib.MyName())
127 dbh := get_db_handle()
128 if err := validate_mysql_version(dbh); err != nil {
132 var state state.State
133 interval := time.Second
134 sigChan := make(chan os.Signal, 1)
135 done := make(chan struct{})
137 termboxChan := new_tb_chan()
139 signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
141 ticker := time.NewTicker(interval) // generate a periodic signal
149 fmt.Println("exiting")
151 case sig := <-sigChan:
152 fmt.Println("Caught a signal", sig)
157 case event := <-termboxChan:
158 // switch on event type
160 case termbox.EventKey: // actions depend on key
162 case termbox.KeyCtrlZ, termbox.KeyCtrlC, termbox.KeyEsc:
164 case termbox.KeyTab: // tab - change display modes
169 case '-': // decrease the interval if > 1
170 if interval > time.Second {
172 interval -= time.Second
173 ticker = time.NewTicker(interval)
175 case '+': // increase interval by creating a new ticker
177 interval += time.Second
178 ticker = time.NewTicker(interval)
180 state.SetHelp(!state.Help())
183 case 't': // toggle between absolute/relative statistics
184 state.SetWantRelativeStats(!state.WantRelativeStats())
186 case 'z': // reset the statistics to now by taking a query of current values
187 state.ResetDBStatistics()
190 case termbox.EventResize: // set sizes
191 state.ScreenSetSize(event.Width, event.Height)
193 case termbox.EventError: // quit
194 log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
200 lib.Logger.Println("Terminating " + lib.MyName())