Sensibly format rows with table name on the right.
[pstop.git] / p_s / setup_instruments / setup_instruments.go
1 // Manage the configuration of setup_instruments.
2 package setup_instruments
3
4 import (
5         "database/sql"
6         "fmt"
7         "log"
8
9         "github.com/sjmudd/pstop/lib"
10 )
11
12 // constants
13 const sql_select = "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE ? AND 'YES NOT IN (enabled,timed)"
14
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{
19         "Error 1142:",
20         "Error 1290:",
21 }
22
23 // one row of performance_schema.setup_instruments
24 type table_row struct {
25         NAME    string
26         ENABLED string
27         TIMED   string
28 }
29
30 type table_rows []table_row
31
32 // SetupInstruments "object"
33 type SetupInstruments struct {
34         update_tried     bool
35         update_succeeded bool
36         rows             table_rows
37         dbh              *sql.DB
38 }
39
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}
44 }
45
46 // enable mutex and stage monitoring
47 func (si *SetupInstruments) EnableMonitoring() {
48         si.EnableMutexMonitoring()
49         si.EnableStageMonitoring()
50 }
51
52 // Change settings to monitor stage/sql/%
53 func (si *SetupInstruments) EnableStageMonitoring() {
54         lib.Logger.Println("EnableStageMonitoring")
55         sql_match := "stage/sql/%"
56         sql_select := "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE '" + sql_match + "' AND 'YES' NOT IN (enabled,timed)"
57
58         collecting := "Collecting setup_instruments stage/sql configuration settings"
59         updating := "Updating setup_instruments configuration for: stage/sql"
60
61         si.Configure(sql_select, collecting, updating)
62         lib.Logger.Println("EnableStageMonitoring finishes")
63 }
64
65 // Change settings to monitor wait/synch/mutex/%
66 func (si *SetupInstruments) EnableMutexMonitoring() {
67         lib.Logger.Println("EnableMutexMonitoring")
68         sql_match := "wait/synch/mutex/%"
69         sql_select := "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE '" + sql_match + "' AND 'YES' NOT IN (enabled,timed)"
70         collecting := "Collecting setup_instruments wait/synch/mutex configuration settings"
71         updating := "Updating setup_instruments configuration for: wait/synch/mutex"
72
73         si.Configure(sql_select, collecting, updating)
74         lib.Logger.Println("EnableMutexMonitoring finishes")
75 }
76
77 // return true if the error is not in the expected list
78 func error_in_expected_list(actual_error string, expected_errors []string) bool {
79         lib.Logger.Println("checking if", actual_error, "is in", expected_errors)
80         e := actual_error[0:11]
81         expected_error := false
82         for i := range expected_errors {
83                 if e == expected_errors[i] {
84                         lib.Logger.Println("found an expected error", expected_errors[i])
85                         expected_error = true
86                         break
87                 }
88         }
89         lib.Logger.Println("returning", expected_error)
90         return expected_error
91 }
92
93 // generic routine (now) to update some rows in setup instruments
94 func (si *SetupInstruments) Configure(sql_select string, collecting, updating string) {
95         lib.Logger.Println(fmt.Sprintf("Configure(%q,%q,%q)", sql_select, collecting, updating))
96         // skip if we've tried and failed
97         if si.update_tried && !si.update_succeeded {
98                 lib.Logger.Println("Configure() - Skipping further configuration")
99                 return
100         }
101
102         // setup the old values in case they're not set
103         if si.rows == nil {
104                 si.rows = make([]table_row, 0, 500)
105         }
106
107         lib.Logger.Println(collecting)
108
109         lib.Logger.Println("dbh.query", sql_select)
110         rows, err := si.dbh.Query(sql_select)
111         if err != nil {
112                 log.Fatal(err)
113         }
114
115         count := 0
116         for rows.Next() {
117                 var r table_row
118                 if err := rows.Scan(
119                         &r.NAME,
120                         &r.ENABLED,
121                         &r.TIMED); err != nil {
122                         log.Fatal(err)
123                 }
124                 si.rows = append(si.rows, r)
125                 count++
126         }
127         if err := rows.Err(); err != nil {
128                 log.Fatal(err)
129         }
130         rows.Close()
131         lib.Logger.Println("- found", count, "rows whose configuration need changing")
132
133         // update the rows which need to be set - do multiple updates but I don't care
134         lib.Logger.Println(updating)
135
136         const update_sql = "UPDATE setup_instruments SET enabled = ?, TIMED = ? WHERE NAME = ?"
137         lib.Logger.Println("Preparing statement:", update_sql)
138         si.update_tried = true
139         lib.Logger.Println("dbh.Prepare", update_sql)
140         stmt, err := si.dbh.Prepare(update_sql)
141         if err != nil {
142                 lib.Logger.Println("- prepare gave error:", err.Error())
143                 if !error_in_expected_list(err.Error(), EXPECTED_UPDATE_ERRORS) {
144                         log.Fatal("Not expected error so giving up")
145                 } else {
146                         lib.Logger.Println("- expected error so not running statement")
147                 }
148         } else {
149                 lib.Logger.Println("Prepare succeeded, trying to update", len(si.rows), "row(s)")
150                 count = 0
151                 for i := range si.rows {
152                         lib.Logger.Println("- changing row:", si.rows[i].NAME)
153                         lib.Logger.Println("stmt.Exec", "YES", "YES", si.rows[i].NAME)
154                         if res, err := stmt.Exec("YES", "YES", si.rows[i].NAME); err == nil {
155                                 lib.Logger.Println("update succeeded")
156                                 si.update_succeeded = true
157                                 c, _ := res.RowsAffected()
158                                 count += int(c)
159                         } else {
160                                 si.update_succeeded = false
161                                 if error_in_expected_list(err.Error(), EXPECTED_UPDATE_ERRORS) {
162                                         lib.Logger.Println("Insufficient privileges to UPDATE setup_instruments: " + err.Error())
163                                         lib.Logger.Println("Not attempting further updates")
164                                         return
165                                 } else {
166                                         log.Fatal(err)
167                                 }
168                         }
169                 }
170                 if si.update_succeeded {
171                         lib.Logger.Println(count, "rows changed in p_s.setup_instruments")
172                 }
173                 stmt.Close()
174         }
175         lib.Logger.Println("Configure() returns update_tried", si.update_tried, ", update_succeeded", si.update_succeeded)
176 }
177
178 // Restore setup_instruments rows to their previous settings (if changed previously).
179 func (si *SetupInstruments) RestoreConfiguration() {
180         lib.Logger.Println("RestoreConfiguration()")
181         // If the previous update didn't work then don't try to restore
182         if !si.update_succeeded {
183                 lib.Logger.Println("Not restoring p_s.setup_instruments to original settings as initial configuration attempt failed")
184                 return
185         } else {
186                 lib.Logger.Println("Restoring p_s.setup_instruments to its original settings")
187         }
188
189         // update the rows which need to be set - do multiple updates but I don't care
190         update_sql := "UPDATE setup_instruments SET enabled = ?, TIMED = ? WHERE NAME = ?"
191         lib.Logger.Println("dbh.Prepare(", update_sql, ")")
192         stmt, err := si.dbh.Prepare(update_sql)
193         if err != nil {
194                 log.Fatal(err)
195         }
196         count := 0
197         for i := range si.rows {
198                 lib.Logger.Println("stmt.Exec(", si.rows[i].ENABLED, si.rows[i].TIMED, si.rows[i].NAME, ")")
199                 if _, err := stmt.Exec(si.rows[i].ENABLED, si.rows[i].TIMED, si.rows[i].NAME); err != nil {
200                         log.Fatal(err)
201                 }
202                 count++
203         }
204         lib.Logger.Println("stmt.Close()")
205         stmt.Close()
206         lib.Logger.Println(count, "rows changed in p_s.setup_instruments")
207 }