forked from open-falcon/falcon-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
54 lines (44 loc) · 1.17 KB
/
reader.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
package db
import (
"fmt"
"github.com/open-falcon/falcon-plus/modules/aggregator/g"
"log"
)
func ReadClusterMonitorItems() (M map[string]*g.Cluster, err error) {
M = make(map[string]*g.Cluster)
sql := "SELECT `id`, `grp_id`, `numerator`, `denominator`, `endpoint`, `metric`, `tags`, `ds_type`, `step`, `last_update` FROM `cluster`"
cfg := g.Config()
ids := cfg.Database.Ids
if len(ids) != 2 {
log.Fatalln("ids configuration error")
}
if ids[0] != -1 && ids[1] != -1 {
sql = fmt.Sprintf("%s WHERE `id` >= %d and `id` <= %d", sql, ids[0], ids[1])
} else {
if ids[0] != -1 {
sql = fmt.Sprintf("%s WHERE `id` >= %d", sql, ids[0])
}
if ids[1] != -1 {
sql = fmt.Sprintf("%s WHERE `id` <= %d", sql, ids[1])
}
}
if cfg.Debug {
log.Println(sql)
}
rows, err := DB.Query(sql)
if err != nil {
log.Println("[E]", err)
return M, err
}
defer rows.Close()
for rows.Next() {
var c g.Cluster
err = rows.Scan(&c.Id, &c.GroupId, &c.Numerator, &c.Denominator, &c.Endpoint, &c.Metric, &c.Tags, &c.DsType, &c.Step, &c.LastUpdate)
if err != nil {
log.Println("[E]", err)
continue
}
M[fmt.Sprintf("%d%v", c.Id, c.LastUpdate)] = &c
}
return M, err
}