-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
186 lines (167 loc) · 4.39 KB
/
log.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
package main
import (
"fmt"
"net"
"strconv"
"strings"
"time"
)
type logParser interface {
parse(logLine string) *logEntry
readOptions(map[string]interface{}) error
}
type LogFormat int
const (
HaproxyHTTPLogFormat LogFormat = iota
)
func (f *LogFormat) UnmarshalText(b []byte) error {
switch string(b) {
case "HaproxyHTTP":
*f = HaproxyHTTPLogFormat
default:
return fmt.Errorf("Unrecognized log format type %s\n", string(b))
}
return nil
}
func (f *LogFormat) parser() logParser {
switch *f {
case HaproxyHTTPLogFormat:
return &HaproxyHTTPLogParser{}
}
return nil
}
type HaproxyHTTPLogParser struct {
Options struct {
UserAgentRequestHeader *uint
HostRequestHeader *uint
TrueClientIPHeader *uint
}
}
type logEntry struct {
clientIP *net.IP
cdnIP *net.IP
timestamp time.Time
host Dimension
backend Dimension
frontend Dimension
useragent Dimension
}
func UintPtr(i uint) *uint { return &i }
func (p *HaproxyHTTPLogParser) readOptions(options map[string]interface{}) error {
converter := func(s string) (*uint, error) {
i, err := strconv.Atoi(s)
var result uint
if err != nil {
return &result, err
}
result = uint(i)
return &result, nil
}
var err error
for k, v := range options {
switch k {
case "UserAgentRequestHeader":
switch v := v.(type) {
case string:
if p.Options.UserAgentRequestHeader, err = converter(v); err != nil {
return fmt.Errorf("Invalid value for option %s: %s", k, v)
}
case int64:
p.Options.UserAgentRequestHeader = UintPtr(uint(v))
}
case "HostRequestHeader":
switch v := v.(type) {
case string:
if p.Options.HostRequestHeader, err = converter(v); err != nil {
return fmt.Errorf("Invalid value for option %s: %s", k, v)
}
case int64:
p.Options.HostRequestHeader = UintPtr(uint(v))
}
case "TrueClientIPRequestHeader":
switch v := v.(type) {
case string:
if p.Options.TrueClientIPHeader, err = converter(v); err != nil {
return fmt.Errorf("Invalid value for option %s: %s", k, v)
}
case int64:
p.Options.TrueClientIPHeader = UintPtr(uint(v))
}
default:
return fmt.Errorf("Unrecognized option %s", k)
}
}
if p.Options.TrueClientIPHeader == nil || p.Options.HostRequestHeader == nil {
return fmt.Errorf("Must specify HostRequestHeader and TrueClientIPRequestHeader at minimum.")
}
return nil
}
// parseLog takes in an haproxy log line and returns a logEntry.
func (p *HaproxyHTTPLogParser) parse(logLine string) *logEntry {
var entry logEntry
if logLine == "" {
return nil
}
// This string parsing stuff was lifted from TPS
var a, b int
a = 0
if b = strings.Index(logLine[a:], ":"); b == -1 {
return nil
}
clientIPString := logLine[a : a+b]
clientIP := net.ParseIP(clientIPString)
if clientIP == nil {
return nil
}
entry.clientIP = &clientIP
logLine = logLine[a+b:]
// The subsequent square-bracketed string contains our timestamp
if a = strings.Index(logLine, "[") + 1; a == -1 {
return &entry
}
if b = strings.Index(logLine[a:], "]"); b == -1 {
return &entry
}
timestampStr := logLine[a : a+b]
entry.timestamp, _ = time.Parse("02/Jan/2006:15:04:05.999", timestampStr)
logLine = logLine[a+b:]
// The subsequent string is our frontend
if a = strings.Index(logLine, " ") + 1; a == -1 {
return &entry
}
if b = strings.Index(logLine[a:], " "); b == -1 {
return &entry
}
entry.frontend = Dimension{Type: DimensionFrontend, Value: logLine[a : a+b]}
logLine = logLine[a+b:]
// The subsequent string is our backend
if a = strings.Index(logLine, " ") + 1; a == -1 {
return &entry
}
if b = strings.Index(logLine[a:], "/"); b == -1 {
return &entry
}
entry.backend = Dimension{Type: DimensionBackend, Value: logLine[a : a+b]}
logLine = logLine[a+b:]
// The first curly-braced block contains our request headers
if a = strings.Index(logLine, "{"); a == -1 {
return &entry
}
if b = strings.Index(logLine[a:], "}"); b == -1 {
return &entry
}
bracketedHeaders := logLine[a : a+b]
headers := strings.Split(bracketedHeaders, "|")
if len(headers) < 7 {
return &entry
}
entry.useragent = Dimension{Type: DimensionUseragent, Value: headers[*p.Options.UserAgentRequestHeader]}
entry.host = Dimension{Type: DimensionHost, Value: headers[*p.Options.HostRequestHeader]}
ipString := headers[*p.Options.TrueClientIPHeader]
cdnIP := net.ParseIP(ipString)
if cdnIP == nil {
return &entry
}
entry.cdnIP = &cdnIP
return &entry
}