-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapache_parser.go
89 lines (76 loc) · 1.96 KB
/
apache_parser.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
package log2ltsv
import (
"fmt"
"regexp"
)
type ApacheLog struct {
remoteHost string
user string
time string
method string
uri string
protocol string
statusCode string
bytes string
referer string
userAgent string
requestTime string
}
type ApacheParser struct {
log *ApacheLog
}
func NewApacheParser() *ApacheParser {
return &ApacheParser{}
}
func (ap *ApacheParser) parse(line string) error {
apacheLogRE := regexp.MustCompile(`^(\S+)\s` + // remote host
`\S+\s+` +
`(\S+\s+)+` + // user
`\[([^]]+)\]\s` + // time
`"(\S*)\s?` + // method
`((?:[^"]*(?:\\")?)*)\s` + // URL
`([^"]*)"\s` + // protocol
`(\S+)\s` + // status code
`(\S+)\s` + // bytes
`"((?:[^"]*(?:\\")?)*)"\s` + // referer
`"(.*)"` + // user agent
`\s(.*)$`) // request_time
group := apacheLogRE.FindStringSubmatch(line)
if len(group) < 1 {
return fmt.Errorf("invalid log format")
}
t, err := stringToISO8601(group[3])
if err != nil {
return err
}
ap.log = &ApacheLog{
remoteHost: group[1],
user: group[2],
time: t,
method: group[4],
uri: group[5],
protocol: group[6],
statusCode: group[7],
bytes: group[8],
referer: group[9],
userAgent: group[10],
requestTime: group[11],
}
return nil
}
func (ap *ApacheParser) ParseAndOutput(line string) (string, error) {
err := ap.parse(line)
if err != nil {
return "", err
}
values := make([]string, 0)
values = append(values, makeValue("user", ap.log.user))
values = append(values, makeValue("time", ap.log.time))
values = append(values, makeValue("method", ap.log.method))
values = append(values, makeValue("uri", ap.log.uri))
values = append(values, makeValue("status", ap.log.statusCode))
values = append(values, makeValue("size", ap.log.bytes))
values = append(values, makeValue("reqtime", ap.log.requestTime))
values = append(values, makeValue("apptime", ap.log.requestTime))
return toLtsv(values), nil
}