forked from open-falcon/falcon-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.go
140 lines (118 loc) · 3.61 KB
/
cfg.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
package g
import (
"encoding/json"
"github.com/toolkits/file"
"log"
"strings"
"sync"
)
type HttpConfig struct {
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
}
type RpcConfig struct {
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
}
type SocketConfig struct {
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
Timeout int `json:"timeout"`
}
type JudgeConfig struct {
Enabled bool `json:"enabled"`
Batch int `json:"batch"`
ConnTimeout int `json:"connTimeout"`
CallTimeout int `json:"callTimeout"`
MaxConns int `json:"maxConns"`
MaxIdle int `json:"maxIdle"`
Replicas int `json:"replicas"`
Cluster map[string]string `json:"cluster"`
ClusterList map[string]*ClusterNode `json:"clusterList"`
}
type GraphConfig struct {
Enabled bool `json:"enabled"`
Batch int `json:"batch"`
ConnTimeout int `json:"connTimeout"`
CallTimeout int `json:"callTimeout"`
MaxConns int `json:"maxConns"`
MaxIdle int `json:"maxIdle"`
Replicas int `json:"replicas"`
Cluster map[string]string `json:"cluster"`
ClusterList map[string]*ClusterNode `json:"clusterList"`
}
type TsdbConfig struct {
Enabled bool `json:"enabled"`
Batch int `json:"batch"`
ConnTimeout int `json:"connTimeout"`
CallTimeout int `json:"callTimeout"`
MaxConns int `json:"maxConns"`
MaxIdle int `json:"maxIdle"`
MaxRetry int `json:"retry"`
Address string `json:"address"`
}
type GlobalConfig struct {
Debug bool `json:"debug"`
MinStep int `json:"minStep"` //最小周期,单位sec
Http *HttpConfig `json:"http"`
Rpc *RpcConfig `json:"rpc"`
Socket *SocketConfig `json:"socket"`
Judge *JudgeConfig `json:"judge"`
Graph *GraphConfig `json:"graph"`
Tsdb *TsdbConfig `json:"tsdb"`
}
var (
ConfigFile string
config *GlobalConfig
configLock = new(sync.RWMutex)
)
func Config() *GlobalConfig {
configLock.RLock()
defer configLock.RUnlock()
return config
}
func ParseConfig(cfg string) {
if cfg == "" {
log.Fatalln("use -c to specify configuration file")
}
if !file.IsExist(cfg) {
log.Fatalln("config file:", cfg, "is not existent. maybe you need `mv cfg.example.json cfg.json`")
}
ConfigFile = cfg
configContent, err := file.ToTrimString(cfg)
if err != nil {
log.Fatalln("read config file:", cfg, "fail:", err)
}
var c GlobalConfig
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("parse config file:", cfg, "fail:", err)
}
// split cluster config
c.Judge.ClusterList = formatClusterItems(c.Judge.Cluster)
c.Graph.ClusterList = formatClusterItems(c.Graph.Cluster)
configLock.Lock()
defer configLock.Unlock()
config = &c
log.Println("g.ParseConfig ok, file ", cfg)
}
// CLUSTER NODE
type ClusterNode struct {
Addrs []string `json:"addrs"`
}
func NewClusterNode(addrs []string) *ClusterNode {
return &ClusterNode{addrs}
}
// map["node"]="host1,host2" --> map["node"]=["host1", "host2"]
func formatClusterItems(cluster map[string]string) map[string]*ClusterNode {
ret := make(map[string]*ClusterNode)
for node, clusterStr := range cluster {
items := strings.Split(clusterStr, ",")
nitems := make([]string, 0)
for _, item := range items {
nitems = append(nitems, strings.TrimSpace(item))
}
ret[node] = NewClusterNode(nitems)
}
return ret
}