-
Notifications
You must be signed in to change notification settings - Fork 17
/
processors_extra.go
509 lines (445 loc) · 10.5 KB
/
processors_extra.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*************************************************************************
* Copyright 2017 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package timegrinder
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
//the processor_extra file contains grinder formats that don't fit into the traditional
//model and may be a little more expensive to actually process
type syslogProcessor struct {
processor
}
func NewSyslogProcessor() *syslogProcessor {
return &syslogProcessor{
processor: processor{
rxp: regexp.MustCompile(SyslogRegex),
rxstr: SyslogRegex,
format: SyslogFormat,
name: Syslog.String(),
min: len(SyslogFormat),
},
}
}
type unixProcessor struct {
re *regexp.Regexp
rxstr string
format string
name string
min int
}
func NewUnixMilliTimeProcessor() *unixProcessor {
return &unixProcessor{
re: regexp.MustCompile(UnixMilliRegex),
rxstr: UnixMilliRegex,
format: ``, //format API doesn't work here
name: UnixMilli.String(),
min: 10,
}
}
func (up *unixProcessor) Format() string {
return up.format
}
func (up *unixProcessor) Name() string {
return up.name
}
func (up *unixProcessor) ToString(t time.Time) string {
uns := t.Unix()
return fmt.Sprintf("%d.%d", uns, t.UnixNano()-(uns*ns))
}
func (up *unixProcessor) ExtractionRegex() string {
return _unixCoreRegex
}
func NewUserProcessor(name, rxps, fmts string) (*processor, error) {
//check that regex compiles
rx, err := regexp.Compile(rxps)
if err != nil {
return nil, err
}
//check that you can format NOW using the format string
//then reparse the output and get something sensible
x := time.Now().Format(fmts)
if _, err = time.Parse(fmts, x); err != nil {
return nil, err
}
//check that the regex hits on the format string
bts := []byte(x)
idxs := rx.FindIndex(bts)
if len(idxs) != 2 {
return nil, errors.New("Regular expression does not trigger on format")
}
//check that the extraction via the regex can be parsed
x = string(bts[idxs[0]:idxs[1]])
if _, err = time.Parse(fmts, x); err != nil {
return nil, err
}
//return the processor
return &processor{
rxp: rx,
rxstr: rxps,
format: fmts,
name: name,
}, nil
}
func (sp syslogProcessor) Extract(d []byte, loc *time.Location) (time.Time, bool, int) {
if len(d) < sp.min {
return time.Time{}, false, -1
}
t, ok, offset := sp.processor.Extract(d, loc)
if !ok {
return time.Time{}, false, -1
}
//check if we need to set the year
if t.Year() == 0 {
return tweakYear(t), true, offset
}
return t, true, offset
}
func (up unixProcessor) Extract(d []byte, loc *time.Location) (t time.Time, ok bool, offset int) {
if len(d) < up.min {
return time.Time{}, false, -1
}
offset = -1
idx := up.re.FindSubmatchIndex(d)
if len(idx) != 4 {
return
}
s, err := strconv.ParseFloat(string(d[idx[2]:idx[3]]), 64)
if err != nil {
return
}
offset = idx[2]
sec := int64(s)
nsec := int64((s - float64(sec)) * 1000000000.0)
t = time.Unix(sec, nsec).In(loc)
ok = true
return
}
func (up unixProcessor) Match(d []byte) (start, end int, ok bool) {
idx := up.re.FindSubmatchIndex(d)
if len(idx) == 4 {
start, end = idx[2], idx[3]
ok = true
}
return
}
type unixMsProcessor struct {
re *regexp.Regexp
rxstr string
format string
name string
min int
}
// We assume you're not ingesting data from 1970, so we look for at least 13 digits of nanoseconds
func NewUnixMsTimeProcessor() *unixMsProcessor {
return &unixMsProcessor{
re: regexp.MustCompile(UnixMsRegex),
rxstr: UnixMsRegex,
format: ``, //API doesn't work here
name: UnixMs.String(),
min: 12,
}
}
func (unp unixMsProcessor) Format() string {
return unp.format
}
func (unp unixMsProcessor) Name() string {
return unp.name
}
func (unp unixMsProcessor) ToString(t time.Time) string {
return fmt.Sprintf("%d", t.UnixNano()/μs)
}
func (unp unixMsProcessor) ExtractionRegex() string {
return _unixMsCoreRegex
}
func (unp unixMsProcessor) Extract(d []byte, loc *time.Location) (t time.Time, ok bool, offset int) {
if len(d) < unp.min {
return
}
offset = -1
idx := unp.re.FindSubmatchIndex(d)
if len(idx) != 4 {
return
}
ms, err := strconv.ParseInt(string(d[idx[2]:idx[3]]), 10, 64)
if err != nil {
return
}
offset = idx[2]
t = time.Unix(0, ms*1000000).In(loc)
ok = true
return
}
func (unp unixMsProcessor) Match(d []byte) (start, end int, ok bool) {
if len(d) < unp.min {
return
}
idx := unp.re.FindSubmatchIndex(d)
if len(idx) == 4 {
start, end = idx[2], idx[3]
ok = true
}
return
}
type unixNanoProcessor struct {
re *regexp.Regexp
rxstr string
format string
name string
min int
}
// We assume you're not ingesting data from 1970, so we look for at least 16 digits of nanoseconds
func NewUnixNanoTimeProcessor() *unixNanoProcessor {
return &unixNanoProcessor{
re: regexp.MustCompile(UnixNanoRegex),
rxstr: UnixNanoRegex,
format: ``, //api doesn't work here
name: UnixNano.String(),
min: 18,
}
}
func (unp unixNanoProcessor) Format() string {
return unp.format
}
func (unp unixNanoProcessor) Name() string {
return unp.name
}
func (unp unixNanoProcessor) ExtractionRegex() string {
return _unixNanoCoreRegex
}
func (unp unixNanoProcessor) ToString(t time.Time) string {
return fmt.Sprintf("%d", t.UnixNano())
}
func (unp unixNanoProcessor) Extract(d []byte, loc *time.Location) (t time.Time, ok bool, offset int) {
if len(d) < unp.min {
return
}
offset = -1
idx := unp.re.FindSubmatchIndex(d)
if len(idx) != 4 {
return
}
nsec, err := strconv.ParseInt(string(d[idx[2]:idx[3]]), 10, 64)
if err != nil {
return
}
offset = idx[2]
t = time.Unix(0, nsec).In(loc)
ok = true
return
}
func (unp unixNanoProcessor) Match(d []byte) (start, end int, ok bool) {
if len(d) < unp.min {
return
}
idx := unp.re.FindSubmatchIndex(d)
if len(idx) == 4 {
start, end = idx[2], idx[3]
ok = true
}
return
}
func NewUK() Processor {
return &ukProc{
rx: regexp.MustCompile(UKRegex),
rxstr: UKRegex,
format: UKFormat,
name: UK.String(),
min: 20, //deal with low precision
}
}
type ukProc struct {
rx *regexp.Regexp
rxstr string
format string
name string
min int
}
func (p *ukProc) Format() string {
return p.format
}
func (p *ukProc) ToString(t time.Time) string {
return t.Format(`02/01/2006 15:04:05`) + "," + fmt.Sprintf("%d", t.Nanosecond()/int(μs))
}
func (p *ukProc) ExtractionRegex() string {
return p.rxstr
}
func (p *ukProc) Name() string {
return p.name
}
func (p *ukProc) Extract(d []byte, loc *time.Location) (time.Time, bool, int) {
if len(d) < p.min {
return time.Time{}, false, -1
}
idxs := p.rx.FindIndex(d)
if len(idxs) != 2 {
return time.Time{}, false, -1
}
t, err := p.parse(string(d[idxs[0]:idxs[1]]), loc)
if err != nil {
return time.Time{}, false, -1
}
return t, true, idxs[0]
}
func (p *ukProc) Match(d []byte) (start, end int, ok bool) {
if len(d) < p.min {
return
}
idxs := p.rx.FindIndex(d)
if len(idxs) == 2 {
start, end = idxs[0], idxs[1]
ok = true
}
return
}
func (p *ukProc) parse(value string, loc *time.Location) (time.Time, error) {
if i := strings.IndexByte(value, ','); i >= 0 {
t, err := time.Parse(p.format, value[:i]+"."+value[i+1:])
if err == nil {
return t, err
}
}
return time.Parse(p.format, value)
}
type ldapProcessor struct {
re *regexp.Regexp
rxstr string
format string
name string
min int
}
// We assume you're not ingesting data from 1970, so we look for at least 16 digits of nanoseconds
func NewLDAPProcessor() *ldapProcessor {
return &ldapProcessor{
re: regexp.MustCompile(LDAPRegex),
rxstr: LDAPRegex,
format: ``, //api doesn't work here
name: LDAP.String(),
min: 18,
}
}
func (lp ldapProcessor) Format() string {
return lp.format
}
func (lp ldapProcessor) Name() string {
return lp.name
}
func (lp ldapProcessor) ExtractionRegex() string {
return _ldapCoreRegex
}
func (lp ldapProcessor) ToString(t time.Time) string {
l := (t.Unix() + 11644473600) * 10000000
return fmt.Sprintf("%d", l)
}
func (lp ldapProcessor) Extract(d []byte, loc *time.Location) (t time.Time, ok bool, offset int) {
if len(d) < lp.min {
return
}
offset = -1
idx := lp.re.FindSubmatchIndex(d)
if len(idx) != 4 {
return
}
ldap, err := strconv.ParseInt(string(d[idx[2]:idx[3]]), 10, 64)
if err != nil {
return
}
offset = idx[2]
s := (ldap / 10000000) - 11644473600
t = time.Unix(s, 0).In(loc)
ok = true
return
}
func (lp ldapProcessor) Match(d []byte) (start, end int, ok bool) {
if len(d) < lp.min {
return
}
idx := lp.re.FindSubmatchIndex(d)
if len(idx) == 4 {
start, end = idx[2], idx[3]
ok = true
}
return
}
type unixSecondsProcessor struct {
re *regexp.Regexp
rxstr string
format string
name string
min int
}
func NewUnixSecondsProcessor() *unixSecondsProcessor {
return &unixSecondsProcessor{
re: regexp.MustCompile(UnixSecondsRegex),
rxstr: UnixSecondsRegex,
format: ``, //format API doesn't work here
name: UnixSeconds.String(),
min: 9, //see regex for why
}
}
func (up *unixSecondsProcessor) Format() string {
return up.format
}
func (up *unixSecondsProcessor) Name() string {
return up.name
}
func (up *unixSecondsProcessor) ToString(t time.Time) string {
uns := t.Unix()
return fmt.Sprintf("%d", uns)
}
func (up *unixSecondsProcessor) ExtractionRegex() string {
return _unixSecondsRegex
}
func (up unixSecondsProcessor) Extract(d []byte, loc *time.Location) (t time.Time, ok bool, offset int) {
if len(d) < up.min {
return
}
offset = -1
idx := up.re.FindSubmatchIndex(d)
if len(idx) != 4 {
return
}
s, err := strconv.ParseInt(string(d[idx[2]:idx[3]]), 10, 64)
if err != nil {
return
}
offset = idx[2]
t = time.Unix(s, 0).In(loc)
ok = true
return
}
func (up unixSecondsProcessor) Match(d []byte) (start, end int, ok bool) {
if len(d) < up.min {
return
}
idx := up.re.FindSubmatchIndex(d)
if len(idx) == 4 {
start, end = idx[2], idx[3]
ok = true
}
return
}
// tweakYear tries to figure out an appropriate year for the timestamp
// if the current year is zero.
func tweakYear(t time.Time) time.Time {
if t.Year() != 0 {
return t
}
now := time.Now()
year := now.Year()
// If setting the current year puts us too far in the future,
// more than 25 hours, we'll set the previous year instead.
if t.AddDate(year, 0, 0).Sub(now) > (25 * time.Hour) {
return t.AddDate(year-1, 0, 0)
}
return t.AddDate(year, 0, 0)
}