@@ -20,8 +20,9 @@ import (
20
20
)
21
21
22
22
const (
23
- TCP = `tcp`
24
- TLS = `tls`
23
+ TCP = `tcp`
24
+ TLS = `tls`
25
+ ParsedMsg = "parsed_msg"
25
26
)
26
27
27
28
type Syslog struct {
@@ -68,13 +69,12 @@ func (ser SyslogEncodingRemap) Name() string {
68
69
}
69
70
70
71
func (ser SyslogEncodingRemap ) Template () string {
71
- return `{{define "` + ser .Name () + `" -}}
72
+ return fmt . Sprintf ( `{{define "` + ser .Name ()+ `" -}}
72
73
[transforms.{{.ComponentID}}]
73
74
type = "remap"
74
75
inputs = {{.Inputs}}
75
76
source = '''
76
- . = merge(., parse_json!(string!(.message))) ?? .
77
-
77
+ #calculate defaults
78
78
{{if eq .RFC "RFC3164" -}}
79
79
if .log_type == "infrastructure" && .log_source == "node" {
80
80
._internal.syslog.tag = to_string!(.systemd.u.SYSLOG_IDENTIFIER || "")
@@ -118,7 +118,16 @@ if .log_type == "audit" {
118
118
}
119
119
{{end}}
120
120
121
+
121
122
{{if .EncodingFields.FieldVRLList -}}
123
+ _tmp, err = parse_json(string!(.message))
124
+ if err != null {
125
+ _tmp = .
126
+ log(err, level: "error")
127
+ } else {
128
+ _tmp = merge!(.,_tmp)
129
+ }
130
+ %s = _tmp
122
131
{{range $templatePair := .EncodingFields.FieldVRLList -}}
123
132
.{{$templatePair.Field}} = {{$templatePair.VRLString}}
124
133
{{end -}}
@@ -139,7 +148,7 @@ if is_null({{.PayloadKey}}) {
139
148
{{end}}
140
149
'''
141
150
{{end -}}
142
- `
151
+ ` , ParsedMsg )
143
152
}
144
153
145
154
type SyslogEncoding struct {
@@ -219,55 +228,30 @@ func getEncodingTemplatesAndFields(s obs.Syslog) EncodingTemplateField {
219
228
FieldVRLList : []FieldVRLStringPair {},
220
229
}
221
230
222
- if s .Facility == "" {
223
- s .Facility = `{._internal.syslog.facility || "user"}`
231
+ appendField := func (fieldName string , value * string , defaultVal string ) {
232
+ if * value == "" {
233
+ * value = defaultVal
234
+ templateFields .FieldVRLList = append (templateFields .FieldVRLList , FieldVRLStringPair {
235
+ Field : fieldName ,
236
+ VRLString : commontemplate .TransformUserTemplateToVRL (* value ),
237
+ })
238
+ } else {
239
+ templateFields .FieldVRLList = append (templateFields .FieldVRLList , FieldVRLStringPair {
240
+ Field : fieldName ,
241
+ VRLString : commontemplate .TransformUserTemplateToVRL (* value , ParsedMsg ),
242
+ })
243
+ }
224
244
}
225
- templateFields .FieldVRLList = append (templateFields .FieldVRLList , FieldVRLStringPair {
226
- Field : "facility" ,
227
- VRLString : commontemplate .TransformUserTemplateToVRL (s .Facility ),
228
- })
229
245
230
- if s .Severity == "" {
231
- s .Severity = `{._internal.syslog.severity || "informational"}`
232
- }
233
- templateFields .FieldVRLList = append (templateFields .FieldVRLList , FieldVRLStringPair {
234
- Field : "severity" ,
235
- VRLString : commontemplate .TransformUserTemplateToVRL (s .Severity ),
236
- })
237
-
238
- if s .ProcId == "" {
239
- s .ProcId = `{._internal.syslog.proc_id || "-"}`
240
- }
241
- templateFields .FieldVRLList = append (templateFields .FieldVRLList , FieldVRLStringPair {
242
- Field : "proc_id" ,
243
- VRLString : commontemplate .TransformUserTemplateToVRL (s .ProcId ),
244
- })
246
+ appendField ("facility" , & s .Facility , `{._internal.syslog.facility || "user"}` )
247
+ appendField ("severity" , & s .Severity , `{._internal.syslog.severity || "informational"}` )
248
+ appendField ("proc_id" , & s .ProcId , `{._internal.syslog.proc_id || "-"}` )
245
249
246
250
if s .RFC == obs .SyslogRFC3164 {
247
- if s .AppName == "" {
248
- s .AppName = `{._internal.syslog.tag || ""}`
249
- }
250
- templateFields .FieldVRLList = append (templateFields .FieldVRLList , FieldVRLStringPair {
251
- Field : "tag" ,
252
- VRLString : commontemplate .TransformUserTemplateToVRL (s .AppName ),
253
- })
254
-
251
+ appendField ("tag" , & s .AppName , `{._internal.syslog.tag || ""}` )
255
252
} else {
256
- if s .AppName == "" {
257
- s .AppName = `{._internal.syslog.app_name || "-"}`
258
- }
259
- templateFields .FieldVRLList = append (templateFields .FieldVRLList , FieldVRLStringPair {
260
- Field : "app_name" ,
261
- VRLString : commontemplate .TransformUserTemplateToVRL (s .AppName ),
262
- })
263
-
264
- if s .MsgId == "" {
265
- s .MsgId = `{._internal.syslog.msg_id || "-"}`
266
- }
267
- templateFields .FieldVRLList = append (templateFields .FieldVRLList , FieldVRLStringPair {
268
- Field : "msg_id" ,
269
- VRLString : commontemplate .TransformUserTemplateToVRL (s .MsgId ),
270
- })
253
+ appendField ("app_name" , & s .AppName , `{._internal.syslog.app_name || "-"}` )
254
+ appendField ("msg_id" , & s .MsgId , `{._internal.syslog.msg_id || "-"}` )
271
255
}
272
256
273
257
return templateFields
0 commit comments