1 package setup_instruments
7 "github.com/sjmudd/pstop/lib"
10 // We only match on the error number
11 // Error 1142: UPDATE command denied to user
12 // Error 1290: The MySQL server is running with the --read-only option so it cannot execute this statement
13 var EXPECTED_UPDATE_ERRORS = []string { "Error 1142", "Error 1290" }
15 type setup_instruments_row struct {
21 type SetupInstruments struct {
23 rows []setup_instruments_row
26 // Change settings to monitor stage/sql/%
27 func (si *SetupInstruments) EnableStageMonitoring(dbh *sql.DB) {
28 sql := "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE 'stage/sql/%' AND ( enabled <> 'YES' OR timed <> 'YES' )"
29 collecting := "Collecting p_s.setup_instruments stage/sql configuration settings"
30 updating := "Updating p_s.setup_instruments to allow stage/sql configuration"
32 si.ConfigureSetupInstruments(dbh, sql, collecting, updating)
35 // Change settings to monitor wait/synch/mutex/%
36 func (si *SetupInstruments) EnableMutexMonitoring(dbh *sql.DB) {
37 sql := "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE 'wait/synch/mutex/%' AND ( enabled <> 'YES' OR timed <> 'YES' )"
38 collecting := "Collecting p_s.setup_instruments wait/synch/mutex configuration settings"
39 updating := "Updating p_s.setup_instruments to allow wait/synch/mutex configuration"
41 si.ConfigureSetupInstruments(dbh, sql, collecting, updating)
44 // generic routine (now) to update some rows in setup instruments
45 func (si *SetupInstruments) ConfigureSetupInstruments(dbh *sql.DB, sql string, collecting, updating string) {
46 // setup the old values in case they're not set
48 si.rows = make([]setup_instruments_row, 0, 500)
51 lib.Logger.Println(collecting)
53 rows, err := dbh.Query(sql)
61 var r setup_instruments_row
65 &r.TIMED); err != nil {
68 // we collect all information even if it's mainly empty as we may reference it later
69 si.rows = append(si.rows, r)
72 if err := rows.Err(); err != nil {
75 lib.Logger.Println("- found", count, "rows whose configuration need changing")
77 // update the rows which need to be set - do multiple updates but I don't care
78 lib.Logger.Println(updating)
81 for i := range si.rows {
82 sql := "UPDATE setup_instruments SET enabled = 'YES', TIMED = 'YES' WHERE NAME = '" + si.rows[i].NAME + "'"
83 if _, err := dbh.Exec(sql); err == nil {
84 si.update_succeeded = true
86 found_expected := false
87 for i := range EXPECTED_UPDATE_ERRORS {
88 if err.Error()[0:10] == EXPECTED_UPDATE_ERRORS[i] {
96 lib.Logger.Println( "Insufficient privileges to UPDATE setup_instruments: " + err.Error() )
101 if si.update_succeeded {
102 lib.Logger.Println(count, "rows changed in p_s.setup_instruments")
107 // restore any changed rows back to their original state
108 func (si *SetupInstruments) RestoreConfiguration(dbh *sql.DB) {
109 // If the previous update didn't work then don't try to restore
110 if ! si.update_succeeded {
111 lib.Logger.Println("Not restoring p_s.setup_instruments to its original settings as previous UPDATE had failed")
114 lib.Logger.Println("Restoring p_s.setup_instruments to its original settings")
117 // update the rows which need to be set - do multiple updates but I don't care
119 for i := range si.rows {
120 sql := "UPDATE setup_instruments SET enabled = '" + si.rows[i].ENABLED + "', TIMED = '" + si.rows[i].TIMED + "' WHERE NAME = '" + si.rows[i].NAME + "'"
121 if _, err := dbh.Exec(sql); err != nil {
126 lib.Logger.Println(count, "rows changed in p_s.setup_instruments")