-
Notifications
You must be signed in to change notification settings - Fork 10
/
file_watcher.go
166 lines (140 loc) · 4.09 KB
/
file_watcher.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
package blackbox
import (
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"code.cloudfoundry.org/blackbox/syslog"
"code.cloudfoundry.org/go-loggregator/v9/rfc5424"
"github.com/tedsuo/ifrit/grouper"
)
const POLL_INTERVAL = 5 * time.Second
type fileWatcher struct {
logger *log.Logger
sourceDir string
logFilename bool
dynamicGroupClient grouper.DynamicClient
hostname string
maxMessageSize int
structuredData rfc5424.StructuredData
excludeFilePattern string
drain syslog.Drain
}
func NewFileWatcher(
logger *log.Logger,
sourceDir string,
logFilename bool,
dynamicGroupClient grouper.DynamicClient,
drain syslog.Drain,
hostname string,
maxMessageSize int,
structuredData rfc5424.StructuredData,
excludeFilePattern string,
) *fileWatcher {
return &fileWatcher{
logger: logger,
sourceDir: sourceDir,
logFilename: logFilename,
dynamicGroupClient: dynamicGroupClient,
drain: drain,
hostname: hostname,
structuredData: structuredData,
maxMessageSize: maxMessageSize,
excludeFilePattern: excludeFilePattern,
}
}
func (f *fileWatcher) Watch() {
for {
logDirs, err := os.ReadDir(f.sourceDir)
if err != nil {
f.logger.Fatalf("could not list directories in source dir: %s\n", err)
}
for _, logDir := range logDirs {
tag := logDir.Name()
tagDirPath := filepath.Join(f.sourceDir, tag)
fileInfo, err := os.Stat(tagDirPath)
if err != nil {
f.logger.Fatalf("failed to determine if path is directory: %s\n", err)
}
if !fileInfo.IsDir() {
continue
}
f.findLogsToWatch(tag, tagDirPath, fileInfo)
}
time.Sleep(POLL_INTERVAL)
}
}
func (f *fileWatcher) findLogsToWatch(tag string, filePath string, file fs.FileInfo) {
if !file.IsDir() {
if strings.HasSuffix(file.Name(), ".log") {
if matched, _ := filepath.Match(f.excludeFilePattern, file.Name()); matched {
return
}
if _, found := f.dynamicGroupClient.Get(filePath); !found {
f.dynamicGroupClient.Inserter() <- f.memberForFile(filePath)
}
}
return
}
dirContents, err := os.ReadDir(filePath)
if err != nil {
f.logger.Printf("skipping log dir '%s' (could not list files): %s\n", tag, err)
return
}
for _, content := range dirContents {
currentFilePath := filepath.Join(filePath, content.Name())
info, err := content.Info()
if err == nil {
f.findLogsToWatch(tag, currentFilePath, info)
}
}
}
func (f *fileWatcher) memberForFile(logfilePath string) grouper.Member {
drainer, err := syslog.NewDrainer(f.logger, f.drain, f.hostname, f.structuredData, f.maxMessageSize)
if err != nil {
f.logger.Fatalf("could not drain to syslog: %s\n", err)
}
tag := f.determineTag(logfilePath)
tag = f.formatSyslogAppName(tag, logfilePath)
tailer := &Tailer{
Path: logfilePath,
Tag: tag,
Drainer: drainer,
Logger: f.logger,
}
return grouper.Member{Name: tailer.Path, Runner: tailer}
}
func (f *fileWatcher) determineTag(logfilePath string) string {
var tag string
var err error
if f.logFilename {
tag, err = filepath.Rel(f.sourceDir, logfilePath)
} else {
logfileDir := filepath.Dir(logfilePath)
tag, err = filepath.Rel(f.sourceDir, logfileDir)
}
if err != nil {
f.logger.Fatalf("could not compute tag from file path %s: %s\n", logfilePath, err)
}
return tag
}
func (f *fileWatcher) formatSyslogAppName(originalAppname string, logfilePath string) string {
//only ASCII chars from 33 to 126 are allowed
forbiddenCharacters := "[^!-~]+"
reg, err := regexp.Compile(forbiddenCharacters)
if err != nil {
f.logger.Fatalf("could not create regexp for sanitizing app-name %s: %s\n", logfilePath, err)
}
appname := reg.ReplaceAllString(originalAppname, "")
if len(originalAppname) != len(appname) {
f.logger.Printf("App-name consisted of chars outside of ASCII 33 to 126. app-name : %s, path: %s", originalAppname, logfilePath)
}
if len(appname) > 48 {
f.logger.Printf("App-name was too long. Trimmed it to 48 Characters according to syslog formating rules, app-name : %s, path: %s", originalAppname, logfilePath)
appname = appname[0:48]
}
return appname
}