/
syslog.go
142 lines (114 loc) · 3.45 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
package main
import (
"context"
"fmt"
"net"
"os"
"strings"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"github.com/canonical/lxd/lxd/events"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/revert"
)
// StartSyslogListener starts the log monitor.
func StartSyslogListener(ctx context.Context, eventServer *events.Server) error {
var listenConfig net.ListenConfig
sockFile := shared.VarPath("syslog.socket")
if shared.PathExists(sockFile) {
err := os.Remove(sockFile)
if err != nil {
return fmt.Errorf("Failed deleting stale syslog.socket: %w", err)
}
}
conn, err := listenConfig.ListenPacket(ctx, "unixgram", sockFile)
if err != nil {
return fmt.Errorf("Failed listening on syslog socket: %w", err)
}
revert := revert.New()
defer revert.Fail()
revert.Add(func() {
_ = conn.Close()
_ = os.Remove(sockFile)
})
// Get max size
var maxBufSize int
uc, ok := conn.(*net.UnixConn)
if ok {
f, err := uc.File()
if err != nil {
return fmt.Errorf("Failed getting underlying os.File: %w", err)
}
maxBufSize, err = unix.GetsockoptInt(int(f.Fd()), unix.SOL_SOCKET, unix.SO_RCVBUF)
if err != nil {
_ = f.Close()
return fmt.Errorf("Failed getting SO_RCVBUF: %w", err)
}
// This makes the fd non-blocking so that conn.Close() won't block.
// See https://github.com/golang/go/issues/29277#issuecomment-447922481
err = unix.SetNonblock(int(f.Fd()), true)
if err != nil {
_ = f.Close()
return fmt.Errorf("Failed setting non-block: %w", err)
}
_ = f.Close()
}
// This goroutine waits for the context to be cancelled and then closes the connection causing `ReadFrom` to return an error and exit the goroutine below.
go func() {
<-ctx.Done()
_ = conn.Close()
_ = os.Remove(sockFile)
}()
// This goroutine is used for reading packets, and processing the log message. `ReadFrom` will block until it either receives data, or an error occurs. If the connection is closed, `ReadFrom` will return an error, and the goroutine will terminate.
go func() {
buf := make([]byte, maxBufSize)
// This maps OVN log level names to logrus log levels.
logMap := map[string]logrus.Level{
"dbg": logrus.DebugLevel,
"info": logrus.InfoLevel,
"warn": logrus.WarnLevel,
"err": logrus.ErrorLevel,
"emer": logrus.ErrorLevel,
}
for {
n, _, err := conn.ReadFrom(buf)
if err != nil {
return
}
// Acceptable formats:
// - <29> ovs|00017|rconn|INFO|unix:/var/run/openvswitch/br-int.mgmt: connected"
// - <29> ovs|ovn-controller|00017|rconn|INFO|unix:/var/run/openvswitch/br-int.mgmt: connected"
// The first field can be ignored as that information is relevant to syslogd.
fields := strings.SplitN(string(buf[:n]), "|", 6)
if len(fields) < 5 {
continue
}
applicationName := ""
if len(fields) == 6 {
applicationName = fields[1]
}
sequenceNumber := fields[len(fields)-4]
moduleName := fields[len(fields)-3]
logLevel := strings.ToLower(fields[len(fields)-2])
message := fields[len(fields)-1]
event := api.EventLogging{
Level: logMap[logLevel].String(),
Message: message,
Context: map[string]string{
"module": moduleName,
"sequence": sequenceNumber,
},
}
if applicationName != "" {
event.Context["application"] = applicationName
}
err = eventServer.Send("", api.EventTypeOVN, event)
if err != nil {
continue
}
}
}()
revert.Success()
return nil
}