File: builder.go.rl

package info (click to toggle)
golang-github-influxdata-go-syslog 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 964 kB
  • sloc: makefile: 84
file content (312 lines) | stat: -rw-r--r-- 7,230 bytes parent folder | download
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
package rfc5424

import (
    "time"
    "sort"
    "fmt"

    "github.com/influxdata/go-syslog/v2/common"
)

%%{
machine builder;

include common "common.rl";

action mark {
    pb = p
}

action set_timestamp {
	if t, e := time.Parse(RFC3339MICRO, string(data[pb:p])); e == nil {
        sm.timestamp = &t
    }
}

action set_hostname {
    if s := string(data[pb:p]); s != "-" {
        sm.hostname = &s
    }
}

action set_appname {
    if s := string(data[pb:p]); s != "-" {
        sm.appname = &s
    }
}

action set_procid {
    if s := string(data[pb:p]); s != "-" {
        sm.procID = &s
    }
}

action set_msgid {
    if s := string(data[pb:p]); s != "-" {
        sm.msgID = &s
    }
}

action set_sdid {
    if sm.structuredData == nil {
        sm.structuredData = &(map[string]map[string]string{})
    }

    id := string(data[pb:p])
    elements := *sm.structuredData
    if _, ok := elements[id]; !ok {
        elements[id] = map[string]string{}
    }
}

action set_sdpn {
    // Assuming SD map already exists, contains currentid key (set from outside)
    elements := *sm.structuredData
    elements[currentid][string(data[pb:p])] = ""
}

action markslash {
    backslashes = append(backslashes, p)
}

action set_sdpv {
    // Store text
	text := data[pb:p]
	// Strip backslashes only when there are ...
    if len(backslashes) > 0 {
        text = common.RemoveBytes(text, backslashes, pb)
    }
	// Assuming SD map already exists, contains currentid key and currentparamname key (set from outside)
    elements := *sm.structuredData
    elements[currentid][currentparamname] = string(text)
}

action set_msg {
    if s := string(data[pb:p]); s != "" {
        sm.message = &s
    }
}

timestamp := (fulldate 'T' fulltime) >mark %set_timestamp;

hostname := hostnamerange >mark %set_hostname;

appname := appnamerange >mark %set_appname;

procid := procidrange >mark %set_procid;

msgid := msgidrange >mark %set_msgid;

sdid := sdname >mark %set_sdid;

sdpn := sdname >mark %set_sdpn;

escapes = (bs >markslash toescape);

sdpv := (utf8charwodelims* escapes*)+ >mark %set_sdpv;

msg := (bom? utf8octets) >mark %set_msg;

write data noerror nofinal;
}%%

type entrypoint int

const (
	timestamp entrypoint = iota
	hostname
	appname
	procid
	msgid
    sdid
    sdpn
    sdpv
    msg
)

func (e entrypoint) translate() int {
    switch e {
    case timestamp:
        return builder_en_timestamp
    case hostname:
        return builder_en_hostname
    case appname:
        return builder_en_appname
    case procid:
        return builder_en_procid
    case msgid:
        return builder_en_msgid
    case sdid:
        return builder_en_sdid
    case sdpn:
        return builder_en_sdpn
    case sdpv:
        return builder_en_sdpv
    case msg:
        return builder_en_msg
    default:
        return builder_start
    }
}

var currentid string
var currentparamname string

func (sm *SyslogMessage) set(from entrypoint, value string) *SyslogMessage {
    data := []byte(value)
	p := 0
	pb := 0
	pe := len(data)
	eof := len(data)
    cs := from.translate()
    backslashes := []int{}
    %% write exec;

    return sm
}

// SetPriority set the priority value and the computed facility and severity codes accordingly.
//
// It ignores incorrect priority values (range [0, 191]).
func (sm *SyslogMessage) SetPriority(value uint8) *SyslogMessage {
	if value >= 0 && value <= 191 {
		sm.setPriority(value)
	}

	return sm
}

