-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfilter.go
93 lines (81 loc) · 2.66 KB
/
filter.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
package dev
import (
"regexp"
"strconv"
)
// FilterTable stores line filters for custom line-by-line processing of configuration.
type FilterTable struct {
table map[string]FilterFunc
re1 *regexp.Regexp
re2 *regexp.Regexp
re3 *regexp.Regexp
re4 *regexp.Regexp
}
// FilterFunc is a helper function type for line filters.
type FilterFunc func(hasPrintf, bool, *FilterTable, []byte, int) []byte
// NewFilterTable creates a filter table.
func NewFilterTable(logger hasPrintf) *FilterTable {
t := &FilterTable{
table: map[string]FilterFunc{},
re1: regexp.MustCompile(`^\w{3}\s\w{3}\s\d{1,2}\s`), // Thu Feb 11 15:45:43.545 BRST
re2: regexp.MustCompile(`^Building`), // Building configuration...
re3: regexp.MustCompile(`^!! Last`), // !! Last configuration change at Tue Jan 26 16:40:46 2016 by user
re4: regexp.MustCompile(`^\w+ uptime is `), // asr9010 uptime is 9 years, 2 weeks, 5 days, 20 hours, 3 minutes
}
registerFilters(logger, t.table)
return t
}
func register(logger hasPrintf, table map[string]FilterFunc, name string, f FilterFunc) {
logger.Printf("line filter registered: '%s'", name)
table[name] = f
}
func registerFilters(logger hasPrintf, table map[string]FilterFunc) {
register(logger, table, "iosxr", filterIOSXR)
register(logger, table, "noop", filterNoop)
register(logger, table, "drop", filterDrop)
register(logger, table, "count_lines", filterCountLines)
}
func filterDrop(logger hasPrintf, debug bool, table *FilterTable, line []byte, lineNum int) []byte {
return []byte{}
}
func filterNoop(logger hasPrintf, debug bool, table *FilterTable, line []byte, lineNum int) []byte {
return line
}
func filterCountLines(logger hasPrintf, debug bool, table *FilterTable, line []byte, lineNum int) []byte {
line = append([]byte(strconv.Itoa(lineNum)+": "), line...)
return line
}
/*
Thu Feb 11 15:45:43.545 BRST
Building configuration...
!! IOS XR Configuration 5.1.3
!! Last configuration change at Tue Jan 26 16:40:46 2016 by user
asr9010 uptime is 9 years, 2 weeks, 5 days, 20 hours, 3 minutes
*/
func filterIOSXR(logger hasPrintf, debug bool, table *FilterTable, line []byte, lineNum int) []byte {
if table.re1.Match(line) {
if debug {
logger.Printf("filterIOSXR: drop: [%s]", string(line))
}
return []byte{}
}
if table.re2.Match(line) {
if debug {
logger.Printf("filterIOSXR: drop: [%s]", string(line))
}
return []byte{}
}
if table.re3.Match(line) {
if debug {
logger.Printf("filterIOSXR: drop: [%s]", string(line))
}
return []byte{}
}
if table.re4.Match(line) {
if debug {
logger.Printf("filterIOSXR: drop: [%s]", string(line))
}
return []byte{}
}
return line
}