/
config.go
379 lines (344 loc) · 11.2 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//
// Copyright 2015 Gregory Trubetskoy. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package daemon
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/tgres/tgres/misc"
"github.com/tgres/tgres/rrd"
"github.com/tgres/tgres/serde"
)
type Config struct { // Needs to be exported for TOML to work
PidPath string `toml:"pid-file"`
LogPath string `toml:"log-file"`
LogCycle duration `toml:"log-cycle-interval"`
DbConnectString string `toml:"db-connect-string"`
PgSegmentWidth int `toml:"pg-segment-width"`
MinStep duration `toml:"min-step"`
MaxReceiverQueueSize int `toml:"max-receiver-queue-size"`
MaxMemoryBytes int `toml:"max-memory-bytes"`
GraphiteTextListenSpec string `toml:"graphite-text-listen-spec"`
GraphiteUdpListenSpec string `toml:"graphite-udp-listen-spec"`
GraphitePickleListenSpec string `toml:"graphite-pickle-listen-spec"`
StatsdTextListenSpec string `toml:"statsd-text-listen-spec"`
StatsdUdpListenSpec string `toml:"statsd-udp-listen-spec"`
HttpListenSpec string `toml:"http-listen-spec"`
HttpAllowOrigin string `toml:"http-allow-origin"`
QueryCacheSize int `toml:"query-cache-size"`
Workers int
DSs []ConfigDSSpec `toml:"ds"`
StatFlush duration `toml:"stat-flush-interval"`
StatsNamePrefix string `toml:"stats-name-prefix"`
}
type regex struct{ *regexp.Regexp }
func (r *regex) UnmarshalText(text []byte) (err error) {
r.Regexp, err = regexp.Compile(string(text))
return err
}
type duration struct{ time.Duration }
func (d *duration) UnmarshalText(text []byte) (err error) {
d.Duration, err = time.ParseDuration(string(text))
return err
}
// Needs to be exported for TOML
type ConfigDSSpec struct {
Regexp regex
Step duration
Heartbeat duration
RRAs []ConfigRRASpec
}
type ConfigRRASpec struct {
Function rrd.Consolidation
Step time.Duration
Span time.Duration
Xff float64
}
func (r *ConfigRRASpec) UnmarshalText(text []byte) error {
r.Xff = 0.5
parts := strings.SplitN(string(text), ":", 4)
if len(parts) < 2 || len(parts) > 4 {
return fmt.Errorf("Invalid RRA specification (not enough or too many elements): %q", string(text))
}
// If first character of first part is a digit, assume we're
// skipping CF and default to WMEAN.
if len(parts[0]) > 0 && strings.Contains("0123456789", string(parts[0][0])) {
parts = append([]string{"WMEAN"}, parts...)
}
switch strings.ToUpper(parts[0]) {
case "WMEAN":
r.Function = rrd.WMEAN
case "MIN":
r.Function = rrd.MIN
case "MAX":
r.Function = rrd.MAX
case "LAST":
r.Function = rrd.LAST
default:
return fmt.Errorf("Invalid consolidation: %q (valid funcs: wmean, min, max, last)", parts[0])
}
var err error
if r.Step, err = misc.BetterParseDuration(parts[1]); err != nil {
return fmt.Errorf("Invalid Step: %q (%v)", parts[1], err)
}
if r.Span, err = misc.BetterParseDuration(parts[2]); err != nil {
return fmt.Errorf("Invalid Size: %q (%v)", parts[2], err)
}
if (r.Span.Nanoseconds() % r.Step.Nanoseconds()) != 0 {
newSpan := time.Duration(r.Span.Nanoseconds()/r.Step.Nanoseconds()*r.Step.Nanoseconds()) * time.Nanosecond
log.Printf("Span (%q) is not a multiple of step (%q), auto adjusting span to %v.", parts[2], parts[1], newSpan)
r.Span = newSpan
if newSpan.Nanoseconds() == 0 {
return fmt.Errorf("invalid Size (%v)", newSpan)
}
}
if len(parts) == 4 {
var err error
if r.Xff, err = strconv.ParseFloat(parts[3], 64); err != nil {
return fmt.Errorf("Invalid XFF: %q (%v)", parts[3], err)
}
}
return nil
}
var readConfig = func(cfgPath string) (*Config, error) {
cfg := &Config{}
_, err := toml.DecodeFile(cfgPath, cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
func (c *Config) processConfigPidFile(wd string) error {
if c.PidPath == "" {
return fmt.Errorf("pid-file setting empty")
}
if !filepath.IsAbs(c.PidPath) {
if wd == "" {
return fmt.Errorf("pid-file must be absolute path if working directory cannot be determined")
}
c.PidPath = filepath.Join(wd, c.PidPath)
}
pidDir, _ := filepath.Split(c.PidPath)
if err := os.MkdirAll(pidDir, 0755); err != nil {
return errors.New(fmt.Sprintf("Unable to create directory: '%s' (%v).", pidDir, err))
}
return nil
}
func (c *Config) processConfigLogFile(wd string) error {
if os.Getenv("TGRES_LOG") != "" {
c.LogPath = os.Getenv("TGRES_LOG")
}
if c.LogPath == "" {
return fmt.Errorf("log-file setting empty")
}
if !filepath.IsAbs(c.LogPath) {
if wd == "" {
return fmt.Errorf("log-file must be absolute path if working directory cannot be determined")
}
c.LogPath = filepath.Join(wd, c.LogPath)
}
logDir, _ := filepath.Split(c.LogPath)
if err := os.MkdirAll(logDir, 0755); err != nil {
return errors.New(fmt.Sprintf("Unable to create directory: '%s' (%v).", logDir, err))
}
log.Printf("Logs will be written to '%s'.", c.LogPath)
return nil
}
func (c *Config) processConfigLogCycleInterval() error {
if c.LogCycle.Duration == 0 {
return fmt.Errorf("log-cycle-interval setting empty")
}
log.Printf("Will cycle logs every %v (log-cycle-interval).", c.LogCycle.Duration)
logDir, _ := filepath.Split(c.LogPath)
log.Printf("All further status messages will be written to log file(s) in '%s'.", logDir)
logFileCycler(c.LogPath, c.LogCycle.Duration)
log.Print("Server starting.")
return nil
}
func (c *Config) processDbConnectString() error {
if os.Getenv("TGRES_DB_CONNECT") != "" {
c.DbConnectString = os.Getenv("TGRES_DB_CONNECT")
}
if c.DbConnectString == "" {
return fmt.Errorf("db-connect-string empty")
}
return nil
}
func (c *Config) processMinStep() error {
if c.MinStep.Duration == 0 {
return fmt.Errorf("min-step is missing")
} else {
log.Printf("Smallest step allowed: %v (min-step).", c.MinStep.Duration)
}
return nil
}
func (c *Config) processMaxReceiverQueueSize() error {
if c.MaxReceiverQueueSize == 0 {
log.Printf("max-receiver-queue-size unspecified, defaults to 0 (unlimited)")
} else if c.MaxReceiverQueueSize <= 0 {
log.Printf("Receiver Queue Size is unlimited (%d) (max-receiver-queue-size).", c.MaxReceiverQueueSize)
} else {
log.Printf("Receiver Queue Size is limited to %d (max-receiver-queue-size).", c.MaxReceiverQueueSize)
}
return nil
}
func (c *Config) processMaxMemoryBytes() error {
if c.MaxMemoryBytes == 0 {
log.Printf("max-memory-bytes unspecified, defaults to 0 (unlimited)")
} else if c.MaxMemoryBytes <= 0 {
log.Printf("Max Memory (heap allocation bytes) is unlimited (%d) (max-memory-bytes).", c.MaxMemoryBytes)
} else {
log.Printf("Max Memory (heap allocation bytes) is limited to %d (max-memory-bytes).", c.MaxMemoryBytes)
}
return nil
}
func (c *Config) processPgSegmentWidth() error {
if c.PgSegmentWidth == 0 {
// do nothing and keep quiet about it since this is an "advanced" setting
} else if c.PgSegmentWidth <= 0 {
return fmt.Errorf("Invalid pg-segment-width: %d", c.PgSegmentWidth)
} else {
log.Printf("PG Segment Width is %d (pg-segment-width).", c.PgSegmentWidth)
serde.PgSegmentWidth = c.PgSegmentWidth
}
return nil
}
func (c *Config) processStatFlushInterval() error {
if c.StatFlush.Duration == 0 {
return fmt.Errorf("stat-flush-interval is missing")
} else {
log.Printf("Stats (a la statsd) will be flushed every %v (stat-flush-interval).", c.StatFlush.Duration)
}
return nil
}
func (c *Config) processStatsNamePrefix() error {
if c.StatsNamePrefix == "" {
log.Printf("stats-name-prefix is empty, defaulting to 'stats'")
c.StatsNamePrefix = "stats"
}
return nil
}
func (c *Config) processWorkers() error {
if c.Workers == 0 {
return fmt.Errorf("workers missing, must be an integer")
}
log.Printf("Number of workers (and flushers) will be %d.", c.Workers)
return nil
}
func (c *Config) processDSSpec() error {
// TODO validate function, regular expression, all that
for _, ds := range c.DSs {
for _, rra := range ds.RRAs {
if (rra.Step.Nanoseconds() % c.MinStep.Nanoseconds()) != 0 {
return fmt.Errorf("DS %q: invalid Step (%v), must be one or multiple min-step (%v).", ds.Regexp.String(), rra.Step, c.MinStep)
}
if (rra.Step.Nanoseconds() % ds.Step.Duration.Nanoseconds()) != 0 {
newStep := time.Duration(rra.Step.Nanoseconds()/ds.Step.Duration.Nanoseconds()*ds.Step.Duration.Nanoseconds()) * time.Nanosecond
log.Printf("DS %q: RRA step (%v) is not a multiple of DS Step (%v), auto adjusting Step to %v.", ds.Regexp.String(), rra.Step, ds.Step.Duration, newStep)
if newStep.Nanoseconds() == 0 {
return fmt.Errorf("DS %q: invalid Step (%v)", ds.Regexp.String(), newStep)
}
rra.Step = newStep
}
}
}
// TODO xff?
return nil
}
func (c *Config) FindMatchingDSSpec(ident serde.Ident) *rrd.DSSpec {
for _, dsSpec := range c.DSs {
name := ident["name"]
if dsSpec.Regexp.Regexp.MatchString(name) {
return convertDSSpec(&dsSpec)
}
}
return nil
}
func convertDSSpec(dsSpec *ConfigDSSpec) *rrd.DSSpec {
serdeDSSpec := &rrd.DSSpec{
Step: dsSpec.Step.Duration,
Heartbeat: dsSpec.Heartbeat.Duration,
RRAs: make([]rrd.RRASpec, len(dsSpec.RRAs)),
}
for i, r := range dsSpec.RRAs {
serdeDSSpec.RRAs[i] = rrd.RRASpec{
Function: r.Function,
Step: r.Step,
Span: r.Span,
Xff: float32(r.Xff),
}
}
return serdeDSSpec
}
type configer interface {
processConfigPidFile(string) error
processConfigLogFile(string) error
processConfigLogCycleInterval() error
processDbConnectString() error
processMinStep() error
processMaxReceiverQueueSize() error
processMaxMemoryBytes() error
processPgSegmentWidth() error
processStatFlushInterval() error
processStatsNamePrefix() error
processWorkers() error
processDSSpec() error
}
var processConfig = func(c configer, wd string) error {
if err := c.processConfigPidFile(wd); err != nil {
return err
}
if err := c.processConfigLogFile(wd); err != nil {
return err
}
if err := c.processConfigLogCycleInterval(); err != nil {
return err
}
if err := c.processDbConnectString(); err != nil {
return err
}
if err := c.processMinStep(); err != nil {
return err
}
if err := c.processMaxReceiverQueueSize(); err != nil {
return err
}
if err := c.processMaxMemoryBytes(); err != nil {
return err
}
if err := c.processPgSegmentWidth(); err != nil {
return err
}
if err := c.processStatFlushInterval(); err != nil {
return err
}
if err := c.processStatsNamePrefix(); err != nil {
return err
}
if err := c.processWorkers(); err != nil {
return err
}
if err := c.processDSSpec(); err != nil {
return err
}
return nil
}