// SetVersion set the version value.
//
// It ignores incorrect version values (range ]0, 999]).
func (sm *SyslogMessage) SetVersion(value uint16) *SyslogMessage {
	if value > 0 && value <= 999 {
		sm.version = value
	}

	return sm
}

// SetTimestamp set the timestamp value.
func (sm *SyslogMessage) SetTimestamp(value string) *SyslogMessage {
    return sm.set(timestamp, value)
}

// SetHostname set the hostname value.
func (sm *SyslogMessage) SetHostname(value string) *SyslogMessage {
    return sm.set(hostname, value)
}

// SetAppname set the appname value.
func (sm *SyslogMessage) SetAppname(value string) *SyslogMessage {
    return sm.set(appname, value)
}

// SetProcID set the procid value.
func (sm *SyslogMessage) SetProcID(value string) *SyslogMessage {
    return sm.set(procid, value)
}

// SetMsgID set the msgid value.
func (sm *SyslogMessage) SetMsgID(value string) *SyslogMessage {
    return sm.set(msgid, value)
}

// SetElementID set a structured data id.
//
// When the provided id already exists the operation is discarded.
func (sm *SyslogMessage) SetElementID(value string) *SyslogMessage {
    return sm.set(sdid, value)
}

// SetParameter set a structured data parameter belonging to the given element.
//
// If the element does not exist it creates one with the given element id.
// When a parameter with the given name already exists for the given element the operation is discarded.
func (sm *SyslogMessage) SetParameter(id string, name string, value string) *SyslogMessage {
    // Create an element with the given id (or re-use the existing one)
    sm.set(sdid, id)

    // We can create parameter iff the given element id exists
    if sm.structuredData != nil {
        elements := *sm.structuredData
        if _, ok := elements[id]; ok {
            currentid = id
            sm.set(sdpn, name)
            // We can assign parameter value iff the given parameter key exists
            if _, ok := elements[id][name]; ok {
                currentparamname = name
                sm.set(sdpv, value)
            }
        }
    }

    return sm
}

// SetMessage set the message value.
func (sm *SyslogMessage) SetMessage(value string) *SyslogMessage {
	return sm.set(msg, value)
}

func (sm *SyslogMessage) String() (string, error) {
    if !sm.Valid() {
        return "", fmt.Errorf("invalid syslog")
    }

    template := "<%d>%d %s %s %s %s %s %s%s"

    t := "-"
    hn := "-"
    an := "-"
    pid := "-"
    mid := "-"
    sd := "-"
    m := ""
    if sm.timestamp != nil {
        t = sm.timestamp.Format("2006-01-02T15:04:05.999999Z07:00") // verify 07:00
    }
    if sm.hostname != nil {
        hn = *sm.hostname
    }
    if sm.appname != nil {
        an = *sm.appname
    }
    if sm.procID != nil {
        pid = *sm.procID
    }
    if sm.msgID != nil {
        mid = *sm.msgID
    }
    if sm.structuredData != nil {
        // Sort element identifiers
        identifiers := make([]string, 0)
        for k, _ := range *sm.structuredData {
            identifiers = append(identifiers, k)
        }
        sort.Strings(identifiers)

        sd = ""
        for _, id := range identifiers {
            sd += fmt.Sprintf("[%s", id)

            // Sort parameter names
            params := (*sm.structuredData)[id]
            names := make([]string, 0)
            for n, _ := range params {
                names = append(names, n)
            }
            sort.Strings(names)

            for _, name := range names {
                sd += fmt.Sprintf(" %s=\"%s\"", name, common.EscapeBytes(params[name]))
            }
            sd += "]"
        }
    }
    if sm.message != nil {
        m = " " + *sm.message
    }

    return fmt.Sprintf(template, *sm.priority, sm.version, t, hn, an, pid, mid, sd, m), nil
}