-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslogmsgparts.go
238 lines (186 loc) · 4.46 KB
/
syslogmsgparts.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 syslogsidecar
import (
"fmt"
"strconv"
"time"
"github.com/g41797/go-syslog/format"
"github.com/g41797/sputnik"
)
const (
Formermessage = "data"
rfc3164 = "RFC3164"
rfc5424 = "RFC5424"
rfcFormatKey = "rfc"
rfc3164OnlyKey = "tag"
rfc5424OnlyKey = "structured_data"
severityKey = "severity"
badMessageParts = len(formerMessage)
rfc5424Parts = len(rfc5424parts)
rfc3164Parts = len(rfc3164parts)
)
type partType struct {
name string
kind string
}
// RFC3164 parameters with type
// https://documentation.solarwinds.com/en/success_center/kss/content/kss_adminguide_syslog_protocol.htm
var rfc3164parts = [...]partType{
{rfcFormatKey, "string"}, // Non-RFC: Added by syslogsidecar
{"priority", "int"},
{"facility", "int"},
{severityKey, "int"},
{"timestamp", "time.Time"},
{"hostname", "string"},
{rfc3164OnlyKey, "string"},
{"content", "string"},
}
// RFC5424 parameters with type
// https://hackmd.io/@njjack/syslogformat
var rfc5424parts = [...]partType{
{rfcFormatKey, "string"}, // Non-RFC: Added by syslogsidecar
{"priority", "int"},
{"facility", "int"},
{severityKey, "int"},
{"version", "int"},
{"timestamp", "time.Time"},
{"hostname", "string"},
{"app_name", "string"},
{"proc_id", "string"},
{"msg_id", "string"},
{rfc5424OnlyKey, "string"},
{"message", "string"},
}
// Former message - for badly formatted syslog message
var formerMessage = [...]partType{
{Formermessage, "string"},
}
type syslogmsgparts struct {
parts
}
func newsyslogmsgparts() *syslogmsgparts {
result := new(syslogmsgparts)
result.set(128)
return result
}
func (mp *syslogmsgparts) pack(logParts format.LogParts) error {
if mp == nil {
return fmt.Errorf("nil syslogmsgparts")
}
if logParts == nil {
return fmt.Errorf("nil logParts")
}
if len(logParts) == 0 {
return fmt.Errorf("empty logParts")
}
if _, exists := logParts[Formermessage]; exists {
mp.packParts(formerMessage[:], logParts)
return nil
}
if _, exists := logParts[rfc5424OnlyKey]; exists {
logParts[rfcFormatKey] = rfc5424
mp.packParts(rfc5424parts[:], logParts)
return nil
}
if _, exists := logParts[rfc3164OnlyKey]; exists {
logParts[rfcFormatKey] = rfc3164
mp.packParts(rfc3164parts[:], logParts)
return nil
}
mp.packParts(formerMessage[:], logParts)
return nil
}
func (mp *syslogmsgparts) packParts(parts []partType, logParts format.LogParts) {
mp.set(128)
count := len(parts)
mp.setRuneAt(0, rune(count))
mp.skip(count + 1)
for i, part := range parts {
v, exists := logParts[part.name]
if !exists {
mp.setRuneAt(i+1, 0)
continue
}
mp.setRuneAt(i+1, rune(mp.appendText(toString(v, part.kind))))
}
}
func (mp *syslogmsgparts) Unpack(put func(name, value string) error) error {
if mp == nil {
return fmt.Errorf("nil syslogmsgparts")
}
if len(mp.data) == 0 {
return fmt.Errorf("empty syslogmsgparts")
}
count, _ := mp.runeAt(0)
switch int(count) {
case badMessageParts:
return mp.unpackParts(formerMessage[:], put)
case rfc5424Parts:
return mp.unpackParts(rfc5424parts[:], put)
case rfc3164Parts:
return mp.unpackParts(rfc3164parts[:], put)
}
return fmt.Errorf("Wrong packed syslog message")
}
func (mp *syslogmsgparts) unpackParts(parts []partType, put func(name, value string) error) error {
mp.rewind()
count, _ := mp.runeAt(0)
mp.skip(int(count + 1))
for i, part := range parts {
vlen, _ := mp.runeAt(1 + i)
value, err := mp.part(int(vlen))
if err != nil {
return err
}
err = put(part.name, value)
if err != nil {
return err
}
}
return nil
}
func toString(val any, typ string) string {
result := ""
if val == nil {
return result
}
switch typ {
case "string":
result, _ = val.(string)
return result
case "int":
intval, _ := val.(int)
result = strconv.Itoa(intval)
return result
case "time.Time":
tval, _ := val.(time.Time)
result = tval.Format(time.RFC3339)
return result
}
return result
}
func toMsg(logParts format.LogParts) sputnik.Msg {
if logParts == nil {
return nil
}
if len(logParts) == 0 {
return nil
}
msg := Get()
slm, _ := msg[syslogmessage].(*syslogmsgparts)
perr := slm.pack(logParts)
if perr != nil {
return nil
}
return msg
}
func (mp *syslogmsgparts) priority() (string, error) {
mp.rewind()
count, _ := mp.runeAt(0)
if int(count) <= badMessageParts {
return Formermessage, fmt.Errorf("non rfc message")
}
rfclen, _ := mp.runeAt(1)
mp.skip(int(count + rfclen + 1))
prlen, _ := mp.runeAt(2)
return mp.part(int(prlen))
}