-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsyslogparser.go
238 lines (181 loc) · 3.91 KB
/
syslogparser.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package syslogparser
import (
"fmt"
"strconv"
"strings"
"time"
)
const (
PRI_PART_START = '<'
PRI_PART_END = '>'
NO_VERSION = -1
)
var (
ErrEOL = &ParserError{"End of log line"}
ErrNoSpace = &ParserError{"No space found"}
ErrPriorityNoStart = &ParserError{"No start char found for priority"}
ErrPriorityEmpty = &ParserError{"Priority field empty"}
ErrPriorityNoEnd = &ParserError{"No end char found for priority"}
ErrPriorityTooShort = &ParserError{"Priority field too short"}
ErrPriorityTooLong = &ParserError{"Priority field too long"}
ErrPriorityNonDigit = &ParserError{"Non digit found in priority"}
ErrVersionNotFound = &ParserError{"Can not find version"}
ErrTimestampUnknownFormat = &ParserError{"Timestamp format unknown"}
)
type RFC uint8
const (
RFC_UNKNOWN = iota
RFC_3164
RFC_5424
)
type LogParser interface {
Parse() error
Dump() LogParts
Location(*time.Location)
}
type ParserError struct {
ErrorString string
}
type Priority struct {
P int
F Facility
S Severity
}
type Facility struct {
Value int
}
type Severity struct {
Value int
}
type LogParts map[string]interface{}
// https://tools.ietf.org/html/rfc3164#section-4.1
func ParsePriority(buff []byte, cursor *int, l int) (Priority, error) {
pri := newPriority(0)
if l <= 0 {
return pri, ErrPriorityEmpty
}
if buff[*cursor] != PRI_PART_START {
return pri, ErrPriorityNoStart
}
i := 1
priDigit := 0
for i < l {
if i >= 5 {
return pri, ErrPriorityTooLong
}
c := buff[i]
if c == PRI_PART_END {
if i == 1 {
return pri, ErrPriorityTooShort
}
*cursor = i + 1
return newPriority(priDigit), nil
}
if IsDigit(c) {
v, e := strconv.Atoi(string(c))
if e != nil {
return pri, e
}
priDigit = (priDigit * 10) + v
} else {
return pri, ErrPriorityNonDigit
}
i++
}
return pri, ErrPriorityNoEnd
}
// https://tools.ietf.org/html/rfc5424#section-6.2.2
func ParseVersion(buff []byte, cursor *int, l int) (int, error) {
if *cursor >= l {
return NO_VERSION, ErrVersionNotFound
}
c := buff[*cursor]
*cursor++
// XXX : not a version, not an error though as RFC 3164 does not support it
if !IsDigit(c) {
return NO_VERSION, nil
}
v, e := strconv.Atoi(string(c))
if e != nil {
*cursor--
return NO_VERSION, e
}
return v, nil
}
func IsDigit(c byte) bool {
return c >= '0' && c <= '9'
}
func newPriority(p int) Priority {
// The Priority value is calculated by first multiplying the Facility
// number by 8 and then adding the numerical value of the Severity.
return Priority{
P: p,
F: Facility{Value: p / 8},
S: Severity{Value: p % 8},
}
}
func FindNextSpace(buff []byte, from int, l int) (int, error) {
var to int
for to = from; to < l; to++ {
if buff[to] == ' ' {
to++
return to, nil
}
}
return 0, ErrNoSpace
}
func Parse2Digits(buff []byte, cursor *int, l int, min int, max int, e error) (int, error) {
digitLen := 2
if *cursor+digitLen > l {
return 0, ErrEOL
}
sub := string(buff[*cursor : *cursor+digitLen])
*cursor += digitLen
i, err := strconv.Atoi(sub)
if err != nil {
return 0, e
}
if i >= min && i <= max {
return i, nil
}
return 0, e
}
func ParseHostname(buff []byte, cursor *int, l int) (string, error) {
from := *cursor
var to int
for to = from; to < l; to++ {
if buff[to] == ' ' {
break
}
}
hostname := buff[from:to]
*cursor = to
return string(hostname), nil
}
func ShowCursorPos(buff []byte, cursor int) {
fmt.Println(string(buff))
padding := strings.Repeat("-", cursor)
fmt.Println(padding + "↑\n")
}
func (err *ParserError) Error() string {
return err.ErrorString
}
func DetectRFC(buff []byte) (RFC, error) {
max := 10
var v int
var err error
for i := 0; i < max; i++ {
if buff[i] == '>' && i < max {
x := i + 1
v, err = ParseVersion(buff, &x, max)
break
}
}
if err != nil {
return RFC_UNKNOWN, err
}
if v == NO_VERSION {
return RFC_3164, nil
}
return RFC_5424, nil
}