forked from StephanU/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
191 lines (164 loc) · 5.47 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
package log
import (
"fmt"
"time"
"github.com/dustin/go-humanize"
cfg "github.com/elastic/beats/filebeat/config"
"github.com/elastic/beats/filebeat/harvester"
"github.com/elastic/beats/filebeat/harvester/reader"
"github.com/elastic/beats/filebeat/input/file"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/libbeat/common/match"
"github.com/elastic/beats/libbeat/logp"
)
var (
defaultConfig = config{
// Common
ForwarderConfig: harvester.ForwarderConfig{
Type: cfg.DefaultType,
},
CleanInactive: 0,
// Prospector
Enabled: true,
IgnoreOlder: 0,
ScanFrequency: 10 * time.Second,
CleanRemoved: true,
HarvesterLimit: 0,
Symlinks: false,
TailFiles: false,
ScanSort: "",
ScanOrder: "asc",
// Harvester
BufferSize: 16 * humanize.KiByte,
MaxBytes: 10 * humanize.MiByte,
LogConfig: LogConfig{
Backoff: 1 * time.Second,
BackoffFactor: 2,
MaxBackoff: 10 * time.Second,
CloseInactive: 5 * time.Minute,
CloseRemoved: true,
CloseRenamed: false,
CloseEOF: false,
CloseTimeout: 0,
},
}
)
type config struct {
harvester.ForwarderConfig `config:",inline"`
LogConfig `config:",inline"`
// Common
InputType string `config:"input_type"`
CleanInactive time.Duration `config:"clean_inactive" validate:"min=0"`
// Prospector
Enabled bool `config:"enabled"`
ExcludeFiles []match.Matcher `config:"exclude_files"`
IgnoreOlder time.Duration `config:"ignore_older"`
Paths []string `config:"paths"`
ScanFrequency time.Duration `config:"scan_frequency" validate:"min=0,nonzero"`
CleanRemoved bool `config:"clean_removed"`
HarvesterLimit uint64 `config:"harvester_limit" validate:"min=0"`
Symlinks bool `config:"symlinks"`
TailFiles bool `config:"tail_files"`
RecursiveGlob bool `config:"recursive_glob.enabled"`
// Harvester
BufferSize int `config:"harvester_buffer_size"`
Encoding string `config:"encoding"`
ScanOrder string `config:"scan.order"`
ScanSort string `config:"scan.sort"`
ExcludeLines []match.Matcher `config:"exclude_lines"`
IncludeLines []match.Matcher `config:"include_lines"`
MaxBytes int `config:"max_bytes" validate:"min=0,nonzero"`
Multiline *reader.MultilineConfig `config:"multiline"`
JSON *reader.JSONConfig `config:"json"`
}
type LogConfig struct {
Backoff time.Duration `config:"backoff" validate:"min=0,nonzero"`
BackoffFactor int `config:"backoff_factor" validate:"min=1"`
MaxBackoff time.Duration `config:"max_backoff" validate:"min=0,nonzero"`
CloseInactive time.Duration `config:"close_inactive"`
CloseRemoved bool `config:"close_removed"`
CloseRenamed bool `config:"close_renamed"`
CloseEOF bool `config:"close_eof"`
CloseTimeout time.Duration `config:"close_timeout" validate:"min=0"`
}
// Contains available scan options
const (
ScanOrderAsc = "asc"
ScanOrderDesc = "desc"
ScanSortNone = ""
ScanSortModtime = "modtime"
ScanSortFilename = "filename"
)
// ValidScanOrder of valid scan orders
var ValidScanOrder = map[string]struct{}{
ScanOrderAsc: {},
ScanOrderDesc: {},
}
// ValidScanOrder of valid scan orders
var ValidScanSort = map[string]struct{}{
ScanSortNone: {},
ScanSortModtime: {},
ScanSortFilename: {},
}
func (c *config) Validate() error {
// DEPRECATED 6.0.0: warning is already outputted on propsector level
if c.InputType != "" {
c.Type = c.InputType
}
// Prospector
if c.Type == harvester.LogType && len(c.Paths) == 0 {
return fmt.Errorf("No paths were defined for prospector")
}
if c.CleanInactive != 0 && c.IgnoreOlder == 0 {
return fmt.Errorf("ignore_older must be enabled when clean_inactive is used")
}
if c.CleanInactive != 0 && c.CleanInactive <= c.IgnoreOlder+c.ScanFrequency {
return fmt.Errorf("clean_inactive must be > ignore_older + scan_frequency to make sure only files which are not monitored anymore are removed")
}
// Harvester
// Check input type
if _, ok := harvester.ValidType[c.Type]; !ok {
return fmt.Errorf("Invalid input type: %v", c.Type)
}
if c.JSON != nil && len(c.JSON.MessageKey) == 0 &&
c.Multiline != nil {
return fmt.Errorf("When using the JSON decoder and multiline together, you need to specify a message_key value")
}
if c.JSON != nil && len(c.JSON.MessageKey) == 0 &&
(len(c.IncludeLines) > 0 || len(c.ExcludeLines) > 0) {
return fmt.Errorf("When using the JSON decoder and line filtering together, you need to specify a message_key value")
}
if c.ScanSort != "" {
cfgwarn.Experimental("scan_sort is used.")
// Check input type
if _, ok := ValidScanSort[c.ScanSort]; !ok {
return fmt.Errorf("Invalid scan sort: %v", c.ScanSort)
}
// Check input type
if _, ok := ValidScanOrder[c.ScanOrder]; !ok {
return fmt.Errorf("Invalid scan order: %v", c.ScanOrder)
}
}
return nil
}
func (c *config) resolvePaths() error {
var paths []string
if !c.RecursiveGlob {
logp.Debug("prospector", "recursive glob disabled")
paths = c.Paths
} else {
logp.Debug("prospector", "recursive glob enabled")
}
for _, path := range c.Paths {
patterns, err := file.GlobPatterns(path, recursiveGlobDepth)
if err != nil {
return err
}
if len(patterns) > 1 {
logp.Debug("prospector", "%q expanded to %#v", path, patterns)
}
paths = append(paths, patterns...)
}
c.Paths = paths
return nil
}