forked from open-falcon/mymon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
innodbStatus.go
88 lines (80 loc) · 2.45 KB
/
innodbStatus.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
package main
import (
"regexp"
"strconv"
"strings"
"github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/native"
)
func innodbStatus(m *MysqlIns, db mysql.Conn) ([]*MetaData, error) {
status, _, err := db.QueryFirst("SHOW /*!50000 ENGINE */ INNODB STATUS")
if err != nil {
return nil, err
}
ctn := status.Str(2)
rows := strings.Split(ctn, "\n")
return parseInnodbStatus(m, rows)
}
func parseInnodbStatus(m *MysqlIns, rows []string) ([]*MetaData, error) {
var section string
data := make([]*MetaData, 0)
for _, row := range rows {
switch {
case match("^BACKGROUND THREAD$", row):
section = "BACKGROUND THREAD"
continue
case match("^DEAD LOCK ERRORS$", row), match("^LATEST DETECTED DEADLOCK$", row):
section = "DEAD LOCK ERRORS"
continue
case match("^FOREIGN KEY CONSTRAINT ERRORS$", row), match("^LATEST FOREIGN KEY ERROR$", row):
section = "FOREIGN KEY CONSTRAINT ERRORS"
continue
case match("^SEMAPHORES$", row):
section = "SEMAPHORES"
continue
case match("^TRANSACTIONS$", row):
section = "TRANSACTIONS"
continue
case match("^FILE I/O$", row):
section = "FILE I/O"
continue
case match("^INSERT BUFFER AND ADAPTIVE HASH INDEX$", row):
section = "INSERT BUFFER AND ADAPTIVE HASH INDEX"
continue
case match("^LOG$", row):
section = "LOG"
continue
case match("^BUFFER POOL AND MEMORY$", row):
section = "BUFFER POOL AND MEMORY"
continue
case match("^ROW OPERATIONS$", row):
section = "ROW OPERATIONS"
continue
}
if section == "SEMAPHORES" {
matches := regexp.MustCompile(`^Mutex spin waits\s+(\d+),\s+rounds\s+(\d+),\s+OS waits\s+(\d+)`).FindStringSubmatch(row)
if len(matches) == 4 {
spin_waits, _ := strconv.Atoi(matches[1])
Innodb_mutex_spin_waits := NewMetric("Innodb_mutex_spin_waits")
Innodb_mutex_spin_waits.SetValue(spin_waits)
data = append(data, Innodb_mutex_spin_waits)
spin_rounds, _ := strconv.Atoi(matches[2])
Innodb_mutex_spin_rounds := NewMetric("Innodb_mutex_spin_rounds")
Innodb_mutex_spin_rounds.SetValue(spin_rounds)
data = append(data, Innodb_mutex_spin_rounds)
os_waits, _ := strconv.Atoi(matches[3])
Innodb_mutex_os_waits := NewMetric("Innodb_mutex_os_waits")
Innodb_mutex_os_waits.SetValue(os_waits)
data = append(data, Innodb_mutex_os_waits)
}
}
}
return data, nil
}
func match(pattern, s string) bool {
matched, err := regexp.MatchString(pattern, s)
if err != nil {
return false
}
return matched
}