summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/mysql/disable_logging.go
blob: 3a2eea6a12bea25e3095e5c81af35c13790915c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: GPL-3.0-or-later

package mysql

const (
	queryShowSessionVariables = `
SHOW SESSION VARIABLES 
WHERE 
  Variable_name LIKE 'sql_log_off'
  OR Variable_name LIKE 'slow_query_log';`
)

const (
	queryDisableSessionQueryLog     = "SET SESSION sql_log_off='ON';"
	queryDisableSessionSlowQueryLog = "SET SESSION slow_query_log='OFF';"
)

func (m *MySQL) disableSessionQueryLog() {
	q := queryShowSessionVariables
	m.Debugf("executing query: '%s'", q)

	var sqlLogOff, slowQueryLog string
	var name string
	_, err := m.collectQuery(q, func(column, value string, _ bool) {
		switch column {
		case "Variable_name":
			name = value
		case "Value":
			switch name {
			case "sql_log_off":
				sqlLogOff = value
			case "slow_query_log":
				slowQueryLog = value
			}
		}
	})
	if err != nil {
		m.Debug(err)
		return
	}

	if sqlLogOff == "OFF" && m.doDisableSessionQueryLog {
		// requires SUPER privileges
		q = queryDisableSessionQueryLog
		m.Debugf("executing query: '%s'", q)
		if _, err := m.collectQuery(q, func(_, _ string, _ bool) {}); err != nil {
			m.Infof("failed to disable session query log (sql_log_off): %v", err)
			m.doDisableSessionQueryLog = false
		}
	}
	if slowQueryLog == "ON" {
		q = queryDisableSessionSlowQueryLog
		m.Debugf("executing query: '%s'", q)
		if _, err := m.collectQuery(q, func(_, _ string, _ bool) {}); err != nil {
			m.Debugf("failed to disable session slow query log (slow_query_log): %v", err)
		}
	}
}