-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogtail.go
241 lines (196 loc) · 5.9 KB
/
logtail.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
package logmetrics
import (
"fmt"
"log"
"path/filepath"
"time"
"github.com/mathpl/tail"
)
type tailer struct {
ts tailStats
filename string
channel_number int
tsd_pusher chan []string
lg *logGroup
Bye chan bool
}
type tailStats struct {
line_read int64
byte_read int64
line_match int64
last_report time.Time
hostname string
filename string
log_group string
interval int
}
type lineResult struct {
filename string
matches []string
}
func (ts *tailStats) isTimeForStats() bool {
return time.Now().Sub(ts.last_report) > time.Duration(ts.interval)*time.Second
}
func (ts *tailStats) incLineMatch() {
ts.line_match++
}
func (ts *tailStats) incLine(line string) {
ts.line_read++
ts.byte_read += int64(len(line))
}
func (ts *tailStats) getTailStatsKey() []string {
t := time.Now()
ts.last_report = t
line := make([]string, 3)
line[0] = fmt.Sprintf("logmetrics_collector.tail.line_read %d %d host=%s log_group=%s filename=%s", t.Unix(), ts.line_read, ts.hostname, ts.log_group, ts.filename)
line[1] = fmt.Sprintf("logmetrics_collector.tail.byte_read %d %d host=%s log_group=%s filename=%s", t.Unix(), ts.byte_read, ts.hostname, ts.log_group, ts.filename)
line[2] = fmt.Sprintf("logmetrics_collector.tail.line_matched %d %d host=%s log_group=%s filename=%s", t.Unix(), ts.line_match, ts.hostname, ts.log_group, ts.filename)
return line
}
func (t *tailer) tailFile() {
t.ts = tailStats{last_report: time.Now(), hostname: getHostname(),
filename: t.filename, log_group: t.lg.name, interval: t.lg.interval}
maxMatches := t.lg.expected_matches + 1
var filename_matches []string
if t.lg.filename_match_re != nil {
m := t.lg.filename_match_re.MatcherString(t.filename, 0)
filename_matches = m.ExtractString()[1:]
}
//os.Seek end of file descriptor
seekParam := 2
if t.lg.parse_from_start {
seekParam = 0
}
loc := tail.SeekInfo{0, seekParam}
maxLineSize := 2048
tail, err := tail.TailFile(t.filename, tail.Config{Location: &loc, Follow: true, ReOpen: true, Poll: t.lg.poll_file, MaxLineSize: maxLineSize})
if err != nil {
log.Fatalf("Unable to tail %s: %s", t.filename, err)
return
}
log.Printf("Tailing %s data to datapool[%s:%d]", t.filename, t.lg.name, t.channel_number)
line_overflow := false
//FIXME: Bug in ActiveTail can get partial lines
for {
select {
case line, ok := <-tail.Lines:
if !ok {
err := tail.Err()
if err != nil {
log.Printf("Tail on %s ended with error: %v", t.filename, err)
} else {
log.Printf("Tail on %s ended early", t.filename)
}
return
}
if line == nil {
log.Printf("tail.Lines on %s returned nil; not possible", t.filename)
continue
}
//Support to skip very long lines
if line_overflow {
line_overflow = (len(line.Text) == maxLineSize)
continue
}
line_overflow = (len(line.Text) == maxLineSize)
//Test out all the regexp, pick the first one that matches
match_one := false
for _, re := range t.lg.re {
m := re.MatcherString(line.Text, 0)
matches := m.ExtractString()
if len(matches) == maxMatches {
match_one = true
if filename_matches != nil {
matches = append(matches, filename_matches[:]...)
}
results := lineResult{t.filename, matches}
t.lg.tail_data[t.channel_number] <- results
t.ts.incLineMatch()
break
}
}
t.ts.incLine(line.Text)
if t.lg.fail_regex_warn && !match_one {
log.Printf("Regexp match failed on %s, expected %d matches: %s", t.filename, maxMatches, line.Text)
}
if (t.ts.line_read%100) == 0 && t.ts.isTimeForStats() {
t.tsd_pusher <- t.ts.getTailStatsKey()
}
case <-t.Bye:
log.Printf("Tailer for %s stopped.", t.filename)
return
}
}
}
type filenamePoller struct {
lg *logGroup
poll_interval int
tsd_pushers []chan []string
push_number int
Bye chan bool
}
func (fp *filenamePoller) startFilenamePoller() {
log.Printf("Filename poller for %s started", fp.lg.name)
log.Printf("Using the following regexp for log group %s: %s", fp.lg.name, fp.lg.strRegexp)
rescanFiles := make(chan bool, 1)
go func() {
rescanFiles <- true
for {
time.Sleep(time.Duration(fp.poll_interval) * time.Second)
rescanFiles <- true
}
}()
currentFiles := make(map[string]bool)
channel_number := 0
pusher_channel_number := 0
allTailers := make([]*tailer, 0)
for {
select {
case <-rescanFiles:
newFiles := make(map[string]bool)
for _, glob := range fp.lg.globFiles {
files, err := filepath.Glob(glob)
if err != nil {
log.Fatalf("Unable to find files for log group %s: %s", fp.lg.name, err)
}
for _, v := range files {
newFiles[v] = true
}
}
//Check only the diff, missing files will automatically be dropped
//by their goroutine
for file, _ := range newFiles {
if ok := currentFiles[file]; ok {
delete(newFiles, file)
}
}
//Start tailing new files!
for file, _ := range newFiles {
bye := make(chan bool)
t := tailer{filename: file, channel_number: channel_number, lg: fp.lg, Bye: bye,
tsd_pusher: fp.tsd_pushers[pusher_channel_number]}
go t.tailFile()
allTailers = append(allTailers, &t)
channel_number = (channel_number + 1) % fp.lg.goroutines
pusher_channel_number = (pusher_channel_number + 1) % fp.push_number
currentFiles[file] = true
}
case <-fp.Bye:
for _, t := range allTailers {
go func() { t.Bye <- true }()
}
log.Printf("Filename poller for %s stopped", fp.lg.name)
return
}
}
}
func StartTails(config *Config, tsd_pushers []chan []string) []*filenamePoller {
filenamePollers := make([]*filenamePoller, 0)
for _, logGroup := range config.logGroups {
bye := make(chan bool)
f := filenamePoller{lg: logGroup, poll_interval: config.pollInterval, tsd_pushers: tsd_pushers, push_number: config.GetPusherNumber(), Bye: bye}
filenamePollers = append(filenamePollers, &f)
go f.startFilenamePoller()
}
return filenamePollers
}