cf4e347b2cbf39cd7e125e8b7f4b094ece274c09
[pstop.git] / p_s / setup_instruments / setup_instruments.go
1 // manage the configuration of performance_schema.setup_instruments
2 package setup_instruments
3
4 import (
5         "database/sql"
6         "log"
7
8         "github.com/sjmudd/pstop/lib"
9 )
10
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{
15         "Error 1142",
16         "Error 1290",
17 }
18
19 // one row of performance_schema.setup_instruments
20 type table_row struct {
21         NAME    string
22         ENABLED string
23         TIMED   string
24 }
25
26 type table_rows []table_row
27
28 // SetupInstruments "object"
29 type SetupInstruments struct {
30         update_succeeded bool
31         rows             table_rows
32 }
33
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"
39
40         si.ConfigureSetupInstruments(dbh, sql, collecting, updating)
41 }
42
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"
48
49         si.ConfigureSetupInstruments(dbh, sql, collecting, updating)
50 }
51
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
55         if si.rows == nil {
56                 si.rows = make([]table_row, 0, 500)
57         }
58
59         lib.Logger.Println(collecting)
60
61         rows, err := dbh.Query(sql)
62         if err != nil {
63                 log.Fatal(err)
64         }
65         defer rows.Close()
66
67         count := 0
68         for rows.Next() {
69                 var r table_row
70                 if err := rows.Scan(
71                         &r.NAME,
72                         &r.ENABLED,
73                         &r.TIMED); err != nil {
74                         log.Fatal(err)
75                 }
76                 // we collect all information even if it's mainly empty as we may reference it later
77                 si.rows = append(si.rows, r)
78                 count++
79         }
80         if err := rows.Err(); err != nil {
81                 log.Fatal(err)
82         }
83         lib.Logger.Println("- found", count, "rows whose configuration need changing")
84
85         // update the rows which need to be set - do multiple updates but I don't care
86         lib.Logger.Println(updating)
87
88         count = 0
89         update_sql := "UPDATE setup_instruments SET enabled = ?, TIMED = ? WHERE NAME = ?"
90         stmt, err := dbh.Prepare( update_sql )
91         if err != nil {
92                 log.Fatal(err)
93         }
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
97                 } else {
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
102                                         break
103                                 }
104                         }
105                         if !found_expected {
106                                 log.Fatal(err)
107                         }
108                         lib.Logger.Println("Insufficient privileges to UPDATE setup_instruments: " + err.Error())
109                         break
110                 }
111                 count++
112         }
113         if si.update_succeeded {
114                 lib.Logger.Println(count, "rows changed in p_s.setup_instruments")
115         }
116 }
117
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")
123                 return
124         } else {
125                 lib.Logger.Println("Restoring p_s.setup_instruments to its original settings")
126         }
127
128         // update the rows which need to be set - do multiple updates but I don't care
129         count := 0
130         update_sql := "UPDATE setup_instruments SET enabled = ?, TIMED = ? WHERE NAME = ?"
131         stmt, err := dbh.Prepare( update_sql )
132         if err != nil {
133                 log.Fatal(err)
134         }
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 {
137                         log.Fatal(err)
138                 }
139                 count++
140         }
141         lib.Logger.Println(count, "rows changed in p_s.setup_instruments")
142 }