Skip to content

Commit 42e2f02

Browse files
boyskamcuadros
authored andcommitted
Fix GNU syslog (#63)
* some tests for rfc3164: GNU is failing! * rfc3164 is now compatible with GNU syslog() * add a test case for PID in "normal" 3164 format
1 parent a127d82 commit 42e2f02

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

format/rfc3164_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,57 @@ func (s *FormatSuite) TestRFC3164_SingleSplit(c *C) {
88
f := RFC3164{}
99
c.Assert(f.GetSplitFunc(), IsNil)
1010
}
11+
12+
func (s *FormatSuite) TestRFC3164_CorrectParsingTypical(c *C) {
13+
f := RFC3164{}
14+
15+
find := `<13>May 1 20:51:40 myhostname myprogram: ciao`
16+
parser := f.GetParser([]byte(find))
17+
err := parser.Parse()
18+
c.Assert(err, IsNil)
19+
c.Assert(parser.Dump()["content"], Equals, "ciao")
20+
c.Assert(parser.Dump()["hostname"], Equals, "myhostname")
21+
c.Assert(parser.Dump()["tag"], Equals, "myprogram")
22+
23+
}
24+
func (s *FormatSuite) TestRFC3164_CorrectParsingTypicalWithPID(c *C) {
25+
f := RFC3164{}
26+
27+
find := `<13>May 1 20:51:40 myhostname myprogram[42]: ciao`
28+
parser := f.GetParser([]byte(find))
29+
err := parser.Parse()
30+
c.Assert(err, IsNil)
31+
c.Assert(parser.Dump()["content"], Equals, "ciao")
32+
c.Assert(parser.Dump()["hostname"], Equals, "myhostname")
33+
c.Assert(parser.Dump()["tag"], Equals, "myprogram")
34+
35+
}
36+
37+
func (s *FormatSuite) TestRFC3164_CorrectParsingGNU(c *C) {
38+
// GNU implementation of syslog() has a variant: hostname is missing
39+
f := RFC3164{}
40+
41+
find := `<13>May 1 20:51:40 myprogram: ciao`
42+
parser := f.GetParser([]byte(find))
43+
err := parser.Parse()
44+
c.Assert(err, IsNil)
45+
c.Assert(parser.Dump()["content"], Equals, "ciao")
46+
// c.Assert(parser.Dump()["hostname"], Equals, "myhostname")
47+
c.Assert(parser.Dump()["tag"], Equals, "myprogram")
48+
49+
}
50+
51+
func (s *FormatSuite) TestRFC3164_CorrectParsingJournald(c *C) {
52+
// GNU implementation of syslog() has a variant: hostname is missing
53+
// systemd uses it, and typically also passes PID
54+
f := RFC3164{}
55+
56+
find := `<78>May 1 20:51:02 myprog[153]: blah`
57+
parser := f.GetParser([]byte(find))
58+
err := parser.Parse()
59+
c.Assert(err, IsNil)
60+
c.Assert(parser.Dump()["content"], Equals, "blah")
61+
// c.Assert(parser.Dump()["hostname"], Equals, "myhostname")
62+
c.Assert(parser.Dump()["tag"], Equals, "myprog")
63+
64+
}

internal/syslogparser/rfc3164/rfc3164.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rfc3164
22

33
import (
44
"bytes"
5+
"os"
56
"time"
67

78
"gopkg.in/mcuadros/go-syslog.v2/internal/syslogparser"
@@ -194,7 +195,17 @@ func (p *Parser) parseTimestamp() (time.Time, error) {
194195
}
195196

196197
func (p *Parser) parseHostname() (string, error) {
197-
return syslogparser.ParseHostname(p.buff, &p.cursor, p.l)
198+
oldcursor := p.cursor
199+
hostname, err := syslogparser.ParseHostname(p.buff, &p.cursor, p.l)
200+
if err == nil && len(hostname) > 0 && string(hostname[len(hostname)-1]) == ":" { // not an hostname! we found a GNU implementation of syslog()
201+
p.cursor = oldcursor - 1
202+
myhostname, err := os.Hostname()
203+
if err == nil {
204+
return myhostname, nil
205+
}
206+
return "", nil
207+
}
208+
return hostname, err
198209
}
199210

200211
// http://tools.ietf.org/html/rfc3164#section-4.1.3

0 commit comments

Comments
 (0)