X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=p_s%2Fsetup_instruments%2Fsetup_instruments.go;h=3b260aa32cc9c42fa46fb057149c7dc267a2af46;hb=6d35dfbef03d201de80017ce8f10ec15744c2a2d;hp=595e75af0ee2fea0a71fb823346c98440448009f;hpb=d6ee7b071e88af51731e786b840e47840d100a77;p=pstop.git diff --git a/p_s/setup_instruments/setup_instruments.go b/p_s/setup_instruments/setup_instruments.go index 595e75a..3b260aa 100644 --- a/p_s/setup_instruments/setup_instruments.go +++ b/p_s/setup_instruments/setup_instruments.go @@ -1,3 +1,4 @@ +// manage the configuration of setup_instruments package setup_instruments import ( @@ -7,30 +8,59 @@ import ( "github.com/sjmudd/pstop/lib" ) -// Error 1142: UPDATE command denied to user -const UPDATE_FAILED = "Error 1142" +// We only match on the error number +// Error 1142: UPDATE command denied to user 'cacti'@'10.164.132.182' for table 'setup_instruments' +// Error 1290: The MySQL server is running with the --read-only option so it cannot execute this statement +var EXPECTED_UPDATE_ERRORS = []string{ + "Error 1142", + "Error 1290", +} -type setup_instruments_row struct { +// one row of performance_schema.setup_instruments +type table_row struct { NAME string ENABLED string TIMED string } +type table_rows []table_row + +// SetupInstruments "object" type SetupInstruments struct { update_succeeded bool - rows []setup_instruments_row + rows table_rows +} + +// Change settings to monitor stage/sql/% +func (si *SetupInstruments) EnableStageMonitoring(dbh *sql.DB) { + lib.Logger.Println("EnableStageMonitoring") + sql := "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE 'stage/sql/%' AND ( enabled <> 'YES' OR timed <> 'YES' )" + collecting := "Collecting setup_instruments stage/sql configuration settings" + updating := "Updating setup_instruments configuration for: stage/sql" + + si.ConfigureSetupInstruments(dbh, sql, collecting, updating) } // Change settings to monitor wait/synch/mutex/% func (si *SetupInstruments) EnableMutexMonitoring(dbh *sql.DB) { - si.rows = make([]setup_instruments_row, 0, 100) - - // populate the rows which are not set + lib.Logger.Println("EnableMutexMonitoring") sql := "SELECT NAME, ENABLED, TIMED FROM setup_instruments WHERE NAME LIKE 'wait/synch/mutex/%' AND ( enabled <> 'YES' OR timed <> 'YES' )" + collecting := "Collecting setup_instruments wait/synch/mutex configuration settings" + updating := "Updating setup_instruments configuration for: wait/synch/mutex" + + si.ConfigureSetupInstruments(dbh, sql, collecting, updating) +} - lib.Logger.Println("Collecting p_s.setup_instruments wait/synch/mutex configuration settings") +// generic routine (now) to update some rows in setup instruments +func (si *SetupInstruments) ConfigureSetupInstruments(dbh *sql.DB, select_sql string, collecting, updating string) { + // setup the old values in case they're not set + if si.rows == nil { + si.rows = make([]table_row, 0, 500) + } + + lib.Logger.Println(collecting) - rows, err := dbh.Query(sql) + rows, err := dbh.Query(select_sql) if err != nil { log.Fatal(err) } @@ -38,7 +68,7 @@ func (si *SetupInstruments) EnableMutexMonitoring(dbh *sql.DB) { count := 0 for rows.Next() { - var r setup_instruments_row + var r table_row if err := rows.Scan( &r.NAME, &r.ENABLED, @@ -55,32 +85,43 @@ func (si *SetupInstruments) EnableMutexMonitoring(dbh *sql.DB) { lib.Logger.Println("- found", count, "rows whose configuration need changing") // update the rows which need to be set - do multiple updates but I don't care - lib.Logger.Println("Updating p_s.setup_instruments to allow wait/synch/mutex configuration") + lib.Logger.Println(updating) + lib.Logger.Println("- about to try to update", len(si.rows), "row(s)" ) count = 0 for i := range si.rows { + lib.Logger.Println("- changing row:", si.rows[i].NAME ) sql := "UPDATE setup_instruments SET enabled = 'YES', TIMED = 'YES' WHERE NAME = '" + si.rows[i].NAME + "'" - if _, err := dbh.Exec(sql); err == nil { + lib.Logger.Println("exec statement:", sql ) + if res, err := dbh.Exec(sql); err == nil { si.update_succeeded = true + c, _ := res.RowsAffected() + count += int(c) } else { - if err.Error()[0:10] != UPDATE_FAILED { + found_expected := false + for i := range EXPECTED_UPDATE_ERRORS { + if err.Error()[0:10] == EXPECTED_UPDATE_ERRORS[i] { + found_expected = true + break + } + } + if !found_expected { log.Fatal(err) } + lib.Logger.Println("Insufficient privileges to UPDATE setup_instruments: " + err.Error()) + lib.Logger.Println("Not attempting further updates") break } - count++ } if si.update_succeeded { lib.Logger.Println(count, "rows changed in p_s.setup_instruments") - } else { - lib.Logger.Println( "Insufficient privileges to UPDATE setup_instruments: " . err.String() ) } } -// restore any changed rows back to their original state -func (si *SetupInstruments) Restore(dbh *sql.DB) { +// restore setup_instruments rows to their previous settings +func (si *SetupInstruments) RestoreConfiguration(dbh *sql.DB) { // If the previous update didn't work then don't try to restore - if ! si.update_succeeded { + if !si.update_succeeded { lib.Logger.Println("Not restoring p_s.setup_instruments to its original settings as previous UPDATE had failed") return } else { @@ -88,13 +129,18 @@ func (si *SetupInstruments) Restore(dbh *sql.DB) { } // update the rows which need to be set - do multiple updates but I don't care + update_sql := "UPDATE setup_instruments SET enabled = ?, TIMED = ? WHERE NAME = ?" + stmt, err := dbh.Prepare( update_sql ) + if err != nil { + log.Fatal(err) + } count := 0 for i := range si.rows { - sql := "UPDATE setup_instruments SET enabled = '" + si.rows[i].ENABLED + "', TIMED = '" + si.rows[i].TIMED + "' WHERE NAME = '" + si.rows[i].NAME + "'" - if _, err := dbh.Exec(sql); err != nil { + if _, err := stmt.Exec(si.rows[i].ENABLED, si.rows[i].TIMED, si.rows[i].NAME ); err != nil { log.Fatal(err) } count++ } + stmt.Close() lib.Logger.Println(count, "rows changed in p_s.setup_instruments") }