-
Notifications
You must be signed in to change notification settings - Fork 8
/
mysql.go
180 lines (155 loc) · 4.98 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
package mysql
import (
"regexp"
"strconv"
"strings"
"time"
"github.com/devops-works/slowql/query"
"github.com/devops-works/slowql/server"
"github.com/sirupsen/logrus"
)
// Database holds database structure
type Database struct {
WaitingList chan query.Query
ServerMeta chan server.Server
stringInBrackets *regexp.Regexp
srv server.Server
}
// New instance of mysql database
func New(qc chan query.Query) *Database {
p := Database{
WaitingList: qc,
stringInBrackets: regexp.MustCompile(`\[(.*?)\]`),
}
return &p
}
// ParseBlocks parses query blocks
func (db *Database) ParseBlocks(rawBlocs chan []string) {
for {
select {
case bloc := <-rawBlocs:
db.WaitingList <- db.parseQuery(bloc)
}
}
}
func (db *Database) parseQuery(block []string) query.Query {
var q query.Query
for _, line := range block {
if line[0] == '#' {
db.parseMySQLHeader(line, &q)
} else {
if strings.HasSuffix(q.Query, ";") || q.Query == "" {
q.Query = q.Query + line
} else {
q.Query = q.Query + " " + line
}
}
}
return q
}
func (db *Database) parseMySQLHeader(line string, q *query.Query) {
var err error
parts := strings.Split(line, " ")
for idx, part := range parts {
part = strings.ToLower(part)
if strings.Contains(part, "query_time:") {
time := parts[idx+1]
q.QueryTime, err = strconv.ParseFloat(time, 64)
if err != nil {
logrus.Errorf("query_time: error converting %s to time: %s", parts[idx+1], err)
}
} else if strings.Contains(part, "lock_time:") {
time := parts[idx+1]
q.LockTime, err = strconv.ParseFloat(time, 64)
if err != nil {
logrus.Errorf("lock_time: error converting %s to time: %s", parts[idx+1], err)
}
} else if strings.Contains(part, "time:") {
date := parts[idx+1]
q.Time, err = time.Parse(time.RFC3339, date)
if err != nil {
logrus.Errorf("time: error converting %s to time: %s", parts[idx+1], err)
}
} else if strings.Contains(part, "rows_sent:") {
q.RowsSent, err = strconv.Atoi(parts[idx+1])
if err != nil {
logrus.Errorf("row_sent: error converting %s to int: %s", parts[idx+1], err)
}
} else if strings.Contains(part, "rows_examined:") {
q.RowsExamined, err = strconv.Atoi(parts[idx+1])
if err != nil {
logrus.Errorf("rows_examined: error converting %s to int: %s", parts[idx+1], err)
}
} else if strings.Contains(part, "rows_affected:") {
q.RowsAffected, err = strconv.Atoi(parts[idx+1])
if err != nil {
logrus.Errorf("rows_affected: error converting %s to int: %s", parts[idx+1], err)
}
} else if strings.Contains(part, "id:") {
// Some IDs can have multiple spaces, so we try to bruteforce the
// number of spaces. I tried implementing a version that keeps in
// memory the correct index after the first pass, but it was not
// faster that re-calculating it at each pass
item := ""
for item == "" {
idx++
item = parts[idx]
}
q.ID, err = strconv.Atoi(parts[idx])
if err != nil {
logrus.Errorf("id: error converting %s to int: %s", parts[idx+1], err)
}
} else if strings.Contains(part, "user@host:") {
items := db.stringInBrackets.FindAllString(line, -1)
// We remove first and last bytes of the strings because they are
// square brackets
q.User = items[0][1 : len(items[0])-1]
q.Host = items[1][1 : len(items[1])-1]
} else if strings.Contains(part, "schema:") {
q.Schema = parts[idx+1]
} else if strings.Contains(part, "last_errno:") {
q.LastErrNo, err = strconv.Atoi(parts[idx+1])
if err != nil {
logrus.Errorf("last_errno: error converting %s to int: %s", parts[idx+1], err)
}
} else if strings.Contains(part, "killed:") {
q.Killed, err = strconv.Atoi(parts[idx+1])
if err != nil {
logrus.Errorf("killed: error converting %s to int: %s", parts[idx+1], err)
}
} else if strings.Contains(part, "bytes_sent:") {
q.BytesSent, err = strconv.Atoi(parts[idx+1])
if err != nil {
logrus.Errorf("bytes_sent: error converting %s to int: %s", parts[idx+1], err)
}
}
}
}
// ParseServerMeta parses server meta information
func (db *Database) ParseServerMeta(lines chan []string) {
header := <-lines
versions := header[0]
net := header[1]
// Parse server information
versionre := regexp.MustCompile(`^([^,]+),\s+Version:\s+([0-9\.]+)([A-Za-z0-9-]+)\s+\((.*)\)\. started`)
matches := versionre.FindStringSubmatch(versions)
if len(matches) != 5 {
db.srv.Binary = "unable to parse line"
db.srv.VersionShort = db.srv.Binary
db.srv.Version = db.srv.Binary
db.srv.VersionDescription = db.srv.Binary
db.srv.Port = 0
db.srv.Socket = db.srv.Binary
} else {
db.srv.Binary = matches[1]
db.srv.VersionShort = matches[2]
db.srv.Version = db.srv.VersionShort + matches[3]
db.srv.VersionDescription = matches[4]
db.srv.Port, _ = strconv.Atoi(strings.Split(net, " ")[2])
db.srv.Socket = strings.TrimLeft(strings.Split(net, ":")[2], " ")
}
}
// GetServerMeta returns server meta information
func (db *Database) GetServerMeta() server.Server {
return db.srv
}