Skip to content

Commit da4761f

Browse files
log checksums and proctree for syslog formats
- Log the checksums and process tree when using rfc3164 and rfc5424 formats. - Minor performance improvements.
1 parent ac45c41 commit da4761f

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

daemon/log/formats/formats.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
package formats
22

3+
import (
4+
"log/syslog"
5+
"os"
6+
"strconv"
7+
)
8+
39
// LoggerFormat is the common interface that every format must meet.
410
// Transform expects an arbitrary number of arguments and types, and
511
// it must transform them to a string.
612
// Arguments can be of type Connection, string, int, etc.
713
type LoggerFormat interface {
814
Transform(...interface{}) string
915
}
16+
17+
var (
18+
ourPid = ""
19+
syslogLevel = ""
20+
)
21+
22+
func init() {
23+
ourPid = strconv.FormatUint(uint64(os.Getpid()), 10)
24+
syslogLevel = strconv.FormatUint(uint64(syslog.LOG_NOTICE|syslog.LOG_DAEMON), 10)
25+
}

daemon/log/formats/rfc3164.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package formats
22

33
import (
44
"fmt"
5-
"log/syslog"
6-
"os"
75
"strconv"
86
"strings"
97
"time"
@@ -40,7 +38,16 @@ func (r *Rfc3164) Transform(args ...interface{}) (out string) {
4038
for n, val := range values {
4139
switch val.(type) {
4240
case *protocol.Connection:
41+
checksums := ""
42+
tree := ""
4343
con := val.(*protocol.Connection)
44+
45+
for k, v := range con.ProcessChecksums {
46+
checksums = core.ConcatStrings(checksums, k, ":", v)
47+
}
48+
for _, y := range con.ProcessTree {
49+
tree = core.ConcatStrings(tree, y.Key, ",")
50+
}
4451
out = core.ConcatStrings(out,
4552
" SRC=\"", con.SrcIp, "\"",
4653
" SPT=\"", strconv.FormatUint(uint64(con.SrcPort), 10), "\"",
@@ -54,17 +61,19 @@ func (r *Rfc3164) Transform(args ...interface{}) (out string) {
5461
" PATH=\"", con.ProcessPath, "\"",
5562
" CMDLINE=\"", strings.Join(con.ProcessArgs, " "), "\"",
5663
" CWD=\"", con.ProcessCwd, "\"",
64+
" CHECKSUMS=\"", checksums, "\"",
65+
" PROCTREE=\"", tree, "\"",
5766
)
5867
default:
5968
out = fmt.Sprint(out, " ARG", n, "=\"", val, "\"")
6069
}
6170
}
62-
out = fmt.Sprintf("<%d>%s %s %s[%d]: [%s]\n",
63-
syslog.LOG_NOTICE|syslog.LOG_DAEMON,
71+
out = fmt.Sprintf("<%s>%s %s %s[%s]: [%s]\n",
72+
syslogLevel,
6473
time.Now().Format(time.RFC3339),
6574
hostname,
6675
tag,
67-
os.Getpid(),
76+
ourPid,
6877
out[1:])
6978

7079
return

daemon/log/formats/rfc5424.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package formats
22

33
import (
44
"fmt"
5-
"log/syslog"
6-
"os"
75
"strconv"
86
"strings"
97
"time"
@@ -30,6 +28,7 @@ func NewRfc5424() *Rfc5424 {
3028
func (r *Rfc5424) Transform(args ...interface{}) (out string) {
3129
hostname := ""
3230
tag := ""
31+
event := "GENERIC"
3332
arg1 := args[0]
3433
if len(args) > 1 {
3534
arg2 := args[1]
@@ -41,7 +40,19 @@ func (r *Rfc5424) Transform(args ...interface{}) (out string) {
4140
for n, val := range values {
4241
switch val.(type) {
4342
case *protocol.Connection:
43+
tree := ""
44+
checksums := ""
4445
con := val.(*protocol.Connection)
46+
event = "CONNECTION"
47+
48+
for k, v := range con.ProcessChecksums {
49+
checksums = core.ConcatStrings(checksums, k, ":", v)
50+
}
51+
for _, y := range con.ProcessTree {
52+
tree = core.ConcatStrings(tree, y.Key, ",")
53+
}
54+
55+
// TODO: allow to configure this via configuration file.
4556
out = core.ConcatStrings(out,
4657
" SRC=\"", con.SrcIp, "\"",
4758
" SPT=\"", strconv.FormatUint(uint64(con.SrcPort), 10), "\"",
@@ -55,17 +66,20 @@ func (r *Rfc5424) Transform(args ...interface{}) (out string) {
5566
" PATH=\"", con.ProcessPath, "\"",
5667
" CMDLINE=\"", strings.Join(con.ProcessArgs, " "), "\"",
5768
" CWD=\"", con.ProcessCwd, "\"",
69+
" CHECKSUMS=\"", checksums, "\"",
70+
" PROCTREE=\"", tree, "\"",
5871
)
5972
default:
6073
out = fmt.Sprint(out, " ARG", n, "=\"", val, "\"")
6174
}
6275
}
63-
out = fmt.Sprintf("<%d>1 %s %s %s %d TCPOUT - [%s]\n",
64-
syslog.LOG_NOTICE|syslog.LOG_DAEMON,
76+
out = fmt.Sprintf("<%s>1 %s %s %s %s %s - [%s]\n",
77+
syslogLevel,
6578
time.Now().Format(time.RFC3339),
6679
hostname,
6780
tag,
68-
os.Getpid(),
81+
ourPid,
82+
event,
6983
out[1:])
7084

7185
return

0 commit comments

Comments
 (0)