-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseoperator.go
93 lines (83 loc) · 2.07 KB
/
parseoperator.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 iislog
import (
"time"
"strings"
"github.com/simulot/golib/file/walker"
"github.com/simulot/golib/pipeline"
"github.com/simulot/iislog/iis"
)
// ParserOperator create a operator for the pipeline in charge of
// opening the log parser and output matched lines
func (a *Application) ParserOperator() pipeline.Operator {
filter := a.MakeLogRecordFilter()
return func(in, out chan interface{}) {
for i := range in {
if item, ok := i.(walker.WalkItem); ok {
if r, err := item.Reader(); err == nil {
p := iis.NewLogParser(r)
recChan := p.Parse(filter)
for rec := range recChan {
out <- rec
}
}
item.Close()
} else {
panic("Expecting walker.WalkItem in pipeline.Operator filter")
}
}
}
}
type filter struct {
a *Application
}
func (f *filter) CheckFullLine(line *string) bool { return true }
func (f *filter) CheckDate(date time.Time) bool {
return f.a.dateFrom.Before(date) && f.a.dateTo.After(date)
}
func (f *filter) CheckField(field string, value interface{}) (ret bool) {
ret = true
if ret && f.a.longQueries != 0 && field == "time-taken" {
if value.(time.Duration) < f.a.longQueries {
ret = false
}
}
if ret && field == "cs-uri-stem" {
if f.a.hideAssets {
url := value.(string)
if dot := strings.LastIndex(url, "."); dot >= 0 {
switch strings.ToLower(url[dot:]) {
case ".jpg", ".gif", ".png", ".css", ".js", ".ico", ".html":
ret = false
}
}
}
if ret && len(f.a.urls) > 0 {
ret = false
for _, u := range f.a.urls {
if strings.Contains(value.(string), u) {
ret = true
break
}
}
}
}
if ret && len(f.a.users) > 0 && field == "cs-username" {
ret = false
for _, u := range f.a.users {
if strings.Contains(value.(string), u) {
ret = true
break
}
}
}
return
}
func (f *filter) CheckError(status, substatus int) bool {
if f.a.protocolError && status != 0 && (status < 400 || status >= 600 || (status == 401 && substatus == 2)) {
return false
}
return true
}
func (a *Application) MakeLogRecordFilter() iis.RecordFilter {
return &filter{a}
}