forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
356 lines (317 loc) · 10.6 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
package server
import (
"errors"
"fmt"
"os"
"os/user"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
"github.com/influxdata/kapacitor/command"
"github.com/influxdata/kapacitor/services/alert"
"github.com/influxdata/kapacitor/services/alerta"
"github.com/influxdata/kapacitor/services/config"
"github.com/influxdata/kapacitor/services/deadman"
"github.com/influxdata/kapacitor/services/hipchat"
"github.com/influxdata/kapacitor/services/httpd"
"github.com/influxdata/kapacitor/services/influxdb"
"github.com/influxdata/kapacitor/services/k8s"
"github.com/influxdata/kapacitor/services/logging"
"github.com/influxdata/kapacitor/services/opsgenie"
"github.com/influxdata/kapacitor/services/pagerduty"
"github.com/influxdata/kapacitor/services/replay"
"github.com/influxdata/kapacitor/services/reporting"
"github.com/influxdata/kapacitor/services/sensu"
"github.com/influxdata/kapacitor/services/slack"
"github.com/influxdata/kapacitor/services/smtp"
"github.com/influxdata/kapacitor/services/snmptrap"
"github.com/influxdata/kapacitor/services/stats"
"github.com/influxdata/kapacitor/services/storage"
"github.com/influxdata/kapacitor/services/talk"
"github.com/influxdata/kapacitor/services/task_store"
"github.com/influxdata/kapacitor/services/telegram"
"github.com/influxdata/kapacitor/services/udf"
"github.com/influxdata/kapacitor/services/udp"
"github.com/influxdata/kapacitor/services/victorops"
"github.com/influxdata/influxdb/services/collectd"
"github.com/influxdata/influxdb/services/graphite"
"github.com/influxdata/influxdb/services/opentsdb"
)
// Config represents the configuration format for the kapacitord binary.
type Config struct {
HTTP httpd.Config `toml:"http"`
Replay replay.Config `toml:"replay"`
Storage storage.Config `toml:"storage"`
Task task_store.Config `toml:"task"`
InfluxDB []influxdb.Config `toml:"influxdb" override:"influxdb,element-key=name"`
Logging logging.Config `toml:"logging"`
ConfigOverride config.Config `toml:"config-override"`
Alert alert.Config `toml:"alert"`
// Input services
Graphites []graphite.Config `toml:"graphite"`
Collectd collectd.Config `toml:"collectd"`
OpenTSDB opentsdb.Config `toml:"opentsdb"`
UDPs []udp.Config `toml:"udp"`
// Alert handlers
Alerta alerta.Config `toml:"alerta" override:"alerta"`
HipChat hipchat.Config `toml:"hipchat" override:"hipchat"`
OpsGenie opsgenie.Config `toml:"opsgenie" override:"opsgenie"`
PagerDuty pagerduty.Config `toml:"pagerduty" override:"pagerduty"`
SMTP smtp.Config `toml:"smtp" override:"smtp"`
SNMPTrap snmptrap.Config `toml:"snmptrap" override:"snmptrap"`
Sensu sensu.Config `toml:"sensu" override:"sensu"`
Slack slack.Config `toml:"slack" override:"slack"`
Talk talk.Config `toml:"talk" override:"talk"`
Telegram telegram.Config `toml:"telegram" override:"telegram"`
VictorOps victorops.Config `toml:"victorops" override:"victorops"`
// Third-party integrations
Kubernetes k8s.Config `toml:"kubernetes" override:"kubernetes"`
Reporting reporting.Config `toml:"reporting"`
Stats stats.Config `toml:"stats"`
UDF udf.Config `toml:"udf"`
Deadman deadman.Config `toml:"deadman"`
Hostname string `toml:"hostname"`
DataDir string `toml:"data_dir"`
SkipConfigOverrides bool `toml:"skip-config-overrides"`
DefaultRetentionPolicy string `toml:"default-retention-policy"`
Commander command.Commander `toml:"-"`
}
// NewConfig returns an instance of Config with reasonable defaults.
func NewConfig() *Config {
c := &Config{
Hostname: "localhost",
Commander: command.ExecCommander,
}
c.HTTP = httpd.NewConfig()
c.Storage = storage.NewConfig()
c.Replay = replay.NewConfig()
c.Task = task_store.NewConfig()
c.InfluxDB = []influxdb.Config{influxdb.NewConfig()}
c.Logging = logging.NewConfig()
c.Kubernetes = k8s.NewConfig()
c.ConfigOverride = config.NewConfig()
c.Alert = alert.NewConfig()
c.Collectd = collectd.NewConfig()
c.OpenTSDB = opentsdb.NewConfig()
c.Alerta = alerta.NewConfig()
c.HipChat = hipchat.NewConfig()
c.OpsGenie = opsgenie.NewConfig()
c.PagerDuty = pagerduty.NewConfig()
c.SMTP = smtp.NewConfig()
c.Sensu = sensu.NewConfig()
c.Slack = slack.NewConfig()
c.Talk = talk.NewConfig()
c.SNMPTrap = snmptrap.NewConfig()
c.Telegram = telegram.NewConfig()
c.VictorOps = victorops.NewConfig()
c.Reporting = reporting.NewConfig()
c.Stats = stats.NewConfig()
c.UDF = udf.NewConfig()
c.Deadman = deadman.NewConfig()
return c
}
// NewDemoConfig returns the config that runs when no config is specified.
func NewDemoConfig() (*Config, error) {
c := NewConfig()
var homeDir string
// By default, store meta and data files in current users home directory
u, err := user.Current()
if err == nil {
homeDir = u.HomeDir
} else if os.Getenv("HOME") != "" {
homeDir = os.Getenv("HOME")
} else {
return nil, fmt.Errorf("failed to determine current user for storage")
}
c.Replay.Dir = filepath.Join(homeDir, ".kapacitor", c.Replay.Dir)
c.Task.Dir = filepath.Join(homeDir, ".kapacitor", c.Task.Dir)
c.Storage.BoltDBPath = filepath.Join(homeDir, ".kapacitor", c.Storage.BoltDBPath)
c.DataDir = filepath.Join(homeDir, ".kapacitor", c.DataDir)
return c, nil
}
// Validate returns an error if the config is invalid.
func (c *Config) Validate() error {
if c.Hostname == "" {
return fmt.Errorf("must configure valid hostname")
}
if c.DataDir == "" {
return fmt.Errorf("must configure valid data dir")
}
if err := c.Replay.Validate(); err != nil {
return err
}
if err := c.Storage.Validate(); err != nil {
return err
}
if err := c.HTTP.Validate(); err != nil {
return err
}
if err := c.Task.Validate(); err != nil {
return err
}
// Validate the set of InfluxDB configs.
// All names should be unique.
names := make(map[string]bool, len(c.InfluxDB))
// Should be exactly one default if at least one configs is enabled.
defaultInfluxDB := -1
numEnabled := 0
for i := range c.InfluxDB {
c.InfluxDB[i].ApplyConditionalDefaults()
config := c.InfluxDB[i]
if names[config.Name] {
return fmt.Errorf("duplicate name %q for influxdb configs", config.Name)
}
names[config.Name] = true
if err := config.Validate(); err != nil {
return err
}
if config.Enabled && config.Default {
if defaultInfluxDB != -1 {
return fmt.Errorf("More than one InfluxDB default was specified: %s %s", config.Name, c.InfluxDB[defaultInfluxDB].Name)
}
defaultInfluxDB = i
}
if config.Enabled {
numEnabled++
}
}
if numEnabled > 1 && defaultInfluxDB == -1 {
return errors.New("at least one of the enabled InfluxDB clusters must be marked as default.")
}
// Validate inputs
for _, g := range c.Graphites {
if err := g.Validate(); err != nil {
return fmt.Errorf("invalid graphite config: %v", err)
}
}
// Validate alert handlers
if err := c.Alerta.Validate(); err != nil {
return err
}
if err := c.HipChat.Validate(); err != nil {
return err
}
if err := c.OpsGenie.Validate(); err != nil {
return err
}
if err := c.PagerDuty.Validate(); err != nil {
return err
}
if err := c.SMTP.Validate(); err != nil {
return err
}
if err := c.SNMPTrap.Validate(); err != nil {
return err
}
if err := c.Sensu.Validate(); err != nil {
return err
}
if err := c.Slack.Validate(); err != nil {
return err
}
if err := c.Talk.Validate(); err != nil {
return err
}
if err := c.Telegram.Validate(); err != nil {
return err
}
if err := c.VictorOps.Validate(); err != nil {
return err
}
if err := c.UDF.Validate(); err != nil {
return err
}
return nil
}
func (c *Config) ApplyEnvOverrides() error {
return c.applyEnvOverrides("KAPACITOR", "", reflect.ValueOf(c))
}
func (c *Config) applyEnvOverrides(prefix string, fieldDesc string, spec reflect.Value) error {
// If we have a pointer, dereference it
s := spec
if spec.Kind() == reflect.Ptr {
s = spec.Elem()
}
var value string
if s.Kind() != reflect.Struct {
value = os.Getenv(prefix)
// Skip any fields we don't have a value to set
if value == "" {
return nil
}
if fieldDesc != "" {
fieldDesc = " to " + fieldDesc
}
}
switch s.Kind() {
case reflect.String:
s.SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var intValue int64
// Handle toml.Duration
if s.Type().Name() == "Duration" {
dur, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("failed to apply %v%v using type %v and value '%v'", prefix, fieldDesc, s.Type().String(), value)
}
intValue = dur.Nanoseconds()
} else {
var err error
intValue, err = strconv.ParseInt(value, 0, s.Type().Bits())
if err != nil {
return fmt.Errorf("failed to apply %v%v using type %v and value '%v'", prefix, fieldDesc, s.Type().String(), value)
}
}
s.SetInt(intValue)
case reflect.Bool:
boolValue, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("failed to apply %v%v using type %v and value '%v'", prefix, fieldDesc, s.Type().String(), value)
}
s.SetBool(boolValue)
case reflect.Float32, reflect.Float64:
floatValue, err := strconv.ParseFloat(value, s.Type().Bits())
if err != nil {
return fmt.Errorf("failed to apply %v%v using type %v and value '%v'", prefix, fieldDesc, s.Type().String(), value)
}
s.SetFloat(floatValue)
case reflect.Struct:
if err := c.applyEnvOverridesToStruct(prefix, s); err != nil {
return err
}
}
return nil
}
func (c *Config) applyEnvOverridesToStruct(prefix string, s reflect.Value) error {
typeOfSpec := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
// Get the toml tag to determine what env var name to use
configName := typeOfSpec.Field(i).Tag.Get("toml")
// Replace hyphens with underscores to avoid issues with shells
configName = strings.Replace(configName, "-", "_", -1)
fieldName := typeOfSpec.Field(i).Name
// Skip any fields that we cannot set
if f.CanSet() || f.Kind() == reflect.Slice {
// Use the upper-case prefix and toml name for the env var
key := strings.ToUpper(configName)
if prefix != "" {
key = strings.ToUpper(fmt.Sprintf("%s_%s", prefix, configName))
}
// If the type is s slice, apply to each using the index as a suffix
// e.g. GRAPHITE_0
if f.Kind() == reflect.Slice || f.Kind() == reflect.Array {
for i := 0; i < f.Len(); i++ {
if err := c.applyEnvOverrides(fmt.Sprintf("%s_%d", key, i), fieldName, f.Index(i)); err != nil {
return err
}
}
} else if err := c.applyEnvOverrides(key, fieldName, f); err != nil {
return err
}
}
}
return nil
}