/
journald-syslog.go
219 lines (194 loc) · 5.57 KB
/
journald-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
// Copyright 2015 Ed Marshall. All rights reserved.
// Use of this source code is governed by a GPL-style
// license that can be found in the COPYING file.
package main
import (
"log"
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/coreos/go-systemd/activation"
"github.com/coreos/go-systemd/journal"
"github.com/jonboulle/clockwork"
)
// RFC5424: MUST receive 408-octet messages, SHOULD accept 2048-octet messages
const PACKETSIZE = 2048
// SyslogMessage represents a completely-parsed syslog packet.
type SyslogMessage struct {
Version int
Facility int
Severity int
Timestamp time.Time
Hostname string
Tag string
StructuredData string
Message string
Source string
clock clockwork.Clock
}
func NewSyslogMessage() *SyslogMessage {
// We're technically a relay, so per RFC3164, we're expected to fill
// in a few defaults before passing the message along.
clock := clockwork.NewRealClock()
return &SyslogMessage{
Version: 0,
Facility: 0,
Severity: 5,
Timestamp: clock.Now(),
clock: clock,
}
}
// ParseSyslog takes a syslog packet and source address as strings, and
// parses them into a SyslogMessage.
func (msg *SyslogMessage) Parse(buf string, source string) {
msg.Source = source
rest := buf[:]
// PRI
if rest[0] == '<' {
if priEnd := strings.IndexRune(rest, '>'); priEnd > 1 && priEnd < 5 {
if pri, err := strconv.Atoi(rest[1:priEnd]); err == nil {
msg.Facility = pri >> 3
msg.Severity = pri & 7
rest = rest[priEnd+1:]
// VERSION
if rest[0] == '1' {
msg.Version = 1
rest = rest[2:]
// TIMESTAMP
if tsEnd := strings.IndexRune(rest, ' '); tsEnd >= 0 {
// Try a couple of RFC3339-compatible parsings.
ts, err := time.ParseInLocation(time.RFC3339Nano, rest[:tsEnd], time.UTC)
if err != nil {
ts, err = time.ParseInLocation(time.RFC3339, rest[:tsEnd], time.UTC)
}
if err == nil {
msg.Timestamp = ts
rest = rest[tsEnd+1:]
// HOSTNAME, APP-NAME/PROCID/MSGID (TAG)
if parts := strings.SplitN(rest, " ", 5); len(parts) == 5 {
msg.Hostname = parts[0]
msg.Tag = strings.Join(parts[1:4], " ")
rest = parts[4]
}
// TODO: This is lame. Do proper structured data parsing
// Make SyslogMessage.StructuredData a map[string]map[string]string,
// populated as {SD-ID:{PARAM-NAME:PARAM-VALUE,...},...}.
if rest[0] == '[' {
if sdEnd := strings.IndexRune(rest, ']'); sdEnd > 1 {
msg.StructuredData = rest[:sdEnd]
rest = rest[sdEnd+2:]
}
}
}
}
} else {
// TIMESTAMP
if ts, err := time.Parse(time.Stamp, rest[:15]); err == nil {
msg.Timestamp = ts
rest = rest[16:]
// HOSTNAME, TAG
if parts := strings.SplitN(rest, " ", 3); len(parts) == 3 {
msg.Hostname = parts[0]
msg.Tag = parts[1]
rest = parts[2]
}
}
}
}
}
}
msg.Message = rest
}
// IngestMessage takes a syslog packet and source address as strings, and
// logs a parsed version of them to journald.
func IngestMessage(buf string, source string) {
msg := NewSyslogMessage()
msg.Parse(buf, source)
vars := map[string]string{
"SYSLOG_VERSION": strconv.Itoa(msg.Version),
"SYSLOG_FACILITY": strconv.Itoa(msg.Facility),
"SYSLOG_SEVERITY": strconv.Itoa(msg.Severity),
"SYSLOG_TIMESTAMP": msg.Timestamp.String(),
// Without the hostname, the tag isn't a complete identifier.
"SYSLOG_IDENTIFIER": strings.Join([]string{
msg.Hostname, msg.Tag}, " "),
}
if len(msg.Hostname) > 0 {
vars["SYSLOG_HOSTNAME"] = msg.Hostname
}
if len(msg.Source) > 0 {
vars["SYSLOG_SOURCE"] = msg.Source
}
// TODO: When structured data is actually stored in a structured form,
// populate entries as SYSLOG_SD_<SD_ID>=<SD-PARAM ...>.
if len(msg.StructuredData) > 0 {
vars["SYSLOG_STRUCTURED_DATA"] = msg.StructuredData
}
err := journal.Send(msg.Message, journal.Priority(msg.Severity), vars)
if err != nil {
log.Println(err)
}
}
// HandleListener takes a TCPListener socket (passed in from systemd) and
// repeatedly accepts new connections from it, handing the packets off for
// processing to IngestMessage.
func HandleListener(fd *net.TCPListener) {
for {
conn, err := fd.Accept()
if err != nil {
log.Println(err)
continue
}
go func(conn net.Conn) {
defer conn.Close()
buf := make([]byte, PACKETSIZE)
if count, err := conn.Read(buf); err != nil {
log.Println(err)
} else {
addr := conn.RemoteAddr()
IngestMessage(string(buf[:count]), addr.String())
}
}(conn)
}
}
// HandlePacket takes a UDPConn socket (passed in from systemd) and repeatedly
// reads new packets from it, handing them off for processing to IngestMessage.
func HandlePacket(fd *net.UDPConn) {
for {
buf := make([]byte, PACKETSIZE)
if count, addr, err := fd.ReadFromUDP(buf); err != nil {
log.Println(err)
} else {
go IngestMessage(string(buf[:count]), addr.String())
}
}
}
func main() {
packetConns, _ := activation.PacketConns(false)
listeners, _ := activation.Listeners(false)
if len(packetConns) == 0 && len(listeners) == 0 {
log.Fatal("no UDP or TCP sockets supplied by systemd")
}
var wg sync.WaitGroup
for _, fd := range packetConns {
if conn, ok := fd.(*net.UDPConn); ok {
wg.Add(1)
go func(conn *net.UDPConn) {
defer wg.Done()
HandlePacket(conn)
}(conn)
}
}
for _, fd := range listeners {
if conn, ok := fd.(*net.TCPListener); ok {
wg.Add(1)
go func(conn *net.TCPListener) {
defer wg.Done()
HandleListener(conn)
}(conn)
}
}
wg.Wait()
}