forked from facesea/banshee
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
236 lines (221 loc) · 7.26 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2015 Eleme Inc. All rights reserved.
package config
import (
"encoding/json"
"github.com/eleme/banshee/util/log"
"io/ioutil"
)
// Measures
const (
// Time
Second uint32 = 1
Minute = 60 * Second
Hour = 60 * Minute
Day = 24 * Hour
)
// Defaults
const (
// Default time interval for all metrics in seconds.
DefaultInterval uint32 = 10 * Second
// Default hit limit to a rule in an interval
DefaultIntervalHitLimit int = 100
// Default period for all metrics in seconds.
DefaultPeriod uint32 = 1 * Day
// Default metric expiration.
DefaultExpiration uint32 = 7 * Day
// Default weight factor for moving average.
DefaultTrendingFactor float64 = 0.1
// Default filter offset to query history metrics.
DefaultFilterOffset float64 = 0.01
// Default cleaner interval.
DefaultCleanerInterval uint32 = 3 * Hour
// Default cleaner threshold.
DefaultCleanerThreshold uint32 = 3 * Day
// Default value of alerting interval.
DefaultAlerterInterval uint32 = 20 * Minute
// Default value of alert times limit in one day for the same metric
DefaultAlerterOneDayLimit uint32 = 5
// Default value of least count.
DefaultLeastCount uint32 = 5 * Minute / DefaultInterval
)
// Limitations
const (
// Max value for the number of DefaultThresholdMaxs.
MaxNumDefaultThresholdMaxs = 8
// Max value for the number of DefaultThresholdMins.
MaxNumDefaultThresholdMins = 8
// Max value for the number of FillBlankZeros.
MaxFillBlankZerosLen = 8
// Min value for the expiration to period.
MinExpirationNumToPeriod uint32 = 5
// Min value for the cleaner threshold to period.
MinCleanerThresholdNumToPeriod uint32 = 2
)
// Config is the configuration container.
type Config struct {
Interval uint32 `json:"interval"`
Period uint32 `json:"period"`
Expiration uint32 `json:"expiration"`
Storage configStorage `json:"storage"`
Detector configDetector `json:"detector"`
Webapp configWebapp `json:"webapp"`
Alerter configAlerter `json:"alerter"`
Cleaner configCleaner `json:"cleaner"`
}
type configStorage struct {
Path string `json:"path"`
}
type configDetector struct {
Port int `json:"port"`
TrendingFactor float64 `json:"trendingFactor"`
FilterOffset float64 `json:"filterOffset"`
LeastCount uint32 `json:"leastCount"`
BlackList []string `json:"blackList"`
IntervalHitLimit int `json:"intervalHitLimit"`
DefaultThresholdMaxs map[string]float64 `json:"defaultThresholdMaxs"`
DefaultThresholdMins map[string]float64 `json:"DefaultThresholdMins"`
FillBlankZeros []string `json:"fillBlankZeros"`
}
type configWebapp struct {
Port int `json:"port"`
Auth [2]string `json:"auth"`
Static string `json:"static"`
}
type configAlerter struct {
Command string `json:"command"`
Workers int `json:"workers"`
Interval uint32 `json:"inteval"`
OneDayLimit uint32 `json:"oneDayLimit"`
}
type configCleaner struct {
Interval uint32 `json:"interval"`
Threshold uint32 `json:"threshold"`
}
// New creates a Config with default values.
func New() *Config {
config := new(Config)
config.Interval = DefaultInterval
config.Period = DefaultPeriod
config.Expiration = DefaultExpiration
config.Storage.Path = "storage/"
config.Detector.Port = 2015
config.Detector.TrendingFactor = DefaultTrendingFactor
config.Detector.FilterOffset = DefaultFilterOffset
config.Detector.LeastCount = DefaultLeastCount
config.Detector.BlackList = []string{}
config.Detector.IntervalHitLimit = DefaultIntervalHitLimit
config.Detector.DefaultThresholdMaxs = make(map[string]float64, 0)
config.Detector.DefaultThresholdMins = make(map[string]float64, 0)
config.Detector.FillBlankZeros = []string{}
config.Webapp.Port = 2016
config.Webapp.Auth = [2]string{"admin", "admin"}
config.Webapp.Static = "static/dist"
config.Alerter.Command = ""
config.Alerter.Workers = 4
config.Alerter.Interval = DefaultAlerterInterval
config.Alerter.OneDayLimit = DefaultAlerterOneDayLimit
config.Cleaner.Interval = DefaultCleanerInterval
config.Cleaner.Threshold = DefaultCleanerThreshold
return config
}
// UpdateWithJSONFile update the config from a json file.
func (config *Config) UpdateWithJSONFile(fileName string) error {
log.Debug("read config from %s..", fileName)
b, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
err = json.Unmarshal(b, config)
if err != nil {
return err
}
return err
}
// Copy config.
func (config *Config) Copy() *Config {
c := New()
c.Interval = config.Interval
c.Period = config.Period
c.Expiration = config.Expiration
c.Storage.Path = config.Storage.Path
c.Detector.Port = config.Detector.Port
c.Detector.TrendingFactor = config.Detector.TrendingFactor
c.Detector.FilterOffset = config.Detector.FilterOffset
c.Detector.LeastCount = config.Detector.LeastCount
c.Detector.BlackList = config.Detector.BlackList
c.Detector.DefaultThresholdMaxs = config.Detector.DefaultThresholdMaxs
c.Detector.DefaultThresholdMins = config.Detector.DefaultThresholdMins
c.Detector.FillBlankZeros = config.Detector.FillBlankZeros
c.Detector.IntervalHitLimit = config.Detector.IntervalHitLimit
c.Webapp.Port = config.Webapp.Port
c.Webapp.Auth = config.Webapp.Auth
c.Webapp.Static = config.Webapp.Static
c.Alerter.Command = config.Alerter.Command
c.Alerter.Workers = config.Alerter.Workers
c.Alerter.Interval = config.Alerter.Interval
c.Alerter.OneDayLimit = config.Alerter.OneDayLimit
c.Cleaner.Interval = config.Cleaner.Interval
c.Cleaner.Threshold = config.Cleaner.Threshold
return c
}
// Validate config
func (config *Config) Validate() error {
// Globals
if config.Interval < 1*Second || config.Interval > 5*Minute {
return ErrInterval
}
if config.Interval > config.Period {
return ErrPeriod
}
if config.Expiration < config.Period*MinExpirationNumToPeriod {
return ErrExpiration
}
// Detector
if config.Detector.Port < 1 || config.Detector.Port > 65535 {
return ErrDetectorPort
}
if config.Detector.TrendingFactor <= 0 || config.Detector.TrendingFactor >= 1 {
return ErrDetectorTrendingFactor
}
if config.Detector.FilterOffset <= 0 || config.Detector.FilterOffset >= 1 {
return ErrDetectorFilterOffset
}
if len(config.Detector.DefaultThresholdMaxs) > MaxNumDefaultThresholdMaxs {
return ErrDetectorDefaultThresholdMaxsLen
}
if len(config.Detector.DefaultThresholdMins) > MaxNumDefaultThresholdMins {
return ErrDetectorDefaultThresholdMinsLen
}
for _, value := range config.Detector.DefaultThresholdMaxs {
if value == 0 {
return ErrDetectorDefaultThresholdMaxZero
}
}
for _, value := range config.Detector.DefaultThresholdMins {
if value == 0 {
return ErrDetectorDefaultThresholdMinZero
}
}
if len(config.Detector.FillBlankZeros) > MaxFillBlankZerosLen {
return ErrDetectorFillBlankZerosLen
}
// Webapp
if config.Webapp.Port < 1 || config.Webapp.Port > 65535 {
return ErrWebappPort
}
// Alerter
if len(config.Alerter.Command) == 0 {
return ErrAlerterCommandEmpty
}
if config.Alerter.Interval <= 0 {
return ErrAlerterInterval
}
if config.Alerter.OneDayLimit <= 0 {
return ErrAlerterOneDayLimit
}
// Cleaner
if config.Cleaner.Threshold < config.Period*MinCleanerThresholdNumToPeriod {
return ErrCleanerThreshold
}
return nil
}