-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
config.go
89 lines (74 loc) · 2.35 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
// Package config provides the winlogbeat specific configuration options.
package config
import (
"fmt"
"sort"
"strings"
"github.com/joeshaw/multierror"
)
const (
// DefaultRegistryFile specifies the default filename of the registry file.
DefaultRegistryFile = ".winlogbeat.yml"
)
// Validator is the interface for configuration data that can be validating.
//
// Validate reads the configuration and validates all fields. An error
// describing all problems is returned (versus returning an error only for the
// first problem encountered).
type Validator interface {
Validate() error
}
// Settings is the root of the Winlogbeat configuration data hierarchy.
type Settings struct {
Winlogbeat WinlogbeatConfig `config:"winlogbeat"`
Raw map[string]interface{} `config:",inline"`
}
var (
DefaultSettings = Settings{
Winlogbeat: WinlogbeatConfig{
RegistryFile: DefaultRegistryFile,
},
}
)
// Validate validates the Settings data and returns an error describing
// all problems or nil if there are none.
func (s Settings) Validate() error {
// TODO: winlogbeat should not try to validate top-level beats config
validKeys := []string{
"fields", "fields_under_root", "tags", "name",
"queue_size", "bulk_queue_size", "max_procs",
"processors", "logging", "output", "path", "winlogbeat",
"dashboards",
}
sort.Strings(validKeys)
// Check for invalid top-level keys.
var errs multierror.Errors
for k := range s.Raw {
k = strings.ToLower(k)
i := sort.SearchStrings(validKeys, k)
if i >= len(validKeys) || validKeys[i] != k {
errs = append(errs, fmt.Errorf("Invalid top-level key '%s' "+
"found. Valid keys are %s", k, strings.Join(validKeys, ", ")))
}
}
err := s.Winlogbeat.Validate()
if err != nil {
errs = append(errs, err)
}
return errs.Err()
}
// WinlogbeatConfig contains all of Winlogbeat configuration data.
type WinlogbeatConfig struct {
EventLogs []map[string]interface{} `config:"event_logs"`
RegistryFile string `config:"registry_file"`
}
// Validate validates the WinlogbeatConfig data and returns an error describing
// all problems or nil if there are none.
func (ebc WinlogbeatConfig) Validate() error {
var errs multierror.Errors
if len(ebc.EventLogs) == 0 {
errs = append(errs, fmt.Errorf("At least one event log must be "+
"configured as part of event_logs"))
}
return errs.Err()
}