forked from StephanU/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
292 lines (246 loc) · 7.41 KB
/
log.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
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
package harvester
import (
"errors"
"fmt"
"io"
"os"
"github.com/elastic/beats/filebeat/config"
"github.com/elastic/beats/filebeat/harvester/encoding"
"github.com/elastic/beats/filebeat/harvester/processor"
"github.com/elastic/beats/filebeat/input"
"github.com/elastic/beats/libbeat/logp"
"golang.org/x/text/transform"
)
const (
defaultMaxBytes = 10 * (1 << 20) // 10MB
)
func NewHarvester(
prospectorCfg config.ProspectorConfig,
cfg *config.HarvesterConfig,
path string,
stat *FileStat,
spooler chan *input.FileEvent,
) (*Harvester, error) {
var err error
encoding, ok := encoding.FindEncoding(cfg.Encoding)
if !ok || encoding == nil {
return nil, fmt.Errorf("unknown encoding('%v')", cfg.Encoding)
}
h := &Harvester{
Path: path,
ProspectorConfig: prospectorCfg,
Config: cfg,
Stat: stat,
SpoolerChan: spooler,
encoding: encoding,
}
h.ExcludeLinesRegexp, err = InitRegexps(cfg.ExcludeLines)
if err != nil {
return h, err
}
h.IncludeLinesRegexp, err = InitRegexps(cfg.IncludeLines)
if err != nil {
return h, err
}
return h, nil
}
func createLineReader(
in FileSource,
codec encoding.Encoding,
bufferSize int,
maxBytes int,
readerConfig logFileReaderConfig,
mlrConfig *config.MultilineConfig,
) (processor.LineProcessor, error) {
var p processor.LineProcessor
var err error
fileReader, err := newLogFileReader(in, readerConfig)
if err != nil {
return nil, err
}
p, err = processor.NewLineSource(fileReader, codec, bufferSize)
if err != nil {
return nil, err
}
if mlrConfig != nil {
p, err = processor.NewMultiline(p, maxBytes, mlrConfig)
if err != nil {
return nil, err
}
return processor.NewStripNewline(p), nil
}
p = processor.NewStripNewline(p)
return processor.NewLimitProcessor(p, maxBytes), nil
}
// Log harvester reads files line by line and sends events to the defined output
func (h *Harvester) Harvest() {
defer func() {
// On completion, push offset so we can continue where we left off if we relaunch on the same file
h.Stat.Return <- h.Offset
// Make sure file is closed as soon as harvester exits
// If file was never properly opened, it can't be closed
if h.file != nil {
h.file.Close()
logp.Debug("harvester", "Closing file: %s", h.Path)
}
}()
enc, err := h.open()
if err != nil {
logp.Err("Stop Harvesting. Unexpected file opening error: %s", err)
return
}
info, err := h.file.Stat()
if err != nil {
logp.Err("Stop Harvesting. Unexpected file stat rror: %s", err)
return
}
logp.Info("Harvester started for file: %s", h.Path)
// TODO: NewLineReader uses additional buffering to deal with encoding and testing
// for new lines in input stream. Simple 8-bit based encodings, or plain
// don't require 'complicated' logic.
config := h.Config
readerConfig := logFileReaderConfig{
forceClose: config.ForceCloseFiles,
maxInactive: h.ProspectorConfig.IgnoreOlderDuration,
backoffDuration: config.BackoffDuration,
maxBackoffDuration: config.MaxBackoffDuration,
backoffFactor: config.BackoffFactor,
}
maxBytes := defaultMaxBytes
if config.MaxBytes != nil {
maxBytes = *config.MaxBytes
}
reader, err := createLineReader(
h.file, enc, config.BufferSize, maxBytes, readerConfig, config.Multiline)
if err != nil {
logp.Err("Stop Harvesting. Unexpected encoding line reader error: %s", err)
return
}
for {
// Partial lines return error and are only read on completion
ts, text, bytesRead, err := readLine(reader)
if err != nil {
if err == errFileTruncate {
seeker, ok := h.file.(io.Seeker)
if !ok {
logp.Err("can not seek source")
return
}
logp.Info("File was truncated. Begin reading file from offset 0: %s", h.Path)
h.Offset = 0
seeker.Seek(h.Offset, os.SEEK_SET)
continue
}
logp.Info("Read line error: %s", err)
return
}
if h.shouldExportLine(text) {
// Sends text to spooler
event := &input.FileEvent{
ReadTime: ts,
Source: &h.Path,
InputType: h.Config.InputType,
DocumentType: h.Config.DocumentType,
Offset: h.Offset,
Bytes: bytesRead,
Text: &text,
Fields: &h.Config.Fields,
Fileinfo: &info,
}
event.SetFieldsUnderRoot(h.Config.FieldsUnderRoot)
h.SpoolerChan <- event // ship the new event downstream
}
// Set Offset
h.Offset += int64(bytesRead) // Update offset if complete line has been processed
}
}
// shouldExportLine decides if the line is exported or not based on
// the include_lines and exclude_lines options.
func (h *Harvester) shouldExportLine(line string) bool {
if len(h.IncludeLinesRegexp) > 0 {
if !MatchAnyRegexps(h.IncludeLinesRegexp, line) {
// drop line
logp.Debug("harvester", "Drop line as it does not match any of the include patterns %s", line)
return false
}
}
if len(h.ExcludeLinesRegexp) > 0 {
if MatchAnyRegexps(h.ExcludeLinesRegexp, line) {
// drop line
logp.Debug("harvester", "Drop line as it does match one of the exclude patterns%s", line)
return false
}
}
return true
}
// open does open the file given under h.Path and assigns the file handler to h.file
func (h *Harvester) open() (encoding.Encoding, error) {
// Special handling that "-" means to read from standard input
if h.Config.InputType == config.StdinInputType {
return h.openStdin()
}
return h.openFile()
}
func (h *Harvester) openStdin() (encoding.Encoding, error) {
h.file = pipeSource{os.Stdin}
return h.encoding(h.file)
}
// openFile opens a file and checks for the encoding. In case the encoding cannot be detected
// or the file cannot be opened because for example of failing read permissions, an error
// is returned and the harvester is closed. The file will be picked up again the next time
// the file system is scanned
func (h *Harvester) openFile() (encoding.Encoding, error) {
var file *os.File
var err error
var encoding encoding.Encoding
file, err = input.ReadOpen(h.Path)
if err == nil {
// Check we are not following a rabbit hole (symlinks, etc.)
if !input.IsRegularFile(file) {
return nil, errors.New("Given file is not a regular file.")
}
encoding, err = h.encoding(file)
if err != nil {
if err == transform.ErrShortSrc {
logp.Info("Initialising encoding for '%v' failed due to file being too short", file)
} else {
logp.Err("Initialising encoding for '%v' failed: %v", file, err)
}
return nil, err
}
} else {
logp.Err("Failed opening %s: %s", h.Path, err)
return nil, err
}
// update file offset
err = h.initFileOffset(file)
if err != nil {
return nil, err
}
// yay, open file
h.file = fileSource{file}
return encoding, nil
}
func (h *Harvester) initFileOffset(file *os.File) error {
offset, err := file.Seek(0, os.SEEK_CUR)
if h.Offset > 0 {
// continue from last known offset
logp.Debug("harvester",
"harvest: %q position:%d (offset snapshot:%d)", h.Path, h.Offset, offset)
_, err = file.Seek(h.Offset, os.SEEK_SET)
} else if h.Config.TailFiles {
// tail file if file is new and tail_files config is set
logp.Debug("harvester",
"harvest: (tailing) %q (offset snapshot:%d)", h.Path, offset)
h.Offset, err = file.Seek(0, os.SEEK_END)
} else {
// get offset from file in case of encoding factory was
// required to read some data.
logp.Debug("harvester", "harvest: %q (offset snapshot:%d)", h.Path, offset)
h.Offset = offset
}
return err
}
func (h *Harvester) Stop() {
}
const maxConsecutiveEmptyReads = 100