forked from open-falcon/mymon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (160 loc) · 4.12 KB
/
main.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
/*
* Open-Falcon
*
* Copyright (c) 2014-2018 Xiaomi, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product may include a number of subcomponents with separate copyright notices
* and license terms. Your use of these subcomponents is subject to the terms and
* conditions of the subcomponent's license, as noted in the LICENSE file.
*/
package main
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/open-falcon/mymon/common"
"github.com/astaxie/beego/logs"
"github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/native"
)
// Global tag var
var (
IsSlave int
IsReadOnly int
Tag string
)
//Log logger of project
var Log *logs.BeeLogger
func main() {
// parse config file
var confFile string
flag.StringVar(&confFile, "c", "myMon.cfg", "myMon configure file")
version := flag.Bool("v", false, "show version")
flag.Parse()
if *version {
fmt.Println(fmt.Sprintf("%10s: %s", "Version", Version))
fmt.Println(fmt.Sprintf("%10s: %s", "Compile", Compile))
fmt.Println(fmt.Sprintf("%10s: %s", "Branch", Branch))
fmt.Println(fmt.Sprintf("%10s: %d", "GitDirty", GitDirty))
os.Exit(0)
}
conf, err := common.NewConfig(confFile)
if err != nil {
fmt.Printf("NewConfig Error: %s\n", err.Error())
return
}
if conf.Base.LogDir != "" {
err = os.MkdirAll(conf.Base.LogDir, 0755)
if err != nil {
fmt.Printf("MkdirAll Error: %s\n", err.Error())
return
}
}
if conf.Base.SnapshotDir != "" {
err = os.MkdirAll(conf.Base.SnapshotDir, 0755)
if err != nil {
fmt.Printf("MkdirAll Error: %s\n", err.Error())
return
}
}
// init log and other necessary
Log = common.MyNewLogger(conf, common.CompatibleLog(conf))
// auto scan mysql port
err, ports := scanMySqlPort()
if err != nil {
fmt.Printf("scan mysql port error %s", err.Error())
return
}
for _, portString := range ports {
port, _ := strconv.Atoi(portString)
conf.DataBase.Port = port
db, err := common.NewMySQLConnection(conf)
if err != nil {
fmt.Printf("NewMySQLConnection Error: %s\n", err.Error())
return
}
defer func() { _ = db.Close() }()
// start...
Log.Info("MySQL Monitor for falcon, port is %s", port)
go timeout()
err = fetchData(conf, db)
if err != nil && err != io.EOF {
Log.Error("Error: %s", err.Error())
}
}
}
func timeout() {
time.AfterFunc(TimeOut*time.Second, func() {
Log.Error("Execute timeout")
os.Exit(1)
})
}
func fetchData(conf *common.Config, db mysql.Conn) (err error) {
Log.Debug("start fetch data, port is %d", conf.DataBase.Port)
defer func() {
MySQLAlive(conf, err == nil)
}()
// Get GLOBAL variables
IsReadOnly, err = GetIsReadOnly(db)
if err != nil {
return
}
// SHOW XXX Metric
var data []*MetaData
// Get slave status and set IsSlave global var
slaveState, err := ShowSlaveStatus(conf, db)
if err != nil {
return
}
Tag = GetTag(conf)
globalStatus, err := ShowGlobalStatus(conf, db)
if err != nil {
return
}
data = append(data, globalStatus...)
globalVars, err := ShowGlobalVariables(conf, db)
if err != nil {
return
}
data = append(data, globalVars...)
innodbState, err := ShowInnodbStatus(conf, db)
if err != nil {
return
}
data = append(data, innodbState...)
data = append(data, slaveState...)
binaryLogStatus, err := ShowBinaryLogs(conf, db)
if err != nil {
return
}
data = append(data, binaryLogStatus...)
// Send Data to falcon-agent
msg, err := SendData(conf, data)
if err != nil {
Log.Error("Send response %s:%d - %s", conf.DataBase.Host, conf.DataBase.Port, string(msg))
} else {
Log.Info("Send response %s:%d - %s", conf.DataBase.Host, conf.DataBase.Port, string(msg))
}
err = ShowProcesslist(conf, db)
if err != nil {
return
}
return
}
func scanMySqlPort() (error, []string) {
cmd := exec.Command("/bin/sh", "-c", `netstat -ntupl |grep mysqld |grep -o ':[0-9]\+' |sed 's/:\+//g' |sort -k1n |uniq`)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
ports := strings.Split(strings.TrimSpace(out.String()), "\n")
return err, ports
}