forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
150 lines (125 loc) · 4.05 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
// Package config provides the winlogbeat specific configuration options.
package config
import (
"fmt"
"net"
"strconv"
"strings"
"time"
"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
}
// WinlogbeatConfig contains all of Winlogbeat configuration data.
type WinlogbeatConfig struct {
IgnoreOlder string `yaml:"ignore_older"`
EventLogs []EventLogConfig `yaml:"event_logs"`
Metrics MetricsConfig `yaml:"metrics"`
RegistryFile string `yaml:"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 _, err := IgnoreOlderDuration(ebc.IgnoreOlder); err != nil {
errs = append(errs, fmt.Errorf("Invalid top level ignore_older value "+
"'%s' (%v)", ebc.IgnoreOlder, err))
}
if len(ebc.EventLogs) == 0 {
errs = append(errs, fmt.Errorf("At least one event log must be "+
"configured as part of event_logs"))
}
for _, eventLog := range ebc.EventLogs {
if err := eventLog.Validate(); err != nil {
errs = append(errs, err)
}
}
if err := ebc.Metrics.Validate(); err != nil {
errs = append(errs, err)
}
return errs.Err()
}
// MetricsConfig holds the configuration data for the HTTP metric service.
type MetricsConfig struct {
BindAddress string // Bind address for the metric service. Format is host:port.
}
// Validate validates the MetricsConfig data and returns an error describing any
// problems or nil.
func (mc MetricsConfig) Validate() error {
if mc.BindAddress == "" {
return nil
}
host, portStr, err := net.SplitHostPort(mc.BindAddress)
if err != nil {
return fmt.Errorf("bind_address must be formatted as host:port but "+
"was '%s' (%v)", mc.BindAddress, err)
}
if len(host) == 0 {
return fmt.Errorf("bind_address value ('%s') is missing a host",
mc.BindAddress)
}
port, err := strconv.Atoi(portStr)
if err != nil {
return fmt.Errorf("bind_address port value ('%s') must be a number "+
"(%v)", portStr, err)
}
if port < 1 || port > 65535 {
return fmt.Errorf("bind_address port must be within [1-65535] but "+
"was '%d'", port)
}
return nil
}
// EventLogConfig holds the configuration data that specifies which event logs
// to monitor.
type EventLogConfig struct {
Name string
IgnoreOlder string `yaml:"ignore_older"`
API string
}
// Validate validates the EventLogConfig data and returns an error describing
// any problems or nil.
func (elc EventLogConfig) Validate() error {
var errs multierror.Errors
if elc.Name == "" {
err := fmt.Errorf("event log is missing a 'name'")
errs = append(errs, err)
}
if _, err := IgnoreOlderDuration(elc.IgnoreOlder); err != nil {
err := fmt.Errorf("Invalid ignore_older value ('%s') for event_log "+
"'%s' (%v)", elc.IgnoreOlder, elc.Name, err)
errs = append(errs, err)
}
switch strings.ToLower(elc.API) {
case "", "eventlogging", "wineventlog":
break
default:
err := fmt.Errorf("Invalid api value ('%s') for event_log '%s'",
elc.API, elc.Name)
errs = append(errs, err)
}
return errs.Err()
}
// IgnoreOlderDuration returns the parsed value of the IgnoreOlder string. If
// IgnoreOlder is not set then (0, nil) is returned. If IgnoreOlder is not
// parsable as a duration then an error is returned. See time.ParseDuration.
func IgnoreOlderDuration(ignoreOlder string) (time.Duration, error) {
if ignoreOlder == "" {
return time.Duration(0), nil
}
duration, err := time.ParseDuration(ignoreOlder)
return duration, err
}