forked from dropbox/llama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
301 lines (263 loc) · 8.95 KB
/
Copy pathconfig.go
File metadata and controls
301 lines (263 loc) · 8.95 KB
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
package udprobe
import (
"fmt"
"net"
"gopkg.in/yaml.v2"
)
// A sensible default configuration for the collector in YAML
var defaultCollectorConfigYAML = `
summarization:
interval: 30
handlers: 2
api:
bind: 0.0.0.0:5200
src_hostname:
ports:
default:
ip: 0.0.0.0
port: 0
tos: 0
timeout: 1000
port_groups:
default:
- port: default
count: 4
rate_limits:
default:
cps: 4.0
tests:
- targets: default
port_group: default
rate_limit: default
targets:
default:
- ip: 127.0.0.1
port: 8100
tags: {}
`
// PortConfig describes the configuration for a single Port.
type PortConfig struct {
IP string `yaml:"ip"`
Port int64 `yaml:"port"`
Tos int64 `yaml:"tos"`
Timeout int64 `yaml:"timeout"`
}
// PortsConfig is a mapping of port "name" to a PortConfig.
type PortsConfig map[string]PortConfig
// Exists checks if a Port with the given name exists.
func (pc PortsConfig) Exists(name string) bool {
_, ok := pc[name]
return ok
}
// PortGroupConfig describes a set of identical Ports in a PortGroup.
type PortGroupConfig struct {
Port string `yaml:"port"` // Should correspond with a PortsConfig key
Count int64 `yaml:"count"`
}
// PortGroupsConfig is a mapping of port group "name" to PortGroupConfigs.
type PortGroupsConfig map[string][]PortGroupConfig
// Exists checks if a PortGroup with the given name exists.
func (pgc PortGroupsConfig) Exists(name string) bool {
_, ok := pgc[name]
return ok
}
// RateLimitConfig describes the configuration for a rate limiter.
type RateLimitConfig struct {
CPS float64 `yaml:"cps"` // Cycles per second
}
// RateLimitsConfig is a mapping of "name" to RateLimitConfig.
type RateLimitsConfig map[string]RateLimitConfig
// Exists checks if a RateLimit with the given name exists.
func (rlc RateLimitsConfig) Exists(name string) bool {
_, ok := rlc[name]
return ok
}
// TestConfig describes the elements of a test, for use by TestRunner, which
// correspond to their respective named elements in the config.
//
// Ex. A `targets` value of "default" in the config would correspond to a
// TargetsConfig key of "default" which contains the definitions of targets.
type TestConfig struct {
Targets string `yaml:"targets"` // Should correspond with a TargetsConfig key
PortGroup string `yaml:"port_group"` // Should correspond with a PortGroupsConfig key
RateLimit string `yaml:"rate_limit"` // Should correspond with a RateLimitsConfig key
}
// TestsConfig is a slice of TestConfig structs.
type TestsConfig []TestConfig
// TargetConfig describes a single target for testing, including tags that
// are applied to the resulting summaries.
type TargetConfig struct {
IP string `yaml:"ip"`
Port int64 `yaml:"port"`
Tags Tags `yaml:"tags"`
}
// AddrString converts the tc into a string formated "IP:port" combo.
func (tc *TargetConfig) AddrString() string {
str := fmt.Sprintf("%v:%v", tc.IP, tc.Port)
return str
}
// ResolveUDPAddr converts the tc into a net.UDPAddr pointer.
func (tc *TargetConfig) ResolveUDPAddr() (*net.UDPAddr, error) {
return net.ResolveUDPAddr("udp", tc.AddrString())
}
// TargetSet is a slice of TargetConfig structs.
type TargetSet []TargetConfig
// TagSet converts the ts into TagSet struct.
func (ts TargetSet) TagSet(srcHostname string) TagSet {
tagset := make(TagSet)
ts.IntoTagSet(tagset, srcHostname, "")
return tagset
}
// IntoTagSet is similar to TagSet but updates the provided tagset instead of
// creating a new one.
//
// If a tag key already exists with a different value, a warning is logged
// identifying the conflict. This can occur when the same IP appears in
// multiple TargetSets with different tag values for the same key.
func (ts TargetSet) IntoTagSet(tagset TagSet, srcHostname string, targetSetName string) {
for _, target := range ts {
key := target.IP
if tagset[key] == nil {
tagset[key] = make(Tags)
}
if srcHostname != "" && tagset[key]["src_hostname"] == "" {
tagset[key]["src_hostname"] = srcHostname
}
for k, v := range target.Tags {
// src_hostname is a special case: a per-target tag can override the
// global src_hostname parameter, which is intentional. Skip warning
// for this specific tag key.
if k == "src_hostname" {
tagset[key][k] = v
continue
}
if existing, ok := tagset[key][k]; ok && existing != v {
LogWarning(fmt.Sprintf("Tag conflict in TargetsConfig for IP %q: target set %q "+
"is overwriting tag %q (was %q, now %q)",
key, targetSetName, k, existing, v))
}
tagset[key][k] = v
}
}
}
// ListTargets provides a slice of "IP:port" string representations for all of
// the targets in the ts.
func (ts TargetSet) ListTargets() []string {
addrs := make([]string, 0)
for _, target := range ts {
addrs = append(addrs, target.AddrString())
}
return addrs
}
// ListResolvedTargets provides a slice of net.UDPAddr pointers for all of
// the targets in the ts, and will return with an error as soon as one is hit.
func (ts TargetSet) ListResolvedTargets() ([]*net.UDPAddr, error) {
addrs := make([]*net.UDPAddr, 0)
for _, target := range ts {
addr, err := target.ResolveUDPAddr()
if err != nil {
return addrs, err
}
addrs = append(addrs, addr)
}
return addrs, nil
}
// TargetsConfig is a mapping of "name" to TargetSet slice.
type TargetsConfig map[string]TargetSet
// Exists checks if a TargetSet with the given name exists.
func (tc TargetsConfig) Exists(name string) bool {
_, ok := tc[name]
return ok
}
// TagSet is a wrapper, and merges the TagSet output for all TargetSet slices
// within the tc.
//
// NOTE: If the same IP appears in multiple TargetSets with different tag values
// for the same key, a warning is logged and the last TargetSet's values win.
func (tc TargetsConfig) TagSet(srcHostname string) TagSet {
ts := make(TagSet)
tc.IntoTagSet(ts, srcHostname)
return ts
}
// IntoTagSet is a wrapper about the same function for each contained TargetSet
// and merges them into an existing ts.
func (tc TargetsConfig) IntoTagSet(ts TagSet, srcHostname string) {
for name, targetSet := range tc {
targetSet.IntoTagSet(ts, srcHostname, name)
}
}
// SummarizationConfig describes the parameters for setting up a Summarizer
// and related ResultHandlers.
type SummarizationConfig struct {
Interval int64 `yaml:"interval"`
Handlers int64 `yaml:"handlers"`
}
// APIConfig describes the parameters for the JSON HTTP API.
type APIConfig struct {
Bind string `yaml:"bind"`
}
// CollectorConfig wraps all of the above structs/maps/slices and defines the
// overall configuration for a collector.
type CollectorConfig struct {
Summarization SummarizationConfig `yaml:"summarization"`
API APIConfig `yaml:"api"`
SrcHostname string `yaml:"src_hostname"`
Ports PortsConfig `yaml:"ports"`
PortGroups PortGroupsConfig `yaml:"port_groups"`
RateLimits RateLimitsConfig `yaml:"rate_limits"`
Tests TestsConfig `yaml:"tests"`
Targets TargetsConfig `yaml:"targets"`
}
//
// Config Creators
//
// NewDefaultCollectorConfig provides a sensible default collector config.
func NewDefaultCollectorConfig() (*CollectorConfig, error) {
return NewCollectorConfig([]byte(defaultCollectorConfigYAML))
}
// NewCollectorConfig provides a parsed CollectorConfig based on the provided
// data.
//
// `data` is expected to be a byte slice version of a YAML CollectorConfig.
func NewCollectorConfig(data []byte) (*CollectorConfig, error) {
cc := &CollectorConfig{}
err := yaml.Unmarshal(data, cc)
if err != nil {
return cc, fmt.Errorf("failed to parse collector config: %s", err)
}
return cc, nil
}
// LegacyCollectorConfig is for backward compatibility with the existing UDProbe
// config and represents only a map of targets to tags.
type LegacyCollectorConfig map[string]map[string]string
// ToDefaultCollectorConfig converts a LegacyCollectorConfig to CollectorConfig
// by merging with the defaults and applying the provided port on targets.
func (legacy *LegacyCollectorConfig) ToDefaultCollectorConfig(port int64) (*CollectorConfig, error) {
cc, err := NewDefaultCollectorConfig()
if err != nil {
return cc, err
}
// Override the default targets
cc.Targets = make(TargetsConfig)
// Parse the targets from the old style into the new one.
// This requires the "default" options from above for now.
for addr, tags := range *legacy {
cc.Targets["default"] = append(cc.Targets["default"], TargetConfig{
IP: addr,
Port: port,
Tags: tags,
})
}
return cc, nil
}
// NewLegacyCollectorConfig creates a new LegacyCollectorConfig struct based
// on the provided data, which is expected to be a YAML representation of the
// config.
func NewLegacyCollectorConfig(data []byte) (*LegacyCollectorConfig, error) {
lcc := make(LegacyCollectorConfig)
err := yaml.Unmarshal(data, lcc)
if err != nil {
return &lcc, fmt.Errorf("failed to parse legacy collector config: %s", err)
}
return &lcc, nil
}