-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlinelog.go
99 lines (83 loc) · 2.29 KB
/
linelog.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
package main
import (
"net"
"regexp"
"strconv"
"time"
)
var matcherClasic []string
var matcherApp []string
var reClasic *regexp.Regexp
var reApp *regexp.Regexp
func init() {
reClasic = regexp.MustCompile(`(?P<date>[^Z]+Z) (?P<elb>[^\s]+) (?P<ipclient>[^:]+?):[0-9]+ (?P<ipnode>[^:]+?):[0-9]+ (?P<reqtime>[0-9\.]+) (?P<backtime>[0-9\.]+) (?P<restime>[0-9\.]+) (?P<elbcode>[0-9]{3}) (?P<backcode>[0-9]{3}) (?P<lenght1>[0-9]+) (?P<lenght2>[0-9]+) "(?P<Method>[A-Z]+) (?P<URL>[^"]+) HTTP/[0-9\.]+".*`)
matcherClasic = reClasic.SubexpNames()
reApp = regexp.MustCompile(`(?P<proto>[^\s]+) (?P<date>[^Z]+Z) (?P<target>[^\s]+) (?P<ipclient>[^:]+?):[0-9]+ (?P<ipnode>[^:]+?):[0-9]+ (?P<reqtime>[0-9\.]+) (?P<backtime>[-0-9\.]+) (?P<restime>[-0-9\.]+) (?P<elbcode>[0-9]{3}) (?P<backcode>([0-9]{3}|-)) (?P<lenght1>[0-9]+) (?P<lenght2>[0-9]+) "(?P<Method>[A-Z]+) (?P<URL>[^"]+) HTTP/[0-9\.]+".*`)
matcherApp = reApp.SubexpNames()
}
// LineLog is the struct to analyce and store a line
type LineLog struct {
URL string
Time time.Time
Elapsed float64
Method string
IPClient net.IP
Seek int64
Filelog *FileLog
Len int
}
// NewLineLog create an structure of anlyzce record
func NewLineLog(raw string, filelog *FileLog, seek int64) *LineLog {
line := &LineLog{
Filelog: filelog,
Seek: seek,
Len: len([]byte(raw)),
}
line.parse(raw)
return line
}
// parse the raw record with regular expresion and store in the struct
func (l *LineLog) parse(raw string) {
var r [][]string
var matcher []string
if l.Filelog.ElbType == "classic" {
r = reClasic.FindAllStringSubmatch(raw, -1)
matcher = matcherClasic
} else {
r = reApp.FindAllStringSubmatch(raw, -1)
matcher = matcherApp
}
if r == nil {
return
}
for i, n := range r[0] {
switch matcher[i] {
case "date":
l.Time, _ = time.Parse(time.RFC3339Nano, n)
break
case "ipclient":
if analyze {
l.IPClient = net.ParseIP(n)
}
break
case "reqtime":
if analyze {
_time, _ := strconv.ParseFloat(n, 64)
l.Elapsed = l.Elapsed + _time
}
break
case "backtime":
if analyze {
_time, _ := strconv.ParseFloat(n, 64)
l.Elapsed = l.Elapsed + _time
}
break
case "restime":
if analyze {
_time, _ := strconv.ParseFloat(n, 64)
l.Elapsed = l.Elapsed + _time
}
break
}
}
}