1 // Manage the configuration of setup_instruments.
2 package setup_instruments
9 "github.com/sjmudd/pstop/lib"
13 const sql_select = "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE ? AND 'YES NOT IN (enabled,timed)"
15 // We only match on the error number
16 // Error 1142: UPDATE command denied to user 'cacti'@'10.164.132.182' for table 'setup_instruments'
17 // Error 1290: The MySQL server is running with the --read-only option so it cannot execute this statement
18 var EXPECTED_UPDATE_ERRORS = []string{
23 // one row of performance_schema.setup_instruments
24 type table_row struct {
30 type table_rows []table_row
32 // SetupInstruments "object"
33 type SetupInstruments struct {
40 // Return a newly initialised SetupInstruments structure with a handle to the database.
41 // Better to return a pointer ?
42 func NewSetupInstruments(dbh *sql.DB) SetupInstruments {
43 return SetupInstruments{dbh: dbh}
46 // enable mutex and stage monitoring
47 func (si *SetupInstruments) EnableMonitoring() {
48 si.EnableMutexMonitoring()
49 si.EnableStageMonitoring()
52 // Change settings to monitor stage/sql/%
53 func (si *SetupInstruments) EnableStageMonitoring() {
54 lib.Logger.Println("EnableStageMonitoring")
55 sql_match := "stage/sql/%"
56 collecting := "Collecting setup_instruments stage/sql configuration settings"
57 updating := "Updating setup_instruments configuration for: stage/sql"
59 si.Configure(sql_match, collecting, updating)
60 lib.Logger.Println("EnableStageMonitoring finishes")
63 // Change settings to monitor wait/synch/mutex/%
64 func (si *SetupInstruments) EnableMutexMonitoring() {
65 lib.Logger.Println("EnableMutexMonitoring")
66 sql_match := "wait/synch/mutex/%"
67 collecting := "Collecting setup_instruments wait/synch/mutex configuration settings"
68 updating := "Updating setup_instruments configuration for: wait/synch/mutex"
70 si.Configure(sql_match, collecting, updating)
71 lib.Logger.Println("EnableMutexMonitoring finishes")
74 // return true if the error is not in the expected list
75 func error_in_expected_list(actual_error string, expected_errors []string) bool {
76 lib.Logger.Println("checking if", actual_error, "is in", expected_errors)
77 e := actual_error[0:11]
78 expected_error := false
79 for i := range expected_errors {
80 if e == expected_errors[i] {
81 lib.Logger.Println("found an expected error", expected_errors[i])
86 lib.Logger.Println("returning", expected_error)
90 // generic routine (now) to update some rows in setup instruments
91 func (si *SetupInstruments) Configure(sql_match string, collecting, updating string) {
92 lib.Logger.Println(fmt.Sprintf("Configure(%q,%q,%q)", sql_match, collecting, updating))
93 // skip if we've tried and failed
94 if si.update_tried && !si.update_succeeded {
95 lib.Logger.Println("Configure() - Skipping further configuration")
99 // setup the old values in case they're not set
101 si.rows = make([]table_row, 0, 500)
104 lib.Logger.Println(collecting)
106 lib.Logger.Println("dbh.query", sql_select, sql_match)
107 rows, err := si.dbh.Query(sql_select, sql_match)
118 &r.TIMED); err != nil {
121 si.rows = append(si.rows, r)
124 if err := rows.Err(); err != nil {
128 lib.Logger.Println("- found", count, "rows whose configuration need changing")
130 // update the rows which need to be set - do multiple updates but I don't care
131 lib.Logger.Println(updating)
133 const update_sql = "UPDATE setup_instruments SET enabled = ?, TIMED = ? WHERE NAME = ?"
134 lib.Logger.Println("Preparing statement:", update_sql)
135 si.update_tried = true
136 lib.Logger.Println("dbh.Prepare", update_sql)
137 stmt, err := si.dbh.Prepare(update_sql)
139 lib.Logger.Println("- prepare gave error:", err.Error())
140 if !error_in_expected_list(err.Error(), EXPECTED_UPDATE_ERRORS) {
141 log.Fatal("Not expected error so giving up")
143 lib.Logger.Println("- expected error so not running statement")
146 lib.Logger.Println("Prepare succeeded, trying to update", len(si.rows), "row(s)")
148 for i := range si.rows {
149 lib.Logger.Println("- changing row:", si.rows[i].NAME)
150 lib.Logger.Println("stmt.Exec", "YES", "YES", si.rows[i].NAME)
151 if res, err := stmt.Exec("YES", "YES", si.rows[i].NAME); err == nil {
152 lib.Logger.Println("update succeeded")
153 si.update_succeeded = true
154 c, _ := res.RowsAffected()
157 si.update_succeeded = false
158 if error_in_expected_list(err.Error(), EXPECTED_UPDATE_ERRORS) {
159 lib.Logger.Println("Insufficient privileges to UPDATE setup_instruments: " + err.Error())
160 lib.Logger.Println("Not attempting further updates")
167 if si.update_succeeded {
168 lib.Logger.Println(count, "rows changed in p_s.setup_instruments")
172 lib.Logger.Println("Configure() returns update_tried", si.update_tried, ", update_succeeded", si.update_succeeded)
175 // Restore setup_instruments rows to their previous settings (if changed previously).
176 func (si *SetupInstruments) RestoreConfiguration() {
177 lib.Logger.Println("RestoreConfiguration()")
178 // If the previous update didn't work then don't try to restore
179 if !si.update_succeeded {
180 lib.Logger.Println("Not restoring p_s.setup_instruments to original settings as initial configuration attempt failed")
183 lib.Logger.Println("Restoring p_s.setup_instruments to its original settings")
186 // update the rows which need to be set - do multiple updates but I don't care
187 update_sql := "UPDATE setup_instruments SET enabled = ?, TIMED = ? WHERE NAME = ?"
188 lib.Logger.Println("dbh.Prepare(", update_sql, ")")
189 stmt, err := si.dbh.Prepare(update_sql)
194 for i := range si.rows {
195 lib.Logger.Println("stmt.Exec(", si.rows[i].ENABLED, si.rows[i].TIMED, si.rows[i].NAME, ")")
196 if _, err := stmt.Exec(si.rows[i].ENABLED, si.rows[i].TIMED, si.rows[i].NAME); err != nil {
201 lib.Logger.Println("stmt.Close()")
203 lib.Logger.Println(count, "rows changed in p_s.setup_instruments")