1 // manage the configuration of performance_schema.setup_instruments
2 package setup_instruments
8 "github.com/sjmudd/pstop/lib"
11 // We only match on the error number
12 // Error 1142: UPDATE command denied to user
13 // Error 1290: The MySQL server is running with the --read-only option so it cannot execute this statement
14 var EXPECTED_UPDATE_ERRORS = []string{
19 // one row of performance_schema.setup_instruments
20 type table_row struct {
26 type table_rows []table_row
28 // SetupInstruments "object"
29 type SetupInstruments struct {
34 // Change settings to monitor stage/sql/%
35 func (si *SetupInstruments) EnableStageMonitoring(dbh *sql.DB) {
36 sql := "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE 'stage/sql/%' AND ( enabled <> 'YES' OR timed <> 'YES' )"
37 collecting := "Collecting p_s.setup_instruments stage/sql configuration settings"
38 updating := "Updating p_s.setup_instruments to allow stage/sql configuration"
40 si.ConfigureSetupInstruments(dbh, sql, collecting, updating)
43 // Change settings to monitor wait/synch/mutex/%
44 func (si *SetupInstruments) EnableMutexMonitoring(dbh *sql.DB) {
45 sql := "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE 'wait/synch/mutex/%' AND ( enabled <> 'YES' OR timed <> 'YES' )"
46 collecting := "Collecting p_s.setup_instruments wait/synch/mutex configuration settings"
47 updating := "Updating p_s.setup_instruments to allow wait/synch/mutex configuration"
49 si.ConfigureSetupInstruments(dbh, sql, collecting, updating)
52 // generic routine (now) to update some rows in setup instruments
53 func (si *SetupInstruments) ConfigureSetupInstruments(dbh *sql.DB, sql string, collecting, updating string) {
54 // setup the old values in case they're not set
56 si.rows = make([]table_row, 0, 500)
59 lib.Logger.Println(collecting)
61 rows, err := dbh.Query(sql)
73 &r.TIMED); err != nil {
76 // we collect all information even if it's mainly empty as we may reference it later
77 si.rows = append(si.rows, r)
80 if err := rows.Err(); err != nil {
83 lib.Logger.Println("- found", count, "rows whose configuration need changing")
85 // update the rows which need to be set - do multiple updates but I don't care
86 lib.Logger.Println(updating)
89 update_sql := "UPDATE setup_instruments SET enabled = ?, TIMED = ? WHERE NAME = ?"
90 stmt, err := dbh.Prepare( update_sql )
94 for i := range si.rows {
95 if _, err := stmt.Exec(update_sql, "YES", "YES", si.rows[i].NAME); err == nil {
96 si.update_succeeded = true
98 found_expected := false
99 for i := range EXPECTED_UPDATE_ERRORS {
100 if err.Error()[0:10] == EXPECTED_UPDATE_ERRORS[i] {
101 found_expected = true
108 lib.Logger.Println("Insufficient privileges to UPDATE setup_instruments: " + err.Error())
113 if si.update_succeeded {
114 lib.Logger.Println(count, "rows changed in p_s.setup_instruments")
118 // restore setup_instruments rows to their previous settings
119 func (si *SetupInstruments) RestoreConfiguration(dbh *sql.DB) {
120 // If the previous update didn't work then don't try to restore
121 if !si.update_succeeded {
122 lib.Logger.Println("Not restoring p_s.setup_instruments to its original settings as previous UPDATE had failed")
125 lib.Logger.Println("Restoring p_s.setup_instruments to its original settings")
128 // update the rows which need to be set - do multiple updates but I don't care
130 update_sql := "UPDATE setup_instruments SET enabled = ?, TIMED = ? WHERE NAME = ?"
131 stmt, err := dbh.Prepare( update_sql )
135 for i := range si.rows {
136 if _, err := stmt.Exec(update_sql, si.rows[i].ENABLED, si.rows[i].TIMED, si.rows[i].NAME ); err != nil {
141 lib.Logger.Println(count, "rows changed in p_s.setup_instruments")