-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforwarder.go
327 lines (307 loc) · 7.99 KB
/
forwarder.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/signal"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/hpcloud/tail"
"github.com/hpcloud/tail/watch"
"gopkg.in/tomb.v1"
)
type tailrConfig struct {
tomb.Tomb // provides: Done, Kill, Dying
watcherWaitgroup sync.WaitGroup
watcherMutex sync.Mutex
watchDir, logPattern *string
offsetFilePath *string
useOffset *bool
fileOffsetMap map[string]int64
noop *bool
outputChan chan string
noopOutputChan chan string
protocol, server, port *string
conn net.Conn
}
func NewTailrConfig() *tailrConfig {
return &tailrConfig{
fileOffsetMap: make(map[string]int64),
outputChan: make(chan string, 10),
noopOutputChan: make(chan string, 10),
}
}
func (c *tailrConfig) outputHandler() {
OutputHandlerLoop:
for {
select {
case n := <-c.noopOutputChan:
fmt.Println(n)
case o := <-c.outputChan:
fmt.Fprintf(c.conn, o+"\n")
case <-c.Dying():
log.Printf(
"Stopping noop output handler as the process received a stop signal",
)
break OutputHandlerLoop
}
}
defer func() {
if c.conn != nil {
c.conn.Close()
}
close(c.outputChan)
close(c.noopOutputChan)
c.watcherWaitgroup.Done()
}()
}
func (c *tailrConfig) shutdownHandler() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)
<-sigChan
log.Println("Received Shutdown signal, triggering exit ...")
// Notify the Tail that we are going to stop
c.Kill(nil)
defer func() {
close(sigChan)
c.watcherWaitgroup.Done()
}()
}
func (c *tailrConfig) updateOffsetFile() {
c.watcherMutex.Lock()
defer c.watcherMutex.Unlock()
offsetJson, err := json.Marshal(c.fileOffsetMap)
if err != nil {
log.Printf("Error Marshaling File Offset Metadata")
return
}
err = ioutil.WriteFile(*c.offsetFilePath, []byte(offsetJson), 0644)
if err != nil {
log.Println("Error updating Offset metadata File")
return
}
}
func (c *tailrConfig) checkOffsetFile() error {
fi, err := os.Stat(*c.offsetFilePath)
if err != nil {
return fmt.Errorf("Failed to collect file stat for file %s: %v", *c.offsetFilePath, err)
}
if fi.IsDir() {
return errors.New("Error Offset File path belongs to directory not file")
}
offsetMap, err := ioutil.ReadFile(*c.offsetFilePath)
if err != nil {
return fmt.Errorf("Error reading offSet File %s: %v", *c.offsetFilePath, err)
}
err = json.Unmarshal(offsetMap, &c.fileOffsetMap)
if err != nil {
return errors.New("Error Unmarshalling json file offset data")
}
return nil
}
func (c *tailrConfig) startFileForwarder(f string) {
defer c.watcherWaitgroup.Done()
tailConfig := tail.Config{Follow: true, Poll: true, MustExist: true}
c.watcherMutex.Lock()
offset, ok := c.fileOffsetMap[f]
c.watcherMutex.Unlock()
if ok {
tailConfig.Location = &tail.SeekInfo{Offset: offset, Whence: 1}
}
t, err := tail.TailFile(f, tailConfig)
if err != nil {
log.Printf("Failed to start tailing %s: %v", f, err)
return
}
// Start the background stopper and offset writer
fileWatcher := watch.NewPollingFileWatcher(f)
fi, err := os.Stat(f)
if err != nil {
log.Printf("Failed to collect file stat for file %s", f)
return
}
size := fi.Size()
fileChange, err := fileWatcher.ChangeEvents(&t.Tomb, size)
if err != nil {
log.Println(err)
}
timer := time.NewTicker(time.Second * 20)
ForwarderLoop:
for {
select {
case <-timer.C:
if t == nil {
// oops looks like the file was removed
continue
}
offset, err := t.Tell()
if err != nil {
log.Printf(
"Error collecting file offset for %s",
f,
)
continue
}
c.watcherMutex.Lock()
c.fileOffsetMap[f] = offset
c.watcherMutex.Unlock()
log.Printf("offset updated for file %v", f)
case <-fileChange.Deleted:
log.Printf(
"Stopping File Forwarder for %v as the file was removed",
f,
)
// Remove the key from the file Offset Map
c.watcherMutex.Lock()
delete(c.fileOffsetMap, f)
c.watcherMutex.Unlock()
t.Stop()
break ForwarderLoop
case <-c.Dying():
log.Printf(
"Stopping File Forwarder for %v as the process received a stop signal",
f,
)
t.Stop()
break ForwarderLoop
case lines := <-t.Lines:
// Hmm, sometimes we get empty Line struct :/ mostly when the file gets deleted
if lines == nil {
continue
} else if *c.noop {
c.noopOutputChan <- lines.Text
continue
}
c.outputChan <- lines.Text
}
}
}
func (c *tailrConfig) dirWatcher(dir string) {
defer c.watcherWaitgroup.Done()
// Start log forwarder for existing log files
match, err := filepath.Glob(path.Join(dir, *c.logPattern))
if err != nil {
log.Printf("Error finding files inside %s: %v", dir, err)
return
}
for _, f := range match {
log.Printf("Starting File Forwarder for %s", f)
c.watcherWaitgroup.Add(1)
go c.startFileForwarder(f)
}
// start the watcher for new files and dirs taht gets created
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("Error creating watcher for %s: %v", dir, err)
return
}
err = watcher.Add(dir)
if err != nil {
log.Printf("Error starting watcher for %s: %v", dir, err)
watcher.Close()
return
}
// Now listen for the events
DirWatchLoop:
for {
select {
case event := <-watcher.Events:
if event.Op.String() == "CREATE" {
log.Println("Watcher Event:", event.Name, event.Op)
fi, err := os.Stat(event.Name)
if err != nil {
log.Printf("Failed to collect file stat for file %s", event.Name)
continue
}
if fi.IsDir() {
// A new directory, lets start the directory watcher
c.watcherWaitgroup.Add(1)
go c.dirWatcher(event.Name)
continue
}
// A new file, check if it matches the regex
// Replace * with some valid characters to work with regexp
filePattern := strings.Replace(*c.logPattern, "*", "[0-9a-zA-Z@_-]+", -1) + "$"
matched, err := regexp.MatchString(filePattern, event.Name)
if err != nil {
log.Printf("Error matching regex with pattern %s for file name %s", filePattern, event.Name)
continue
}
if !matched {
log.Printf("File %s did not match the pattern %s", event.Name, filePattern)
continue
}
// File name matched the pattern, lets start forwarding
log.Printf("Starting File Forwarder for %s", event.Name)
c.watcherWaitgroup.Add(1)
go c.startFileForwarder(event.Name)
}
if event.Op.String() == "REMOVE" {
// Ouch the directory got removed, lets stop the corresponding
// directory watcher
if event.Name == dir {
log.Println("Watcher Event:", event.Name, event.Op)
log.Printf(
"Stopping Directory Watcher for %s as the directory was removed",
dir,
)
watcher.Close()
break DirWatchLoop
}
}
case err := <-watcher.Errors:
log.Println("Watcher Error:", err)
case <-c.Dying():
log.Printf(
"Stopping Directory watcher for %s as the process received a stop signal",
dir,
)
watcher.Close()
break DirWatchLoop
}
}
}
func (c *tailrConfig) recursiveDirWatcher() {
defer c.watcherWaitgroup.Done()
if !*c.noop {
// start the output server connection
var err error
c.conn, err = net.Dial(
*c.protocol,
fmt.Sprintf("%s:%s", *c.server, *c.port),
)
if err != nil {
panic(errors.New(fmt.Sprintf("Unable to connect to Server %s on Port %s", *c.server, *c.port)))
}
}
dirs, err := ioutil.ReadDir(*c.watchDir)
if err != nil {
log.Printf("Unable to read the watch directory %s", *c.watchDir)
return
}
c.watcherWaitgroup.Add(3)
// Start the signal handler
go c.shutdownHandler()
// start the output handler
go c.outputHandler()
// start the directory watchers
go c.dirWatcher(*c.watchDir)
for _, dir := range dirs {
if dir.IsDir() {
log.Printf("Starting Directory Watcher for %s", dir.Name())
c.watcherWaitgroup.Add(1)
// Watch each of the sub folders
go c.dirWatcher(path.Join(*c.watchDir, dir.Name()))
}
}
}