master
go 60 lines 1.49 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package mysql
4
5 import "context"
6
7 const (
8 queryShowSessionVariables = `
9 SHOW SESSION VARIABLES
10 WHERE
11 Variable_name LIKE 'sql_log_off'
12 OR Variable_name LIKE 'slow_query_log';`
13 )
14
15 const (
16 queryDisableSessionQueryLog = "SET SESSION sql_log_off='ON';"
17 queryDisableSessionSlowQueryLog = "SET SESSION slow_query_log='OFF';"
18 )
19
20 func (c *Collector) disableSessionQueryLog(ctx context.Context) {
21 q := queryShowSessionVariables
22 c.Debugf("executing query: '%s'", q)
23
24 var sqlLogOff, slowQueryLog string
25 var name string
26 _, err := c.collectQuery(ctx, q, func(column, value string, _ bool) {
27 switch column {
28 case "Variable_name":
29 name = value
30 case "Value":
31 switch name {
32 case "sql_log_off":
33 sqlLogOff = value
34 case "slow_query_log":
35 slowQueryLog = value
36 }
37 }
38 })
39 if err != nil {
40 c.Debug(err)
41 return
42 }
43
44 if sqlLogOff == "OFF" && c.doDisableSessionQueryLog {
45 // requires SUPER privileges
46 q = queryDisableSessionQueryLog
47 c.Debugf("executing query: '%s'", q)
48 if _, err := c.collectQuery(ctx, q, func(_, _ string, _ bool) {}); err != nil {
49 c.Infof("failed to disable session query log (sql_log_off): %v", err)
50 c.doDisableSessionQueryLog = false
51 }
52 }
53 if slowQueryLog == "ON" {
54 q = queryDisableSessionSlowQueryLog
55 c.Debugf("executing query: '%s'", q)
56 if _, err := c.collectQuery(ctx, q, func(_, _ string, _ bool) {}); err != nil {
57 c.Debugf("failed to disable session slow query log (slow_query_log): %v", err)
58 }
59 }
60 }