-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmysql.go
260 lines (210 loc) · 6.1 KB
/
mysql.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package snapshot
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"github.com/Notifiarr/notifiarr/pkg/mnd"
_ "github.com/go-sql-driver/mysql" // We use mysql driver, this is how it's loaded.
"golift.io/cnfg"
)
// MySQLConfig allows us to gather a process list for the snapshot.
type MySQLConfig struct {
Name string `json:"name" toml:"name" xml:"name"`
Host string `json:"host" toml:"host" xml:"host"`
User string `json:"-" toml:"user" xml:"user"`
Pass string `json:"-" toml:"pass" xml:"pass"`
Timeout cnfg.Duration `json:"timeout" toml:"timeout" xml:"timeout"`
// Only used by service checks, snapshot interval is used for mysql.
Interval cnfg.Duration `json:"interval" toml:"interval" xml:"interval"`
}
// MySQLProcesses allows us to manipulate our list with methods.
type MySQLProcesses []*MySQLProcess
// MySQLProcess represents the data returned from SHOW PROCESS LIST.
type MySQLProcess struct {
ID int64 `json:"id"`
User string `json:"user"`
Host string `json:"host"`
DB NullString `json:"db"`
Cmd string `json:"command"`
Time int64 `json:"time"`
State string `json:"state"`
Info NullString `json:"info"`
Progress float64 `json:"progress"` // mariadb
}
type NullString struct {
sql.NullString
}
type MySQLStatus map[string]interface{}
type MySQLServerData struct {
Name string `json:"name"`
Processes MySQLProcesses `json:"processes"`
GStatus MySQLStatus `json:"globalstatus"`
}
// MarshalJSON makes the output from sql.NullString not suck.
func (n NullString) MarshalJSON() ([]byte, error) {
if !n.Valid {
return []byte(`"NULL"`), nil
}
return json.Marshal(strings.TrimSpace(n.String)) //nolint:wrapcheck
}
// GetMySQL grabs the process list from a bunch of servers.
func (s *Snapshot) GetMySQL(ctx context.Context, servers []*MySQLConfig, limit int) []error {
s.MySQL = make(map[string]*MySQLServerData)
var errs []error
for _, server := range servers {
if server.Host == "" {
continue
}
procs, status, err := getMySQL(ctx, server)
if err != nil {
errs = append(errs, err)
}
s.MySQL[server.Host] = &MySQLServerData{
Name: server.Name,
Processes: procs,
GStatus: status,
}
}
for _, v := range s.MySQL {
sort.Sort(v.Processes)
v.Processes.Shrink(limit)
}
return errs
}
func getMySQL(ctx context.Context, mysql *MySQLConfig) (MySQLProcesses, MySQLStatus, error) {
hostID := mysql.Host
if mysql.Name != "" {
hostID = mysql.Name
}
host := "@tcp(" + mysql.Host + ")"
if strings.HasPrefix(mysql.Host, "@") {
host = mysql.Host
}
dbase, err := sql.Open("mysql", mysql.User+":"+mysql.Pass+host+"/")
if err != nil {
return nil, nil, fmt.Errorf("mysql server %s: connecting: %w", hostID, err)
}
defer dbase.Close()
list, err := scanMySQLProcessList(ctx, dbase)
if err != nil {
return list, nil, fmt.Errorf("mysql server %s: %w", hostID, err)
}
status, err := scanMySQLStatus(ctx, dbase)
if err != nil {
return list, nil, fmt.Errorf("mysql server %s: %w", hostID, err)
}
return list, status, nil
}
func scanMySQLProcessList(ctx context.Context, dbase *sql.DB) (MySQLProcesses, error) {
mnd.Apps.Add("MySQL&&Process List Queries", 1)
rows, err := dbase.QueryContext(ctx, "SHOW FULL PROCESSLIST") //nolint:execinquery
if err != nil {
mnd.Apps.Add("MySQL&&Errors", 1)
return nil, fmt.Errorf("getting processes: %w", err)
}
defer rows.Close()
if err = rows.Err(); err != nil {
mnd.Apps.Add("MySQL&&Errors", 1)
return nil, fmt.Errorf("getting processes rows: %w", err)
}
var list MySQLProcesses
const mysqlColCount = 8
for rows.Next() {
var pid MySQLProcess
// for each row, scan the result into our tag composite object.
if cols, _ := rows.Columns(); len(cols) == mysqlColCount {
// mysql only has 8 columns
err = rows.Scan(&pid.ID, &pid.User, &pid.Host, &pid.DB, &pid.Cmd, &pid.Time, &pid.State, &pid.Info)
} else {
// mariadb returns 9 columns (adds progress).
err = rows.Scan(&pid.ID, &pid.User, &pid.Host, &pid.DB, &pid.Cmd, &pid.Time, &pid.State, &pid.Info, &pid.Progress)
}
if err != nil {
mnd.Apps.Add("MySQL&&Errors", 1)
return nil, fmt.Errorf("scanning process rows: %w", err)
}
if pid.Info.Valid {
pid.Info.String = strings.Join(strings.Fields(pid.Info.String), " ")
}
list = append(list, &pid)
}
return list, nil
}
func scanMySQLStatus(ctx context.Context, dbase *sql.DB) (MySQLStatus, error) {
list := make(MySQLStatus)
for _, name := range []string{
"Aborted",
"Bytes",
"Connection",
"Created",
"Handler",
"Innodb",
"Key",
"Open",
"Q",
"Slow",
"Sort",
"Uptime",
"Table",
"Threads",
} {
if err := list.processStatus(ctx, dbase, name); err != nil {
return nil, err
}
}
return list, nil
}
func (m MySQLStatus) processStatus(ctx context.Context, dbase *sql.DB, name string) error {
mnd.Apps.Add("MySQL&&Global Status Queries", 1)
rows, err := dbase.QueryContext(ctx, "SHOW GLOBAL STATUS LIKE '"+name+"%'")
if err != nil {
mnd.Apps.Add("MySQL&&Errors", 1)
return fmt.Errorf("getting global status: %w", err)
}
defer rows.Close()
if err = rows.Err(); err != nil {
mnd.Apps.Add("MySQL&&Errors", 1)
return fmt.Errorf("getting global status rows: %w", err)
}
for rows.Next() {
var vname, value string
if err := rows.Scan(&vname, &value); err != nil {
mnd.Apps.Add("MySQL&&Errors", 1)
return fmt.Errorf("scanning global status rows: %w", err)
}
v, err := strconv.ParseFloat(value, mnd.Bits64)
if err != nil || v == 0 {
continue
}
m[vname] = v
}
return nil
}
// Len allows us to sort MySQLProcesses.
func (s MySQLProcesses) Len() int {
return len(s)
}
// Swap allows us to sort MySQLProcesses.
func (s MySQLProcesses) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Less allows us to sort MySQLProcesses.
func (s MySQLProcesses) Less(i, j int) bool {
return s[i].Time > s[j].Time
}
// Shrink a process list.
func (s *MySQLProcesses) Shrink(size int) {
if size == 0 {
size = defaultMyLimit
}
if s == nil {
return
}
if len(*s) > size {
*s = (*s)[:size]
}
}