This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.go
313 lines (292 loc) · 8.85 KB
/
files.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
/*************************************************************************
* Copyright 2022 Gravwell, Inc. All rights reserved.
* Contact: <[email protected]>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package main
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/gravwell/gravwell/v3/filewatch"
"github.com/gravwell/gravwell/v3/ingest"
"github.com/gravwell/gravwell/v3/ingest/entry"
"github.com/gravwell/gravwell/v3/ingest/log"
"github.com/gravwell/gravwell/v3/ingest/processors"
"github.com/gravwell/gravwell/v3/ingesters/utils"
"github.com/gravwell/gravwell/v3/timegrinder"
)
const (
filesStateType string = `files`
)
type files struct {
Base_Directory string // the base directory we will be watching
File_Filter string // the glob for pattern matching
Tag_Name string
Ignore_Timestamps bool //Just apply the current timestamp to lines as we get them
Assume_Local_Timezone bool
Timestamp_Format_Override string //override the timestamp format
Timezone_Override string
Recursive bool // Should we descend into child directories?
Ignore_Line_Prefix []string
Preprocessor []string
}
func fileJob(cfgName string, ctx context.Context, uc chan string) (err error) {
//build a list of base directories and globs
val, ok := cfg.Files[cfgName]
if !ok {
return ErrNotFound
}
var filelist []string
lg.Infof("Processing flat file config %s\n", cfgName)
uc <- fmt.Sprintf("Processing flat file config %s\n", cfgName)
if filelist, err = getFileList(val, st); err != nil {
lg.Error("failed to get file list", log.KV("file-processor", cfgName), log.KVErr(err))
return err
} else if len(filelist) == 0 {
uc <- fmt.Sprintf("Config %v has no unprocessed files", cfgName)
return nil
}
//we have files, get the ingester up and rolling
pproc, err := cfg.Preprocessor.ProcessorSet(igst, val.Preprocessor)
if err != nil {
lg.Error("preprocessor construction error", log.KVErr(err))
return err
}
//get the tag for this listener
tag, err := igst.GetTag(val.Tag_Name)
if err != nil {
lg.Error("failed to resolve tag", log.KV("watcher", cfgName), log.KV("tag", val.Tag_Name), log.KVErr(err))
return err
}
var ignore [][]byte
for _, prefix := range val.Ignore_Line_Prefix {
if prefix != "" {
ignore = append(ignore, []byte(prefix))
}
}
var tg *timegrinder.TimeGrinder
if !val.Ignore_Timestamps {
tcfg := timegrinder.Config{
EnableLeftMostSeed: true,
}
if tg, err = timegrinder.NewTimeGrinder(tcfg); err != nil {
lg.Error("failed to create timegrinder", log.KVErr(err))
return err
} else if err = cfg.TimeFormat.LoadFormats(tg); err != nil {
lg.Error("failed to load custom time formats", log.KVErr(err))
return err
}
if val.Timestamp_Format_Override != `` {
if err = tg.SetFormatOverride(val.Timestamp_Format_Override); err != nil {
lg.Error("failed to set timestamp override", log.KV("timestampoverride", val.Timestamp_Format_Override), log.KVErr(err))
return err
}
}
if val.Assume_Local_Timezone && val.Timezone_Override != `` {
return errors.New("Cannot specify AssumeLocalTZ and TimezoneOverride in the same LogHandlerConfig")
}
if val.Assume_Local_Timezone {
tg.SetLocalTime()
}
if val.Timezone_Override != `` {
if err = tg.SetTimezone(val.Timezone_Override); err != nil {
lg.Error("failed to set timezone override", log.KV("timezone", val.Timezone_Override), log.KVErr(err))
return err
}
}
}
rdrCfg := utils.LineDelimitedStream{
Proc: pproc,
Tag: tag,
SRC: src,
TG: tg,
IgnorePrefixes: ignore,
BatchSize: 128,
Verbose: *verbose,
}
var count, size uint64
var fileCount int
for _, f := range filelist {
fileCount++
var c, s uint64
var rc io.ReadCloser
if checkSig(ctx) {
return nil
}
if rc, err = utils.OpenBufferedFileReader(f, 8192); err != nil {
lg.Error("failed to open file", log.KV("path", f), log.KVErr(err))
return err
}
rdrCfg.Rdr = rc
if c, s, err = utils.IngestLineDelimitedStream(rdrCfg); err != nil {
rc.Close()
lg.Error("failed to ingest file", log.KV("path", f), log.KVErr(err))
return err
}
count += c
size += s
if err = rc.Close(); err != nil {
lg.Error("failed to close file", log.KV("path", f), log.KVErr(err))
return err
} else if err = st.Add(filesStateType, fileStatus{Path: f, Count: c, Size: s}); err != nil {
lg.Error("failed to set status of file", log.KV("path", f), log.KVErr(err))
return err
}
uc <- fmt.Sprintf("Ingested %d files (%d entries, %d bytes)", fileCount, count, size)
lg.Info("migrated file", log.KV("path", f))
}
uc <- fmt.Sprintf("Ingested %d files (%d entries, %d bytes)", fileCount, count, size)
return nil
}
type fileStatus struct {
Path string
Count uint64
Size uint64
}
func getFileList(val *files, st *StateTracker) ([]string, error) {
var obj fileStatus
mp := map[string]fileStatus{}
// populate the list of files that have been handled so far
if err := st.GetStates(filesStateType, &obj, func(val interface{}) error {
if val == nil {
return nil ///ummm ok?
}
fs, ok := val.(*fileStatus)
if !ok {
return fmt.Errorf("invalid file status decode value %T", val) // this really should not be possible...
} else if fs == nil {
return fmt.Errorf("nil file status")
}
mp[fs.Path] = *fs
return nil
}); err != nil {
return nil, fmt.Errorf("Failed to decode file states %w", err)
}
fltrs, err := filewatch.ExtractFilters(val.File_Filter)
if err != nil {
return nil, err
}
if val.Recursive {
return getRecursiveDir(fltrs, val, mp)
}
return getSingleDir(fltrs, val, mp)
}
func getSingleDir(fltrs []string, val *files, mp map[string]fileStatus) ([]string, error) {
fl := &fileList{}
//walk the directory and decide if we should bring the file in
if des, err := fs.ReadDir(os.DirFS(val.Base_Directory), `.`); err != nil {
return nil, fmt.Errorf("Failed to read directory %v: %w", val.Base_Directory, err)
} else {
for _, de := range des {
if !de.Type().IsRegular() {
continue
} else if matchFile(fltrs, de.Name()) {
fullPath := filepath.Join(val.Base_Directory, de.Name())
if _, ok := mp[fullPath]; !ok {
fl.add(fullPath, de)
}
}
}
}
return fl.paths(), nil
}
func getRecursiveDir(fltrs []string, val *files, mp map[string]fileStatus) ([]string, error) {
fl := &fileList{}
cb := func(pth string, de fs.DirEntry, err error) error {
if err != nil {
return err
} else if de.Type().IsRegular() {
if matchFile(fltrs, de.Name()) {
fullPath := filepath.Join(val.Base_Directory, pth)
if _, ok := mp[fullPath]; !ok {
fl.add(fullPath, de)
}
}
}
return nil
}
if err := fs.WalkDir(os.DirFS(val.Base_Directory), `.`, cb); err != nil {
return nil, err
}
return fl.paths(), nil
}
func matchFile(fltrs []string, name string) bool {
for _, f := range fltrs {
if matched, err := filepath.Match(f, name); err == nil && matched {
return true
}
}
return false
}
func (f *files) Validate(procs processors.ProcessorConfig) (err error) {
if len(f.Base_Directory) == 0 {
return errors.New("No Base-Directory provided")
}
if len(f.Tag_Name) == 0 {
f.Tag_Name = entry.DefaultTagName
}
if strings.ContainsAny(f.Tag_Name, ingest.FORBIDDEN_TAG_SET) {
return errors.New("Invalid characters in the Tag-Name")
}
f.Base_Directory = filepath.Clean(f.Base_Directory)
if f.Timezone_Override != "" {
if f.Assume_Local_Timezone {
// cannot do both
return fmt.Errorf("Cannot specify Assume-Local-Timezone and Timezone-Override")
}
if _, err = time.LoadLocation(f.Timezone_Override); err != nil {
return fmt.Errorf("Invalid timezone override %v: %v", f.Timezone_Override, err)
}
}
if _, err = filewatch.ExtractFilters(f.File_Filter); err != nil {
return err
}
if err = procs.CheckProcessors(f.Preprocessor); err != nil {
return fmt.Errorf("Files preprocessor invalid: %v", err)
}
return
}
func (f files) TimestampOverride() (v string, err error) {
v = strings.TrimSpace(f.Timestamp_Format_Override)
return
}
func (f files) TimezoneOverride() string {
return f.Timezone_Override
}
type fileEnt struct {
pth string
mod time.Time
}
type fileList struct {
lst []fileEnt
}
func (fl *fileList) add(pth string, de fs.DirEntry) {
var mod time.Time
if fi, err := de.Info(); err == nil && fi != nil {
mod = fi.ModTime()
}
fl.lst = append(fl.lst, fileEnt{pth: pth, mod: mod})
}
func (fl *fileList) paths() (r []string) {
if fl == nil || len(fl.lst) == 0 {
return
}
sort.SliceStable(fl.lst, func(i, j int) bool {
return fl.lst[i].mod.Before(fl.lst[j].mod)
})
r = make([]string, 0, len(fl.lst))
for _, fe := range fl.lst {
r = append(r, fe.pth)
}
return
}