-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.go
220 lines (182 loc) · 6.1 KB
/
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"regexp"
"strconv"
"strings"
"time"
)
const (
errorTypeUnknown string = "_"
errorTypeNaxsiFmt string = "naxsi_fmt"
errorTypeNaxsiExLog string = "naxsi_exlog"
errorTypeFastcgi string = "fastcgi"
errorTypeOpenFailed string = "open_failed"
)
type naxsiFmtItem struct {
Zone string `json:"zone"`
ID string `json:"id"`
VarName string `json:"var_name"`
CScore string `json:"cscore"`
Score string `json:"score"`
}
type nginxErrorEntry struct {
// essential
Time string `json:"time"`
Level string `json:"level"`
PID int `json:"pid"`
TID int `json:"tid"`
CID int `json:"cid,omitempty"`
Message string `json:"message"`
// additional
Msg *string `json:"msg"`
checkSumParts []string
checkSumUseMsg bool
CheckSum string `json:"checksum"`
CheckSumDebug string `json:"checksum_debug"`
Client *string `json:"client,omitempty"`
Server *string `json:"server,omitempty"`
Host *string `json:"host,omitempty"`
Upstream *string `json:"upstream,omitempty"`
UpstreamHost *string `json:"upstream_host,omitempty"`
Referrer *string `json:"referrer,omitempty"`
ReferrerHost *string `json:"referrer_host,omitempty"`
RequestMethod *string `json:"request_method,omitempty"`
RequestURI *string `json:"request_uri,omitempty"`
RequestHTTPVersion *string `json:"request_http_version,omitempty"`
ErrorType string `json:"error_type,omitempty"`
ErrorDetails *string `json:"error_details,omitempty"`
// naxsi fmt
NaxsiFmtIP *string `json:"naxsi_fmt_ip,omitempty"`
NaxsiFmtServer *string `json:"naxsi_fmt_server,omitempty"`
NaxsiFmtURI *string `json:"naxsi_fmt_uri,omitempty"`
NaxsiFmtLearning *bool `json:"naxsi_fmt_learning,omitempty"`
NaxsiFmtVers *string `json:"naxsi_fmt_vers,omitempty"`
NaxsiFmtBlock *bool `json:"naxsi_fmt_block,omitempty"`
NaxsiFmtTotalProcessed *int `json:"naxsi_fmt_total_processed,omitempty"`
NaxsiFmtTotalBlocked *int `json:"naxsi_fmt_total_blocked,omitempty"`
NaxsiFmtItems []naxsiFmtItem `json:"naxsi_fmt_items,omitempty"`
// naxsi exlog
NaxsiExLogIP *string `json:"naxsi_exlog_ip,omitempty"`
NaxsiExLogServer *string `json:"naxsi_exlog_server,omitempty"`
NaxsiExLogURI *string `json:"naxsi_exlog_uri,omitempty"`
NaxsiExLogID *string `json:"naxsi_exlog_id,omitempty"`
NaxsiExLogZone *string `json:"naxsi_exlog_zone,omitempty"`
NaxsiExLogVarName *string `json:"naxsi_exlog_var_name,omitempty"`
NaxsiExLogContent *string `json:"naxsi_exlog_content,omitempty"`
}
var entryCIDRegex = regexp.MustCompile(`^(?s)(?P<time>[0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+:[0-9]+) \[(?P<level>[a-z]+)\] (?P<pid>[0-9]+)#(?P<tid>[0-9]+): \*(?P<cid>[0-9]+)(?P<message>.*)$`)
var entryNoCIDRegex = regexp.MustCompile(`^(?s)(?P<time>[0-9]+\/[0-9]+\/[0-9]+ [0-9]+:[0-9]+:[0-9]+) \[(?P<level>[a-z]+)\] (?P<pid>[0-9]+)#(?P<tid>[0-9]+): (?P<message>.*)$`)
var nginxErrorLogDateTimeLayout = "2006/01/02 15:04:05"
var noneAlphaRegex = regexp.MustCompile(`[^a-zA-Z]`)
var oneSpaceRegex = regexp.MustCompile(`[\s]+`)
func parserTime(timeString string) (t time.Time, e error) {
t, err := time.Parse(nginxErrorLogDateTimeLayout, timeString)
if err != nil {
return time.Time{}, err
}
return t, nil
}
func stripLog(message string) string {
s := noneAlphaRegex.ReplaceAllString(message, " ")
s = oneSpaceRegex.ReplaceAllString(s, " ")
return strings.ToLower(strings.TrimSpace(s))
}
func replaceMatched(message string, whole string) string {
return strings.Replace(message, whole, "", -1)
}
func parser(message string) (entry nginxErrorEntry, e error) {
var matched []string
var re *regexp.Regexp
if isMatched := entryCIDRegex.MatchString(message); isMatched {
re = entryCIDRegex
matched = entryCIDRegex.FindStringSubmatch(message)
} else if isMatched := entryNoCIDRegex.MatchString(message); isMatched {
re = entryNoCIDRegex
matched = entryNoCIDRegex.FindStringSubmatch(message)
} else {
return nginxErrorEntry{}, errors.New("packet is not valid")
}
result := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
result[name] = matched[i]
}
}
logTime, logTimeErr := parserTime(result["time"])
if logTimeErr != nil {
logTime = time.Now()
}
entry = nginxErrorEntry{
ErrorType: errorTypeUnknown,
Time: logTime.Format(time.RFC1123Z),
PID: prettySureInt(result["pid"]),
TID: prettySureInt(result["tid"]),
Level: strings.ToLower(strings.TrimSpace(result["level"])),
Message: strings.TrimSpace(result["message"]),
checkSumUseMsg: true,
}
if result["cid"] != "" {
entry.CID = prettySureInt(result["cid"])
}
entry.Msg = stringPointer(entry.Message)
entry.NaxsiFmtItems = make([]naxsiFmtItem, 0)
// general
findClient(&entry)
findServer(&entry)
findHost(&entry)
findUpstream(&entry)
findReferrer(&entry)
findRequest(&entry)
entry.checkSumParts = []string{*entry.Msg, entry.Level}
// specials
findErrorFastCGI(&entry)
findErrorOpenFailed(&entry)
// naxsi
findNaxsiFmt(&entry)
findNaxsiExLog(&entry)
entry.Msg = stringPointer(stripLog(*entry.Msg))
if entry.checkSumUseMsg {
entry.checkSumParts = []string{*entry.Msg}
}
checkSumParts := strings.Join(entry.checkSumParts, ":")
entry.CheckSumDebug = checkSumParts
hash := sha1.Sum([]byte(checkSumParts))
entry.CheckSum = hex.EncodeToString(hash[:])
return entry, nil
}
func isJSON(str string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(str), &js) == nil
}
func parserJSON(entry nginxErrorEntry) ([]byte, error) {
return json.Marshal(entry)
}
func prettySureInt(s string) int {
i, e := strconv.Atoi(s)
if e != nil {
return 0
}
return i
}
func stringPointer(s string) *string {
if s == "" {
return nil
}
var pt *string
pt = &s
return pt
}
func boolPointer(b bool) *bool {
var pt *bool
pt = &b
return pt
}
func intPointer(i int) *int {
var pt *int
pt = &i
return pt
}