forked from open-falcon/mymon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpm.go
181 lines (151 loc) · 3.32 KB
/
mpm.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
// MySQL Performance Monitor(For open-falcon)
// Write by Li Bin<[email protected]>
package main
import (
"flag"
"fmt"
"os"
"time"
log "github.com/Sirupsen/logrus"
goconf "github.com/akrennmair/goconf"
"github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/native"
)
type Cfg struct {
LogFile string
LogLevel int
FalconClient string
User string
Pass string
Host string
Port int
}
var cfg Cfg
func init() {
var cfgFile string
flag.StringVar(&cfgFile, "c", "myMon.cfg", "myMon configure file")
flag.Parse()
if _, err := os.Stat(cfgFile); err != nil {
if os.IsNotExist(err) {
log.WithField("cfg", cfgFile).Fatalf("myMon config file does not exists: %v", err)
}
}
if err := cfg.readConf(cfgFile); err != nil {
log.Fatalf("Read configure file failed: %v", err)
}
// Init log file
log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.Level(cfg.LogLevel))
if cfg.LogFile != "" {
f, err := os.OpenFile(cfg.LogFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err == nil {
log.SetOutput(f)
return
}
}
log.SetOutput(os.Stderr)
}
func (conf *Cfg) readConf(file string) error {
c, err := goconf.ReadConfigFile(file)
if err != nil {
return err
}
conf.LogFile, err = c.GetString("default", "log_file")
if err != nil {
return err
}
conf.LogLevel, err = c.GetInt("default", "log_level")
if err != nil {
return err
}
conf.FalconClient, err = c.GetString("default", "falcon_client")
if err != nil {
return err
}
conf.User, err = c.GetString("mysql", "user")
if err != nil {
return err
}
conf.Pass, err = c.GetString("mysql", "password")
if err != nil {
return err
}
conf.Host, err = c.GetString("mysql", "host")
if err != nil {
return err
}
conf.Port, err = c.GetInt("mysql", "port")
return err
}
func timeout() {
time.AfterFunc(TIME_OUT*time.Second, func() {
log.Error("Execute timeout")
os.Exit(1)
})
}
func MysqlAlive(m *MysqlIns, ok bool) {
data := NewMetric("mysql_alive_local")
if ok {
data.SetValue(1)
}
msg, err := sendData([]*MetaData{data})
if err != nil {
log.Errorf("Send alive data failed: %v", err)
return
}
log.Infof("Alive data response %s: %s", m.String(), string(msg))
}
func FetchData(m *MysqlIns) (err error) {
defer func() {
MysqlAlive(m, err == nil)
}()
db := mysql.New("tcp", "", fmt.Sprintf("%s:%d", m.Host, m.Port),
cfg.User, cfg.Pass)
db.SetTimeout(500 * time.Millisecond)
if err = db.Connect(); err != nil {
return
}
defer db.Close()
data := make([]*MetaData, 0)
globalStatus, err := GlobalStatus(m, db)
if err != nil {
return
}
data = append(data, globalStatus...)
globalVars, err := GlobalVariables(m, db)
if err != nil {
return
}
data = append(data, globalVars...)
innodbState, err := innodbStatus(m, db)
if err != nil {
return
}
data = append(data, innodbState...)
slaveState, err := slaveStatus(m, db)
if err != nil {
return
}
data = append(data, slaveState...)
msg, err := sendData(data)
if err != nil {
return
}
log.Infof("Send response %s: %s", m.String(), string(msg))
return
}
func (m *MysqlIns) String() string {
return fmt.Sprintf("%s:%d", m.Host, m.Port)
}
func main() {
log.Info("MySQL Monitor for falcon")
go timeout()
err := FetchData(&MysqlIns{
Host: cfg.Host,
Port: cfg.Port,
Tag: fmt.Sprintf("port=%d", cfg.Port),
})
if err != nil {
log.Error(err)
}
}