-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
parser.go
319 lines (273 loc) · 8.28 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package syslog // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/syslog"
import (
"bytes"
"context"
"fmt"
"regexp"
"time"
sl "github.com/influxdata/go-syslog/v3"
"github.com/influxdata/go-syslog/v3/nontransparent"
"github.com/influxdata/go-syslog/v3/octetcounting"
"github.com/influxdata/go-syslog/v3/rfc3164"
"github.com/influxdata/go-syslog/v3/rfc5424"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)
var priRegex = regexp.MustCompile(`\<\d{1,3}\>`)
// parseFunc a parseFunc determines how the raw input is to be parsed into a syslog message
type parseFunc func(input []byte) (sl.Message, error)
// Parser is an operator that parses syslog.
type Parser struct {
helper.ParserOperator
protocol string
location *time.Location
enableOctetCounting bool
allowSkipPriHeader bool
nonTransparentFramingTrailer *string
}
// Process will parse an entry field as syslog.
func (p *Parser) Process(ctx context.Context, entry *entry.Entry) error {
// if pri header is missing and this is an expected behavior then facility and severity values should be skipped.
if !p.enableOctetCounting && p.allowSkipPriHeader {
bytes, err := toBytes(entry.Body)
if err != nil {
return err
}
if p.shouldSkipPriorityValues(bytes) {
return p.ParserOperator.ProcessWithCallback(ctx, entry, p.parse, postprocessWithoutPriHeader)
}
}
return p.ParserOperator.ProcessWithCallback(ctx, entry, p.parse, postprocess)
}
// parse will parse a value as syslog.
func (p *Parser) parse(value any) (any, error) {
bytes, err := toBytes(value)
if err != nil {
return nil, err
}
pFunc, err := p.buildParseFunc()
if err != nil {
return nil, err
}
slog, err := pFunc(bytes)
if err != nil {
return nil, err
}
skipPriHeaderValues := p.shouldSkipPriorityValues(bytes)
switch message := slog.(type) {
case *rfc3164.SyslogMessage:
return p.parseRFC3164(message, skipPriHeaderValues)
case *rfc5424.SyslogMessage:
return p.parseRFC5424(message, skipPriHeaderValues)
default:
return nil, fmt.Errorf("parsed value was not rfc3164 or rfc5424 compliant")
}
}
func (p *Parser) buildParseFunc() (parseFunc, error) {
switch p.protocol {
case RFC3164:
return func(input []byte) (sl.Message, error) {
if p.allowSkipPriHeader && !priRegex.Match(input) {
input = append([]byte("<0>"), input...)
}
return rfc3164.NewMachine(rfc3164.WithLocaleTimezone(p.location)).Parse(input)
}, nil
case RFC5424:
switch {
// Octet Counting Parsing RFC6587
case p.enableOctetCounting:
return newOctetCountingParseFunc(), nil
// Non-Transparent-Framing Parsing RFC6587
case p.nonTransparentFramingTrailer != nil && *p.nonTransparentFramingTrailer == LFTrailer:
return newNonTransparentFramingParseFunc(nontransparent.LF), nil
case p.nonTransparentFramingTrailer != nil && *p.nonTransparentFramingTrailer == NULTrailer:
return newNonTransparentFramingParseFunc(nontransparent.NUL), nil
// Raw RFC5424 parsing
default:
return func(input []byte) (sl.Message, error) {
if p.allowSkipPriHeader && !priRegex.Match(input) {
input = append([]byte("<0>"), input...)
}
return rfc5424.NewMachine().Parse(input)
}, nil
}
default:
return nil, fmt.Errorf("invalid protocol %s", p.protocol)
}
}
func (p *Parser) shouldSkipPriorityValues(value []byte) bool {
if !p.enableOctetCounting && p.allowSkipPriHeader {
// check if entry starts with '<'.
// if not it means that the pre header was missing from the body and hence we should skip it.
if len(value) > 1 && value[0] != '<' {
return true
}
}
return false
}
// parseRFC3164 will parse an RFC3164 syslog message.
func (p *Parser) parseRFC3164(syslogMessage *rfc3164.SyslogMessage, skipPriHeaderValues bool) (map[string]any, error) {
value := map[string]any{
"timestamp": syslogMessage.Timestamp,
"hostname": syslogMessage.Hostname,
"appname": syslogMessage.Appname,
"proc_id": syslogMessage.ProcID,
"msg_id": syslogMessage.MsgID,
"message": syslogMessage.Message,
}
if !skipPriHeaderValues {
value["priority"] = syslogMessage.Priority
value["severity"] = syslogMessage.Severity
value["facility"] = syslogMessage.Facility
}
return p.toSafeMap(value)
}
// parseRFC5424 will parse an RFC5424 syslog message.
func (p *Parser) parseRFC5424(syslogMessage *rfc5424.SyslogMessage, skipPriHeaderValues bool) (map[string]any, error) {
value := map[string]any{
"timestamp": syslogMessage.Timestamp,
"hostname": syslogMessage.Hostname,
"appname": syslogMessage.Appname,
"proc_id": syslogMessage.ProcID,
"msg_id": syslogMessage.MsgID,
"message": syslogMessage.Message,
"structured_data": syslogMessage.StructuredData,
"version": syslogMessage.Version,
}
if !skipPriHeaderValues {
value["priority"] = syslogMessage.Priority
value["severity"] = syslogMessage.Severity
value["facility"] = syslogMessage.Facility
}
return p.toSafeMap(value)
}
// toSafeMap will dereference any pointers on the supplied map.
func (p *Parser) toSafeMap(message map[string]any) (map[string]any, error) {
for key, val := range message {
switch v := val.(type) {
case *string:
if v == nil {
delete(message, key)
continue
}
message[key] = *v
case *uint8:
if v == nil {
delete(message, key)
continue
}
message[key] = int(*v)
case uint16:
message[key] = int(v)
case *time.Time:
if v == nil {
delete(message, key)
continue
}
message[key] = *v
case *map[string]map[string]string:
if v == nil {
delete(message, key)
continue
}
message[key] = convertMap(*v)
default:
return nil, fmt.Errorf("key %s has unknown field of type %T", key, v)
}
}
return message, nil
}
// convertMap converts map[string]map[string]string to map[string]any
// which is expected by stanza converter
func convertMap(data map[string]map[string]string) map[string]any {
ret := map[string]any{}
for key, value := range data {
ret[key] = map[string]any{}
r := ret[key].(map[string]any)
for k, v := range value {
r[k] = v
}
}
return ret
}
func toBytes(value any) ([]byte, error) {
switch v := value.(type) {
case string:
return []byte(v), nil
default:
return nil, fmt.Errorf("unable to convert type '%T' to bytes", value)
}
}
var severityMapping = [...]entry.Severity{
0: entry.Fatal,
1: entry.Error3,
2: entry.Error2,
3: entry.Error,
4: entry.Warn,
5: entry.Info2,
6: entry.Info,
7: entry.Debug,
}
var severityText = [...]string{
0: "emerg",
1: "alert",
2: "crit",
3: "err",
4: "warning",
5: "notice",
6: "info",
7: "debug",
}
var severityField = entry.NewAttributeField("severity")
func cleanupTimestamp(e *entry.Entry) error {
_, ok := entry.NewAttributeField("timestamp").Delete(e)
if !ok {
return fmt.Errorf("failed to cleanup timestamp")
}
return nil
}
func postprocessWithoutPriHeader(e *entry.Entry) error {
return cleanupTimestamp(e)
}
func postprocess(e *entry.Entry) error {
sev, ok := severityField.Delete(e)
if !ok {
return fmt.Errorf("severity field does not exist")
}
sevInt, ok := sev.(int)
if !ok {
return fmt.Errorf("severity field is not an int")
}
if sevInt < 0 || sevInt > 7 {
return fmt.Errorf("invalid severity '%d'", sevInt)
}
e.Severity = severityMapping[sevInt]
e.SeverityText = severityText[sevInt]
return cleanupTimestamp(e)
}
func newOctetCountingParseFunc() parseFunc {
return func(input []byte) (message sl.Message, err error) {
listener := func(res *sl.Result) {
message = res.Message
err = res.Error
}
parser := octetcounting.NewParser(sl.WithBestEffort(), sl.WithListener(listener))
reader := bytes.NewReader(input)
parser.Parse(reader)
return
}
}
func newNonTransparentFramingParseFunc(trailerType nontransparent.TrailerType) parseFunc {
return func(input []byte) (message sl.Message, err error) {
listener := func(res *sl.Result) {
message = res.Message
err = res.Error
}
parser := nontransparent.NewParser(sl.WithBestEffort(), nontransparent.WithTrailer(trailerType), sl.WithListener(listener))
reader := bytes.NewReader(input)
parser.Parse(reader)
return
}
}