-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsyslog.go
254 lines (199 loc) · 5.06 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
package main
import (
"errors"
"fmt"
"net"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/Jeffail/gabs"
)
type StringWriter interface {
WriteString(string) (int, error)
}
type Syslog struct {
Port int
Messages chan Message
close chan bool
Statsd StatisticsSender
}
func (s *Syslog) Init(port int) error {
s.Port = port
s.close = make(chan bool)
ServerAddr, err := net.ResolveUDPAddr("udp", ":"+strconv.Itoa(port))
if err != nil {
return err
}
ServerConn, err := net.ListenUDP("udp", ServerAddr)
if err != nil {
return err
}
buf := make([]byte, 9000)
go func() {
for {
select {
case val, ok := <-s.close:
if val == true || ok == false {
return
}
break
default:
ServerConn.SetDeadline(time.Now().Add(time.Millisecond * 100))
n, _, err := ServerConn.ReadFromUDP(buf)
if s.Messages != nil && err == nil {
msg, err := ParseSyslogMessage(buf[0:n])
if err == nil {
s.Messages <- msg
} else {
if s.Statsd != nil {
s.Statsd.Inc("logs2kafka.invalid_messages", 1, 0.1)
}
fmt.Fprintf(os.Stderr, "Error parsing json message: %s\n", err)
}
}
}
}
}()
return nil
}
func (s *Syslog) Close() {
s.close <- true
}
type Priority struct {
Priority int
Facility int
Severity int
}
func IsDigit(c byte) bool {
return c >= '0' && c <= '9'
}
func FindNextSpace(buff []byte, from int, l int) (int, error) {
var to int
for to = from; to < l; to++ {
if buff[to] == ' ' {
to++
return to, nil
}
}
return 0, errors.New("No space found")
}
func ExtractPriority(buffer []byte, cursor *int, l int) (Priority, error) {
priority := Priority{}
// Skip numbers and spaces before priority
for (buffer[*cursor] >= '0' && buffer[*cursor] <= '9') || buffer[*cursor] == ' ' {
*cursor = *cursor + 1
}
if buffer[*cursor] != '<' {
return priority, errors.New("No priority start character")
}
i := 1 // Start after '<'
priDigit := 0
for i < l {
if i >= 5 {
return priority, errors.New("No priority end character or priority too long")
}
c := buffer[*cursor+i]
if c == '>' {
if i == 1 {
return priority, errors.New("Priority too short")
}
*cursor = i + 1
priority.Priority = priDigit
priority.Facility = priDigit / 8
priority.Severity = priDigit % 8
return priority, nil
}
if IsDigit(c) {
v, e := strconv.Atoi(string(c))
if e != nil {
return priority, e
}
priDigit = (priDigit * 10) + v
} else {
//fmt.Printf("Priority: %s\n", string(c))
return priority, errors.New("Priority was not valid digit")
}
i++
}
return priority, errors.New("No end found")
}
func ParseSyslogMessage(buffer []byte) (Message, error) {
m := Message{}
var err error
var parts []string
var tags []string
var payload string
cursor := 0
l := len(buffer)
_, err = ExtractPriority(buffer, &cursor, l)
if err != nil {
return m, err
}
stringbuffer := strings.TrimSpace(string(buffer[cursor:]))
parts = strings.SplitN(stringbuffer, " ", 7)
//for i := 0; i < len(parts); i++ {
// fmt.Printf("parts[%d]: %s\n", i, parts[i])
//}
// Find out if date format is ISO8601
if len(parts) > 0 && len(parts[0]) > 18 && parts[0][10] == 'T' {
tags = strings.SplitN(parts[2], "/", 4)
//fmt.Printf("ISO8601 tags: %+v, len: %d\n", tags, len(tags))
if len(tags) != 4 {
return m, errors.New("Malformed input on phase 2, assuming ISO8601 date format")
}
payload = strings.SplitN(stringbuffer, " ", 4)[3]
} else {
if len(parts) < 6 {
return m, errors.New("Malformed input on phase 1, assuming legacy date format")
}
// Add docker/ to tag string if it's missing (as is the case with k8s, for example)
var match, _ = regexp.MatchString("^docker", parts[5])
if !match {
parts[5] = "docker/" + parts[5]
}
tags = strings.SplitN(parts[5], "/", 4)
//fmt.Printf("tags: %+v, len: %d\n", tags, len(tags))
if len(tags) != 4 {
return m, errors.New("Malformed input on phase 2, assuming legacy date format")
}
payload = parts[6]
}
//fmt.Printf("Payload after detection: %+v\n", payload)
// Simple JSON detection
if payload[0] == '{' {
m = JSONToMessage(payload)
err := m.ParseJSON()
if err != nil {
m.Container = gabs.New()
m.Container.Set(payload, "msg")
}
} else if len(payload) > 25 && payload[10] == 'T' && payload[24] == '{' { // Detect payload which has ISO8601 timestamp in the beginning
m = JSONToMessage(payload[24:])
err = m.ParseJSON()
if err != nil {
m.Container = gabs.New()
m.Container.Set(payload, "msg")
}
} else {
m.Container = gabs.New()
m.Container.Set(payload, "msg")
}
// if the container_name includes periods (as with k8s), use only the first part
var nameParts = strings.Split(tags[1], ".")
if len(nameParts) > 1 {
m.Container.Set(nameParts[0], "container_name")
m.Container.Set(tags[1], "pod_name")
} else {
m.Container.Set(tags[1], "container_name")
}
m.Container.Set(tags[2], "container_id")
n := strings.Index(tags[3], "[")
if n != -1 {
m.Container.Set(tags[3][0:n], "docker_image")
} else {
m.Container.Set(tags[3], "docker_image")
}
return m, nil
}