-
Notifications
You must be signed in to change notification settings - Fork 26
/
config.go
157 lines (137 loc) · 2.94 KB
/
config.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package goperfcounter
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
)
type GlobalConfig struct {
Debug bool `json:"debug"`
Hostname string `json:"hostname"`
Tags string `json:"tags"`
Step int64 `json:"step"`
Bases []string `json:"bases"`
Push *PushConfig `json:"push"`
Http *HttpConfig `json:"http"`
}
type HttpConfig struct {
Enabled bool `json:"enabled"`
Listen string `json:"listen"`
}
type PushConfig struct {
Enabled bool `json:"enabled"`
Api string `json:"api"`
}
var (
configFn = "./perfcounter.json"
defaultTags = ""
defaultStep = int64(60) //time in sec
defaultBases = []string{}
defaultPush = &PushConfig{Enabled: true, Api: "http://127.0.0.1:1988/v1/push"}
defaultHttp = &HttpConfig{Enabled: false, Listen: ""}
)
var (
cfg *GlobalConfig
cfgLock = new(sync.RWMutex)
)
//
func config() *GlobalConfig {
cfgLock.RLock()
defer cfgLock.RUnlock()
return cfg
}
func loadConfig() error {
if !isFileExist(configFn) {
return fmt.Errorf("config file not found: %s", configFn)
}
c, err := parseConfig(configFn)
if err != nil {
return err
}
updateConfig(c)
return nil
}
func setDefaultConfig() {
dcfg := defaultConfig()
updateConfig(dcfg)
}
func defaultConfig() GlobalConfig {
return GlobalConfig{
Debug: false,
Hostname: defaultHostname(),
Tags: defaultTags,
Step: defaultStep,
Bases: defaultBases,
Push: defaultPush,
Http: defaultHttp,
}
}
//
func updateConfig(c GlobalConfig) {
nc := formatConfig(c)
cfgLock.Lock()
defer cfgLock.Unlock()
cfg = &nc
}
func formatConfig(c GlobalConfig) GlobalConfig {
nc := c
if nc.Hostname == "" {
nc.Hostname = defaultHostname()
}
if nc.Step < 1 {
nc.Step = defaultStep
}
if nc.Tags != "" {
tagsOk := true
tagsSlice := strings.Split(nc.Tags, ",")
for _, tag := range tagsSlice {
kv := strings.Split(tag, "=")
if len(kv) != 2 || kv[0] == "name" { // name是保留tag
tagsOk = false
break
}
}
if !tagsOk {
nc.Tags = defaultTags
}
}
if nc.Push.Enabled && nc.Push.Api == "" {
nc.Push = defaultPush
}
if len(nc.Bases) < 1 {
nc.Bases = defaultBases
}
return nc
}
func parseConfig(cfg string) (GlobalConfig, error) {
var c GlobalConfig
if cfg == "" {
return c, fmt.Errorf("config file not found")
}
configContent, err := readFileString(cfg)
if err != nil {
return c, fmt.Errorf("read config file %s error: %v", cfg, err.Error())
}
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
return c, fmt.Errorf("parse config file %s error: %v", cfg, err.Error())
}
return c, nil
}
func defaultHostname() string {
hostname, _ := os.Hostname()
return hostname
}
func isFileExist(fn string) bool {
_, err := os.Stat(fn)
return err == nil || os.IsExist(err)
}
func readFileString(fn string) (string, error) {
b, err := ioutil.ReadFile(fn)
if err != nil {
return "", err
}
return strings.TrimSpace(string(b)), nil
}