forked from open-falcon/falcon-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.go
80 lines (70 loc) · 1.96 KB
/
strategy.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
package cron
import (
"encoding/json"
"fmt"
"github.com/open-falcon/falcon-plus/common/model"
"github.com/open-falcon/falcon-plus/modules/judge/g"
"log"
"time"
)
func SyncStrategies() {
duration := time.Duration(g.Config().Hbs.Interval) * time.Second
for {
syncStrategies()
syncExpression()
time.Sleep(duration)
}
}
func syncStrategies() {
var strategiesResponse model.StrategiesResponse
err := g.HbsClient.Call("Hbs.GetStrategies", model.NullRpcRequest{}, &strategiesResponse)
if err != nil {
log.Println("[ERROR] Hbs.GetStrategies:", err)
return
}
rebuildStrategyMap(&strategiesResponse)
}
func rebuildStrategyMap(strategiesResponse *model.StrategiesResponse) {
// endpoint:metric => [strategy1, strategy2 ...]
m := make(map[string][]model.Strategy)
for _, hs := range strategiesResponse.HostStrategies {
hostname := hs.Hostname
if g.Config().Debug && hostname == g.Config().DebugHost {
log.Println(hostname, "strategies:")
bs, _ := json.Marshal(hs.Strategies)
fmt.Println(string(bs))
}
for _, strategy := range hs.Strategies {
key := fmt.Sprintf("%s/%s", hostname, strategy.Metric)
if _, exists := m[key]; exists {
m[key] = append(m[key], strategy)
} else {
m[key] = []model.Strategy{strategy}
}
}
}
g.StrategyMap.ReInit(m)
}
func syncExpression() {
var expressionResponse model.ExpressionResponse
err := g.HbsClient.Call("Hbs.GetExpressions", model.NullRpcRequest{}, &expressionResponse)
if err != nil {
log.Println("[ERROR] Hbs.GetExpressions:", err)
return
}
rebuildExpressionMap(&expressionResponse)
}
func rebuildExpressionMap(expressionResponse *model.ExpressionResponse) {
m := make(map[string][]*model.Expression)
for _, exp := range expressionResponse.Expressions {
for k, v := range exp.Tags {
key := fmt.Sprintf("%s/%s=%s", exp.Metric, k, v)
if _, exists := m[key]; exists {
m[key] = append(m[key], exp)
} else {
m[key] = []*model.Expression{exp}
}
}
}
g.ExpressionMap.ReInit(m)
}