-
Notifications
You must be signed in to change notification settings - Fork 204
/
tail.go
629 lines (557 loc) · 14.6 KB
/
tail.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// Copyright (c) 2015 HPE Software Inc. All rights reserved.
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
package tail
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"sync"
"sync/atomic"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/models"
"gopkg.in/tomb.v1"
"github.com/aws/amazon-cloudwatch-agent/plugins/inputs/logfile/tail/watch"
)
var (
ErrStop = errors.New("Tail should now stop")
ErrDeletedNotReOpen = errors.New("File was deleted, tail should now stop")
exitOnDeletionCheckDuration = time.Minute
exitOnDeletionWaitDuration = 5 * time.Minute
OpenFileCount atomic.Int64
)
type Line struct {
Text string
Time time.Time
Err error // Error from tail
Offset int64 // offset of current reader
}
// NewLine returns a Line with present time.
func NewLine(text string, offset int64) *Line {
return &Line{text, time.Now(), nil, offset}
}
// SeekInfo represents arguments to `os.Seek`
type SeekInfo struct {
Offset int64
Whence int // os.SEEK_*
}
type limiter interface {
Pour(uint16) bool
}
// Config is used to specify how a file must be tailed.
type Config struct {
// File-specifc
Location *SeekInfo // Seek to this location before tailing
ReOpen bool // Reopen recreated files (tail -F)
MustExist bool // Fail early if the file does not exist
Poll bool // Poll for file changes instead of using inotify
Pipe bool // Is a named pipe (mkfifo)
RateLimiter limiter
// Generic IO
Follow bool // Continue looking for new lines (tail -f)
MaxLineSize int // If non-zero, split longer lines into multiple lines
Logger telegraf.Logger
// Special handling for utf16
IsUTF16 bool
}
type Tail struct {
Filename string
Lines chan *Line
Config
file *os.File
reader *bufio.Reader
watcher watch.FileWatcher
changes *watch.FileChanges
curOffset int64
tomb.Tomb // provides: Done, Kill, Dying
dropCnt int
lk sync.Mutex
FileDeletedCh chan bool
}
// TailFile begins tailing the file. Output stream is made available
// via the `Tail.Lines` channel. To handle errors during tailing,
// invoke the `Wait` or `Err` method after finishing reading from the
// `Lines` channel.
func TailFile(filename string, config Config) (*Tail, error) {
if config.ReOpen && !config.Follow {
return nil, errors.New("cannot set ReOpen without Follow.")
}
t := &Tail{
Filename: filename,
Lines: make(chan *Line),
Config: config,
FileDeletedCh: make(chan bool),
}
// when Logger was not specified in config, create new one
if t.Logger == nil {
t.Logger = models.NewLogger("inputs", "tail", "")
}
if t.Poll {
t.watcher = watch.NewPollingFileWatcher(filename)
} else {
t.watcher = watch.NewInotifyFileWatcher(filename)
}
if t.MustExist {
var err error
t.file, err = OpenFile(t.Filename)
if err != nil {
return nil, err
}
OpenFileCount.Add(1)
}
if !config.ReOpen {
go t.exitOnDeletion()
}
go t.tailFileSync()
return t, nil
}
// Return the file's current position, like stdio's ftell().
// But this value is not very accurate.
// it may readed one line in the chan(tail.Lines),
// so it may lost one line.
func (tail *Tail) Tell() (offset int64, err error) {
if tail.file == nil {
return
}
offset, err = tail.file.Seek(0, os.SEEK_CUR)
if err != nil {
return
}
tail.lk.Lock()
defer tail.lk.Unlock()
if tail.reader == nil {
return
}
offset -= int64(tail.reader.Buffered())
return
}
// Stop stops the tailing activity.
func (tail *Tail) Stop() error {
tail.Kill(nil)
return tail.Wait()
}
// StopAtEOF stops tailing as soon as the end of the file is reached.
// Blocks until tailer is dead and returns reason for death.
func (tail *Tail) StopAtEOF() error {
tail.Kill(errStopAtEOF)
return tail.Wait()
}
var errStopAtEOF = errors.New("tail: stop at eof")
func (tail *Tail) close() {
if tail.dropCnt > 0 {
tail.Logger.Errorf("Dropped %v lines for stopped tail for file %v", tail.dropCnt, tail.Filename)
}
close(tail.Lines)
tail.closeFile()
}
func (tail *Tail) closeFile() {
if tail.file != nil {
tail.file.Close()
tail.file = nil
OpenFileCount.Add(-1)
}
}
func (tail *Tail) reopen() error {
tail.closeFile()
for {
var err error
tail.file, err = OpenFile(tail.Filename)
tail.curOffset = 0
if err != nil {
if os.IsNotExist(err) {
tail.Logger.Debugf("Waiting for %s to appear...", tail.Filename)
if err := tail.watcher.BlockUntilExists(&tail.Tomb); err != nil {
if err == tomb.ErrDying {
return err
}
return fmt.Errorf("Failed to detect creation of %s: %s", tail.Filename, err)
}
continue
}
return fmt.Errorf("Unable to open file %s: %s", tail.Filename, err)
}
break
}
OpenFileCount.Add(1)
return nil
}
// readLine() tries to return a single line, not including the end-of-line bytes.
// If the line is too long for the buffer then partial line will be returned.
// The rest of the line will be returned from future calls. If error is encountered
// before finding the end-of-line bytes(often io.EOF), it returns the data read
// before the error and the error itself.
func (tail *Tail) readLine() (string, error) {
if tail.Config.IsUTF16 {
return tail.readlineUtf16()
}
tail.lk.Lock()
defer tail.lk.Unlock()
line, err := tail.readSlice('\n')
if err == bufio.ErrBufferFull {
// Handle the case where "\r\n" straddles the buffer.
if len(line) > 0 && line[len(line)-1] == '\r' {
tail.unreadByte()
line = line[:len(line)-1]
}
return string(line), nil
}
if len(line) > 0 && line[len(line)-1] == '\n' {
drop := 1
if len(line) > 1 && line[len(line)-2] == '\r' {
drop = 2
}
line = line[:len(line)-drop]
}
return string(line), err
}
func (tail *Tail) readlineUtf16() (string, error) {
tail.lk.Lock()
defer tail.lk.Unlock()
var cur []byte
var err error
var res [][]byte
var resSize int
for {
// Check LF
cur, err = tail.readSlice('\n')
// buffer size is even
if err == bufio.ErrBufferFull {
// Handle the case where "\r\n" straddles the buffer.
if len(cur) > 1 && cur[len(cur)-1] == '\x00' && cur[len(cur)-2] == '\r' {
tail.unreadByte()
tail.unreadByte()
cur = cur[:len(cur)-2]
}
err = nil
break
}
if err != nil {
break
}
// We only care about 0a00
if len(cur)%2 != 0 {
var nextByte byte
nextByte, err = tail.readByte()
if err != nil {
break
}
if nextByte == '\x00' {
// confirmed it's LF, check for Carriage Return
if len(cur) >= 3 && cur[len(cur)-2] == '\x00' && cur[len(cur)-3] == '\r' {
cur = cur[:len(cur)-3]
} else {
cur = cur[:len(cur)-1]
}
break
}
cur = append(cur, nextByte)
}
// 262144 => 256KB
if resSize+len(cur) >= 262144 {
break
}
buf := make([]byte, len(cur))
copy(buf, cur)
res = append(res, buf)
resSize += len(buf)
}
resSize += len(cur)
res = append(res, cur)
finalRes := make([]byte, resSize)
n := 0
for i := range res {
n += copy(finalRes[n:], res[i])
}
return string(finalRes), err
}
func (tail *Tail) tailFileSync() {
defer tail.Done()
defer tail.close()
if !tail.MustExist {
// deferred first open.
err := tail.reopen()
if err != nil {
if err != tomb.ErrDying {
tail.Kill(err)
}
return
}
}
// openReader should be invoked before seekTo
tail.openReader()
// Seek to requested location on first open of the file.
if tail.Location != nil {
err := tail.seekTo(*tail.Location)
tail.Logger.Debugf("Seeked %s - %+v\n", tail.Filename, tail.Location)
if err != nil {
tail.Killf("Seek error on %s: %s", tail.Filename, err)
return
}
}
if err := tail.watchChanges(); err != nil {
tail.Killf("Error watching for changes on %s: %s", tail.Filename, err)
return
}
var backupOffset int64
// Read line by line.
for {
// do not set backupOffset in named pipes
if !tail.Pipe {
backupOffset = tail.curOffset
}
line, err := tail.readLine()
// Process `line` even if err is EOF.
if err == nil {
cooloff := !tail.sendLine(line, tail.curOffset)
if cooloff {
// Wait a second before seeking till the end of
// file when rate limit is reached.
msg := "Too much log activity; waiting a second before resuming tailing"
tail.Lines <- &Line{msg, time.Now(), errors.New(msg), tail.curOffset}
select {
case <-time.After(time.Second):
case <-tail.Dying():
return
}
if err := tail.seekEnd(); err != nil {
tail.Kill(err)
return
}
}
} else if err == io.EOF {
if !tail.Follow {
if line != "" {
tail.sendLine(line, tail.curOffset)
}
return
}
if tail.Follow && line != "" {
// this has the potential to never return the last line if
// it's not followed by a newline; seems a fair trade here
err := tail.seekTo(SeekInfo{Offset: backupOffset, Whence: 0})
if err != nil {
tail.Kill(err)
return
}
}
// When EOF is reached, wait for more data to become
// available. Wait strategy is based on the `tail.watcher`
// implementation (inotify or polling).
err := tail.waitForChanges()
if err != nil {
if err == ErrDeletedNotReOpen {
close(tail.FileDeletedCh)
for {
line, errReadLine := tail.readLine()
if errReadLine == nil {
tail.sendLine(line, tail.curOffset)
} else {
return
}
}
} else if err != ErrStop {
tail.Kill(err)
}
return
}
} else {
// non-EOF error
tail.Killf("Error reading %s: %s", tail.Filename, err)
return
}
select {
case <-tail.Dying():
if tail.Err() == errStopAtEOF {
continue
}
return
default:
}
}
}
// watchChanges ensures the watcher is running.
func (tail *Tail) watchChanges() error {
if tail.changes != nil {
return nil
}
pos, err := tail.file.Seek(0, os.SEEK_CUR)
if err != nil {
return err
}
tail.changes, err = tail.watcher.ChangeEvents(&tail.Tomb, pos)
return err
}
// waitForChanges waits until the file has been appended, deleted,
// moved or truncated. When moved or deleted - the file will be
// reopened if ReOpen is true. Truncated files are always reopened.
func (tail *Tail) waitForChanges() error {
if err := tail.watchChanges(); err != nil {
return err
}
select {
case <-tail.changes.Modified:
return nil
case <-tail.changes.Deleted:
tail.changes = nil
if tail.ReOpen {
tail.Logger.Infof("Re-opening moved/deleted file %s ...", tail.Filename)
if err := tail.reopen(); err != nil {
return err
}
tail.Logger.Debugf("Successfully reopened %s", tail.Filename)
tail.openReader()
return nil
} else {
tail.Logger.Warnf("Stopping tail as file no longer exists: %s", tail.Filename)
return ErrDeletedNotReOpen
}
case <-tail.changes.Truncated:
// Always reopen truncated files (Follow is true)
tail.Logger.Infof("Re-opening truncated file %s ...", tail.Filename)
if err := tail.reopen(); err != nil {
return err
}
tail.Logger.Debugf("Successfully reopened truncated %s", tail.Filename)
tail.openReader()
return nil
case <-tail.Dying():
return ErrStop
}
}
func (tail *Tail) openReader() {
tail.lk.Lock()
if tail.MaxLineSize > 0 {
// add 2 to account for newline characters
tail.reader = bufio.NewReaderSize(tail.file, tail.MaxLineSize+2)
} else {
tail.reader = bufio.NewReader(tail.file)
}
tail.lk.Unlock()
}
func (tail *Tail) seekEnd() error {
return tail.seekTo(SeekInfo{Offset: 0, Whence: os.SEEK_END})
}
func (tail *Tail) seekTo(pos SeekInfo) error {
_, err := tail.file.Seek(pos.Offset, pos.Whence)
if err != nil {
return fmt.Errorf("Seek error on %s: %s", tail.Filename, err)
}
// Reset the read buffer whenever the file is re-seek'ed
tail.reader.Reset(tail.file)
tail.curOffset, err = tail.Tell()
return err
}
// sendLine sends the line(s) to Lines channel, splitting longer lines
// if necessary. Return false if rate limit is reached.
func (tail *Tail) sendLine(line string, offset int64) bool {
now := time.Now()
lines := []string{line}
// Split longer lines
if tail.MaxLineSize > 0 && len(line) > tail.MaxLineSize {
lines = partitionString(line, tail.MaxLineSize)
}
for i, line := range lines {
// This select is to avoid blockage on the tail.Lines chan
select {
case tail.Lines <- &Line{line, now, nil, offset}:
case <-tail.Dying():
if tail.Err() == errStopAtEOF {
// Try sending, even if it blocks.
tail.Lines <- &Line{line, now, nil, offset}
} else {
tail.dropCnt += len(lines) - i
return true
}
}
}
if tail.Config.RateLimiter != nil {
ok := tail.Config.RateLimiter.Pour(uint16(len(lines)))
if !ok {
tail.Logger.Debugf("Leaky bucket full (%v); entering 1s cooloff period.\n",
tail.Filename)
return false
}
}
return true
}
// Cleanup removes inotify watches added by the tail package. This function is
// meant to be invoked from a process's exit handler. Linux kernel may not
// automatically remove inotify watches after the process exits.
func (tail *Tail) Cleanup() {
watch.Cleanup(tail.Filename)
}
// A wrapper of bufio ReadSlice
func (tail *Tail) readSlice(delim byte) (line []byte, err error) {
line, err = tail.reader.ReadSlice(delim)
tail.curOffset += int64(len(line))
return
}
// A wrapper of bufio ReadByte
func (tail *Tail) readByte() (b byte, err error) {
b, err = tail.reader.ReadByte()
tail.curOffset += 1
return
}
// A wrapper of bufio UnreadByte
func (tail *Tail) unreadByte() (err error) {
err = tail.reader.UnreadByte()
tail.curOffset -= 1
return
}
// A wrapper of tomb Err()
func (tail *Tail) UnexpectedError() (err error) {
err = tail.Err()
// ignore the error ErrStillAlive and errStopAtEOF
if err == tomb.ErrStillAlive || err == errStopAtEOF {
err = nil
}
return
}
func (tail *Tail) exitOnDeletion() {
ticker := time.NewTicker(exitOnDeletionCheckDuration)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if tail.isFileDeleted() {
select {
case <-tail.Dying():
return
case <-time.After(exitOnDeletionWaitDuration):
// wait for some time in case tail can catch up with the EOF
msg := fmt.Sprintf("File %s was deleted, but file content is not tailed completely.", tail.Filename)
tail.Logger.Error(msg)
tail.Kill(errors.New(msg))
return
}
}
case <-tail.Dying():
return
}
}
}
// partitionString partitions the string into chunks of given size,
// with the last chunk of variable size.
func partitionString(s string, chunkSize int) []string {
if chunkSize <= 0 {
panic("Invalid chunkSize")
}
length := len(s)
chunks := 1 + length/chunkSize
start := 0
end := chunkSize
parts := make([]string, 0, chunks)
for {
if end > length {
end = length
}
parts = append(parts, s[start:end])
if end == length {
break
}
start, end = end, end+chunkSize
}
return parts
}