forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
113 lines (103 loc) · 3.49 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
package influxdb
import (
"errors"
"fmt"
"net/url"
"regexp"
"time"
"github.com/influxdata/influxdb/toml"
"github.com/influxdata/kapacitor/services/stats"
"github.com/influxdata/kapacitor/services/udp"
)
const (
// Maximum time to try and connect to InfluxDB during startup.
DefaultStartUpTimeout = 5 * time.Minute
DefaultSubscriptionSyncInterval = 1 * time.Minute
DefaultSubscriptionProtocol = "http"
)
type Config struct {
Enabled bool `toml:"enabled"`
Name string `toml:"name"`
Default bool `toml:"default"`
URLs []string `toml:"urls"`
Username string `toml:"username"`
Password string `toml:"password"`
// Path to CA file
SSLCA string `toml:"ssl-ca"`
// Path to host cert file
SSLCert string `toml:"ssl-cert"`
// Path to cert key file
SSLKey string `toml:"ssl-key"`
// Use SSL but skip chain & host verification
InsecureSkipVerify bool `toml:"insecure-skip-verify"`
Timeout toml.Duration `toml:"timeout"`
DisableSubscriptions bool `toml:"disable-subscriptions"`
SubscriptionProtocol string `toml:"subscription-protocol"`
Subscriptions map[string][]string `toml:"subscriptions"`
ExcludedSubscriptions map[string][]string `toml:"excluded-subscriptions"`
UDPBind string `toml:"udp-bind"`
UDPBuffer int `toml:"udp-buffer"`
UDPReadBuffer int `toml:"udp-read-buffer"`
StartUpTimeout toml.Duration `toml:"startup-timeout"`
SubscriptionSyncInterval toml.Duration `toml:"subscriptions-sync-interval"`
}
func NewConfig() Config {
return Config{
Enabled: true,
// Cannot initialize slice
// See: https://github.com/BurntSushi/toml/pull/68
//URLs: []string{"http://localhost:8086"},
Username: "",
Password: "",
Subscriptions: make(map[string][]string),
ExcludedSubscriptions: map[string][]string{
stats.DefaultDatabse: []string{stats.DefaultRetentionPolicy},
},
UDPBuffer: udp.DefaultBuffer,
StartUpTimeout: toml.Duration(DefaultStartUpTimeout),
SubscriptionProtocol: DefaultSubscriptionProtocol,
SubscriptionSyncInterval: toml.Duration(DefaultSubscriptionSyncInterval),
}
}
func (c *Config) SetDefaultValues() {
if c.UDPBuffer == 0 {
c.UDPBuffer = udp.DefaultBuffer
}
if c.StartUpTimeout == 0 {
c.StartUpTimeout = toml.Duration(DefaultStartUpTimeout)
}
if c.SubscriptionProtocol == "" {
c.SubscriptionProtocol = DefaultSubscriptionProtocol
}
if c.SubscriptionSyncInterval == toml.Duration(0) {
c.SubscriptionSyncInterval = toml.Duration(DefaultSubscriptionSyncInterval)
}
}
var validNamePattern = regexp.MustCompile(`^[-\._\p{L}0-9]+$`)
func (c Config) Validate() error {
if c.Name == "" {
return errors.New("influxdb cluster must be given a name")
}
if !validNamePattern.MatchString(c.Name) {
return errors.New("influxdb cluster name must contain only numbers, letters or '.', '-', and '_' characters")
}
if len(c.URLs) == 0 {
return errors.New("must specify at least one InfluxDB URL")
}
for _, u := range c.URLs {
_, err := url.Parse(u)
if err != nil {
return err
}
_, err = getTLSConfig(c.SSLCA, c.SSLCert, c.SSLKey, c.InsecureSkipVerify)
if err != nil {
return err
}
}
switch c.SubscriptionProtocol {
case "http", "https", "udp":
default:
return fmt.Errorf("invalid subscription protocol, must be one of 'udp', 'http' or 'https', got %s", c.SubscriptionProtocol)
}
return nil
}