forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.go
360 lines (312 loc) · 9.95 KB
/
syslog.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// 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"
"errors"
"fmt"
"strings"
"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"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)
const (
operatorType = "syslog_parser"
RFC3164 = "rfc3164"
RFC5424 = "rfc5424"
NULTrailer = "NUL"
LFTrailer = "LF"
)
func init() {
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}
// NewConfig creates a new syslog parser config with default values
func NewConfig() *Config {
return NewConfigWithID(operatorType)
}
// NewConfigWithID creates a new syslog parser config with default values
func NewConfigWithID(operatorID string) *Config {
return &Config{
ParserConfig: helper.NewParserConfig(operatorID, operatorType),
}
}
// Config is the configuration of a syslog parser operator.
type Config struct {
helper.ParserConfig `mapstructure:",squash"`
BaseConfig `mapstructure:",squash"`
}
// BaseConfig is the detailed configuration of a syslog parser.
type BaseConfig struct {
Protocol string `mapstructure:"protocol,omitempty"`
Location string `mapstructure:"location,omitempty"`
EnableOctetCounting bool `mapstructure:"enable_octet_counting,omitempty"`
NonTransparentFramingTrailer *string `mapstructure:"non_transparent_framing_trailer,omitempty"`
}
// Build will build a JSON parser operator.
func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
if c.ParserConfig.TimeParser == nil {
parseFromField := entry.NewAttributeField("timestamp")
c.ParserConfig.TimeParser = &helper.TimeParser{
ParseFrom: &parseFromField,
LayoutType: helper.NativeKey,
}
}
parserOperator, err := c.ParserConfig.Build(logger)
if err != nil {
return nil, err
}
proto := strings.ToLower(c.Protocol)
switch {
case proto == "":
return nil, fmt.Errorf("missing field 'protocol'")
case proto != RFC5424 && (c.NonTransparentFramingTrailer != nil || c.EnableOctetCounting):
return nil, errors.New("octet_counting and non_transparent_framing are only compatible with protocol rfc5424")
case proto == RFC5424 && (c.NonTransparentFramingTrailer != nil && c.EnableOctetCounting):
return nil, errors.New("only one of octet_counting or non_transparent_framing can be enabled")
case proto == RFC5424 && c.NonTransparentFramingTrailer != nil:
if *c.NonTransparentFramingTrailer != NULTrailer && *c.NonTransparentFramingTrailer != LFTrailer {
return nil, fmt.Errorf("invalid non_transparent_framing_trailer '%s'. Must be either 'LF' or 'NUL'", *c.NonTransparentFramingTrailer)
}
case proto != RFC5424 && proto != RFC3164:
return nil, fmt.Errorf("unsupported protocol version: %s", proto)
}
if c.Location == "" {
c.Location = "UTC"
}
location, err := time.LoadLocation(c.Location)
if err != nil {
return nil, fmt.Errorf("failed to load location %s: %w", c.Location, err)
}
return &Parser{
ParserOperator: parserOperator,
protocol: proto,
location: location,
enableOctetCounting: c.EnableOctetCounting,
nonTransparentFramingTrailer: c.NonTransparentFramingTrailer,
}, nil
}
// parseFunc a parseFunc determines how the raw input is to be parsed into a syslog message
type parseFunc func(input []byte) (sl.Message, error)
func (s *Parser) buildParseFunc() (parseFunc, error) {
switch s.protocol {
case RFC3164:
return func(input []byte) (sl.Message, error) {
return rfc3164.NewMachine(rfc3164.WithLocaleTimezone(s.location)).Parse(input)
}, nil
case RFC5424:
switch {
// Octet Counting Parsing RFC6587
case s.enableOctetCounting:
return newOctetCountingParseFunc(), nil
// Non-Transparent-Framing Parsing RFC6587
case s.nonTransparentFramingTrailer != nil && *s.nonTransparentFramingTrailer == LFTrailer:
return newNonTransparentFramingParseFunc(nontransparent.LF), nil
case s.nonTransparentFramingTrailer != nil && *s.nonTransparentFramingTrailer == NULTrailer:
return newNonTransparentFramingParseFunc(nontransparent.NUL), nil
// Raw RFC5424 parsing
default:
return func(input []byte) (sl.Message, error) {
return rfc5424.NewMachine().Parse(input)
}, nil
}
default:
return nil, fmt.Errorf("invalid protocol %s", s.protocol)
}
}
// Parser is an operator that parses syslog.
type Parser struct {
helper.ParserOperator
protocol string
location *time.Location
enableOctetCounting bool
nonTransparentFramingTrailer *string
}
// Process will parse an entry field as syslog.
func (s *Parser) Process(ctx context.Context, entry *entry.Entry) error {
return s.ParserOperator.ProcessWithCallback(ctx, entry, s.parse, postprocess)
}
// parse will parse a value as syslog.
func (s *Parser) parse(value any) (any, error) {
bytes, err := toBytes(value)
if err != nil {
return nil, err
}
pFunc, err := s.buildParseFunc()
if err != nil {
return nil, err
}
slog, err := pFunc(bytes)
if err != nil {
return nil, err
}
switch message := slog.(type) {
case *rfc3164.SyslogMessage:
return s.parseRFC3164(message)
case *rfc5424.SyslogMessage:
return s.parseRFC5424(message)
default:
return nil, fmt.Errorf("parsed value was not rfc3164 or rfc5424 compliant")
}
}
// parseRFC3164 will parse an RFC3164 syslog message.
func (s *Parser) parseRFC3164(syslogMessage *rfc3164.SyslogMessage) (map[string]any, error) {
value := map[string]any{
"timestamp": syslogMessage.Timestamp,
"priority": syslogMessage.Priority,
"facility": syslogMessage.Facility,
"severity": syslogMessage.Severity,
"hostname": syslogMessage.Hostname,
"appname": syslogMessage.Appname,
"proc_id": syslogMessage.ProcID,
"msg_id": syslogMessage.MsgID,
"message": syslogMessage.Message,
}
return s.toSafeMap(value)
}
// parseRFC5424 will parse an RFC5424 syslog message.
func (s *Parser) parseRFC5424(syslogMessage *rfc5424.SyslogMessage) (map[string]any, error) {
value := map[string]any{
"timestamp": syslogMessage.Timestamp,
"priority": syslogMessage.Priority,
"facility": syslogMessage.Facility,
"severity": syslogMessage.Severity,
"hostname": syslogMessage.Hostname,
"appname": syslogMessage.Appname,
"proc_id": syslogMessage.ProcID,
"msg_id": syslogMessage.MsgID,
"message": syslogMessage.Message,
"structured_data": syslogMessage.StructuredData,
"version": syslogMessage.Version,
}
return s.toSafeMap(value)
}
// toSafeMap will dereference any pointers on the supplied map.
func (s *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 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]
_, ok = entry.NewAttributeField("timestamp").Delete(e)
if !ok {
return fmt.Errorf("failed to cleanup timestamp")
}
return nil
}
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
}
}