-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (100 loc) · 2.51 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
package main
import (
"slices"
"strings"
"time"
"github.com/debeando/go-common/env"
"github.com/debeando/go-common/log"
"github.com/debeando/go-common/mysql"
)
func main() {
log.Info("Start DeBeAndo Agent for MySQL")
if getDebug() {
log.SetLevel(log.DebugLevel)
}
log.DebugWithFields("Environment Variables", log.Fields{
"DEBUG": getDebug(),
"DISABLE": getDisableList(),
"INFLUXDB_BUCKET": influxDB.Bucket,
"INFLUXDB_HOST": influxDB.Host,
"INFLUXDB_PORT": influxDB.Port,
"INFLUXDB_TOKEN": influxDB.Token,
"INTERVAL": getInterval(),
"MYSQL_HOST": MySQL.Host,
"MYSQL_PASSWORD": MySQL.Password,
"MYSQL_PORT": MySQL.Port,
"MYSQL_TIMEOUT": MySQL.Timeout,
"MYSQL_USER": MySQL.Username,
"SERVER": getServer(),
})
influxDB.New()
defer influxDB.Close()
MySQL.Connection = mysql.New(MySQL.Host, MySQL.DSN())
defer MySQL.Connection.Close()
for {
metrics := Metrics{}
MySQL.Connection.Connect()
for _, query := range Queries {
metric := Metric{}
if !query.IsTime(query.Interval) {
continue
}
if slices.Contains(getDisableList(), query.Name) {
log.DebugWithFields("Ignore metric collect", log.Fields{
"name": query.Name,
})
continue
}
MySQL.Connection.FetchAll(query.Beautifier(), func(row map[string]string) {
metric.Measurement = query.Name
metric.AddTag(Tag{
Name: "server",
Value: getServer(),
})
if query.UnPivot {
for column, value := range row {
if valueParsed, ok := mysql.ParseNumberValue(value); ok {
metric.AddField(Field{
Name: column,
Value: valueParsed,
})
} else {
metric.AddTag(Tag{
Name: column,
Value: value,
})
}
}
} else if valueParsed, ok := mysql.ParseNumberValue(row[query.Value]); ok {
metric.AddField(Field{
Name: row[query.Key],
Value: valueParsed,
})
}
})
metrics.Add(metric)
}
if metrics.Count() > 0 {
influxDB.Write(metrics)
}
metrics.Reset()
log.Debug("Wait until next collect metrics.")
time.Sleep(getInterval())
}
}
func getDebug() bool {
return env.GetBool("DEBUG", true)
}
func getInterval() time.Duration {
return time.Duration(env.GetInt("INTERVAL", 3)) * time.Second
}
func getServer() string {
return env.Get("SERVER", MySQL.Host)
}
func getDisableList() []string {
list := strings.Split(env.Get("DISABLE", ""), ",")
for item := range list {
list[item] = strings.TrimSpace(list[item])
}
return list
}