Install and update this go package with `go get -u github.com/sjmudd/pstop`
+### Dependencies
+
+The following Non-core Go dependencies are:
+* github.com/sjmudd/mysql_defaults_file for connecting to MySQL via
+a defaults file.
+* github.com/nsf/termbox-go a library for creating cross-platform
+text-based interfaces.
+
### Configuration
-Access to MySQL is currently via a defaults-file which is assumed
-to be `~/.my.cnf`. Alternatively you can now provide
--defaults-file=/path/to/.my.cnf if needed.
+Access to MySQL can be made by one of the following methods:
+* Default: use a defaults-file named `~/.my.cnf`.
+* use an explicit defaults-file with `--defaults-file=/path/to/.my.cnf`.
+* connect to a host with `--host=somehost --port=999 --user=someuser --password=somepass`, or
+* connect via a socket with `--socket=/path/to/mysql.sock --user=someuser --password=somepass`
+
+The user if not specified will default to the contents of `$USER`.
### Grants
+// key_value_cache provides an extremely simple string key to value cache.
+// This is used to reduce the number of lookups from MySQL filename
+// to the equivalent table or logical name given the conversion
+// routines to do this use many regexps and this is quite expensive.
package key_value_cache
import (
read_requests, served_from_cache, write_requests int
}
-// create a new KeyValueCache entry
+// Create a new KeyValueCache entry.
func NewKeyValueCache() KeyValueCache {
lib.Logger.Println("KeyValueCache()")
- var kvc KeyValueCache
- return kvc
+ return KeyValueCache {}
}
-// return value if found
+// Given a lookup key return the value if found.
func (kvc *KeyValueCache) Get(key string) (result string, err error) {
lib.Logger.Println("KeyValueCache.Get(", key, ")")
if kvc.cache == nil {
}
}
-// write to cache and return value
+// Write to cache and return the value saved.
func (kvc *KeyValueCache) Put(key, value string) string {
lib.Logger.Println("KeyValueCache.Put(", key, ",", value, ")")
kvc.cache[key] = value
return value
}
+// Provide some staticts on read and write requests and the number
+// of requests served from cache.
func (kvc *KeyValueCache) Statistics() (int, int, int) {
return kvc.read_requests, kvc.served_from_cache, kvc.write_requests
}
flag_debug = flag.Bool("debug", false, "Enabling debug logging")
flag_defaults_file = flag.String("defaults-file", "", "Provide a defaults-file to use to connect to MySQL")
flag_help = flag.Bool("help", false, "Provide some help for "+lib.MyName())
+ flag_host = flag.String("host", "", "Provide the hostname of the MySQL to connect to")
+ 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 */
+ flag_socket = flag.String("socket", "", "Provide the path to the local MySQL server to connect to")
+ flag_password = flag.String("password", "", "Provide the password when connecting to the MySQL server")
+ flag_user = flag.String("user", "", "Provide the username to connect with to MySQL (default: $USER)")
flag_version = flag.Bool("version", false, "Show the version of "+lib.MyName())
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
)
// Connect to the database with the given defaults-file, or ~/.my.cnf if not provided.
-func get_db_handle( defaults_file string ) *sql.DB {
+func connect_by_defaults_file( defaults_file string ) *sql.DB {
var err error
var dbh *sql.DB
- lib.Logger.Println("get_db_handle() connecting to database")
+ lib.Logger.Println("connect_by_defaults_file() connecting to database")
dbh, err = mysql_defaults_file.OpenUsingDefaultsFile(sql_driver, defaults_file, "performance_schema")
if err != nil {
return dbh
}
+// connect to MySQL using various component parts needed to make the dsn
+func connect_by_components( components map[string]string ) *sql.DB {
+ var err error
+ var dbh *sql.DB
+ lib.Logger.Println("connect_by_components() connecting to database")
+
+ new_dsn := mysql_defaults_file.BuildDSN(components, "performance_schema")
+ dbh, err = sql.Open(sql_driver, new_dsn)
+ if err != nil {
+ log.Fatal(err)
+ }
+ if err = dbh.Ping(); err != nil {
+ log.Fatal(err)
+ }
+
+ return dbh
+}
+
// make chan for termbox events and run a poller to send events to the channel
// - return the channel
func new_tb_chan() chan termbox.Event {
fmt.Println("Usage: " + lib.MyName() + " <options>")
fmt.Println("")
fmt.Println("Options:")
- fmt.Println("-defaults-file=/path/to/defaults.file Connect to MySQL using given defaults-file" )
- fmt.Println("-help show this help message")
- fmt.Println("-version show the version")
+ fmt.Println("--defaults-file=/path/to/defaults.file Connect to MySQL using given defaults-file" )
+ fmt.Println("--help Show this help message")
+ fmt.Println("--version Show the version")
+ fmt.Println("--host=<hostname> MySQL host to connect to")
+ fmt.Println("--port=<port> MySQL port to connect to")
+ fmt.Println("--socket=<path> MySQL path of the socket to connect to")
+ fmt.Println("--user=<user> User to connect with")
+ fmt.Println("--password=<password> Password to use when connecting")
}
// pstop requires MySQL 5.6+ or MariaDB 10.0+. Check the version
lib.Logger.Println("Starting " + lib.MyName())
- if flag_defaults_file != nil && *flag_defaults_file != "" {
- defaults_file = *flag_defaults_file
+ var dbh *sql.DB
+
+ if *flag_host != "" || *flag_socket != "" {
+ lib.Logger.Println("--host= or --socket= defined")
+ var components = make(map[string]string)
+ if *flag_host != "" && *flag_socket != "" {
+ fmt.Println(lib.MyName() + ": Do not specify --host and --socket together" )
+ os.Exit(1)
+ }
+ if *flag_host != "" {
+ components["host"] = *flag_host
+ }
+ if *flag_port != 0 {
+ if *flag_socket == "" {
+ components["port"] = fmt.Sprintf("%d", *flag_port)
+ } else {
+ fmt.Println(lib.MyName() + ": Do not specify --socket and --port together" )
+ os.Exit(1)
+ }
+ }
+ if *flag_socket != "" {
+ components["socket"] = *flag_socket
+ }
+ if *flag_user != "" {
+ components["user"] = *flag_user
+ }
+ if *flag_password != "" {
+ components["password"] = *flag_password
+ }
+ dbh = connect_by_components( components )
+ } else {
+ if flag_defaults_file != nil && *flag_defaults_file != "" {
+ lib.Logger.Println("--defaults-file defined")
+ defaults_file = *flag_defaults_file
+ } else {
+ lib.Logger.Println("connecting by implicit defaults file")
+ }
+ dbh = connect_by_defaults_file( defaults_file )
}
- dbh := get_db_handle( defaults_file )
if err := validate_mysql_version(dbh); err != nil {
log.Fatal(err)
}