forked from gaochao1/swcollector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customcfg.go
56 lines (46 loc) · 1.04 KB
/
customcfg.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
package g
import (
"encoding/json"
"log"
"sync"
"github.com/toolkits/file"
)
type MetricConfig struct {
IpRange []string `json:"ipRange"`
Metric string `json:metric`
Tag string `json:tag`
Type string `json:type`
Oid string `json:oid`
}
type CustomConfig struct {
Metrics []*MetricConfig `json:"metrics`
}
var (
CustConfigFile string
custconfig *CustomConfig
custlock = new(sync.RWMutex)
)
func CustConfig() *CustomConfig {
custlock.RLock()
defer custlock.RUnlock()
return custconfig
}
func ParseCustConfig(cfg string) {
if !file.IsExist(cfg) {
log.Fatalln("config file:", cfg, "is not existent")
}
CustConfigFile = cfg
configContent, err := file.ToTrimString(cfg)
if err != nil {
log.Fatalln("read config file:", cfg, "fail:", err)
}
var c CustomConfig
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("parse config file:", cfg, "fail:", err)
}
custlock.Lock()
defer custlock.Unlock()
custconfig = &c
log.Println("read customconfig file:", cfg, "successfully")
}