This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
tail_group.go
136 lines (122 loc) · 3 KB
/
tail_group.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
package dendrite
import (
"github.com/fizx/logs"
"os"
"path/filepath"
"time"
)
type TailGroups []*TailGroup
type TailGroup struct {
Glob string
Pattern string
OffsetDir string
Name string
Hostname string
Tails map[string]*Tail
output chan Record
fields []FieldConfig
maxBackfill int64
maxLineSize int64
}
func (groups *TailGroups) Loop() {
for i := 0; true; i++ {
if i%60 == 0 {
groups.Refresh()
}
groups.Poll()
time.Sleep(1 * time.Second)
}
}
func (groups *TailGroups) Poll() {
for _, g := range *groups {
g.Poll()
}
}
func (groups *TailGroups) Refresh() {
for _, g := range *groups {
g.Refresh()
}
}
func NewTailGroup(config SourceConfig, output chan Record) *TailGroup {
group := new(TailGroup)
group.Hostname = config.Hostname
group.output = output
group.Name = config.Name
group.Glob = config.Glob
group.Pattern = config.Pattern
group.OffsetDir = config.OffsetDir
group.Tails = make(map[string]*Tail)
group.fields = config.Fields
group.maxLineSize = config.MaxLineSizeBytes
group.maxBackfill = config.MaxBackfillBytes
group.Refresh()
return group
}
func (group *TailGroup) activate(match string) {
tail, ok := group.Tails[match]
if !ok {
fi, _ := os.Stat(match)
for _, tail = range group.Tails {
tfi, _ := tail.Stat()
if os.SameFile(fi, tfi) {
tail.Close()
delete(group.Tails, tail.Path)
off := tail.Offset()
tail.SetOffset(0)
tail.WriteOffset()
base := filepath.Base(match)
offset := filepath.Join(group.OffsetDir, base+".ptr")
tail = NewTail(group.NewParser(base), group.maxBackfill, match, offset, off)
group.Tails[match] = tail
return
}
}
base := filepath.Base(match)
offset := filepath.Join(group.OffsetDir, base+".ptr")
tail = NewTail(group.NewParser(base), group.maxBackfill, match, offset, 0)
group.Tails[match] = tail
}
}
func (group *TailGroup) NewParser(file string) Parser {
return NewRegexpParser(group.Hostname, group.Name, file, group.output, group.Pattern, group.fields, group.maxLineSize)
}
func (group *TailGroup) deactivate(match string) {
tail, ok := group.Tails[match]
if ok {
delete(group.Tails, match)
tail.Close()
}
}
func (group *TailGroup) Refresh() {
d, _ := os.Getwd()
logs.Debug("pwd:", d)
matches, err := filepath.Glob(group.Glob)
if err != nil {
logs.Debug("Error in glob: ", err)
} else if matches == nil {
logs.Debug("Glob matched zero files: ", group.Glob)
} else if matches != nil {
logs.Debug("Glob matched %d files: ", len(matches), group.Glob)
for _, match := range matches {
info, err := os.Stat(match)
if err != nil {
logs.Debug("Can't stat: ", err)
} else if info.IsDir() {
logs.Debug("Ignoring directory: ", match)
} else {
if time.Since(info.ModTime()).Hours() >= 1 {
logs.Debug("Ignoring idle file: ", match)
group.deactivate(match)
} else {
logs.Debug("Tailing: ", match)
group.activate(match)
}
}
}
}
}
func (group *TailGroup) Poll() {
for _, tail := range group.Tails {
tail.Poll()
}
}