This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
forked from logplex/pg_logplexcollector
/
logfebe.go
349 lines (295 loc) · 8.51 KB
/
logfebe.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main
import (
"bytes"
"io"
"log"
"net"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/logplex/logplexc"
"github.com/uhoh-itsmaciek/femebe/buf"
"github.com/uhoh-itsmaciek/femebe/core"
)
// A function that, when called, panics. The provider of the function
// is assumed to be able to recover from the panic, usually by using a
// sentinel value to ensure that only panics as a result of calling
// the exitFn is called.
//
// If an exitFn is part of the parameter list for a function, it is
// customary for it not to have an error return.
//
// This is useful when it's fairly clear that an error should be
// handled in one part of a program all the time, e.g. abort a
// goroutine after logging the cause of the exit.
type exitFn func(args ...interface{})
// Fills a message on behalf of the caller. Often the closure will
// close over a core.MessageStream to provide a source of data for the
// filled message.
type msgInit func(dst *core.Message, exit exitFn)
func logWorker(die dieCh, l net.Listener, cfg logplexc.Config, sr *serveRecord) {
// Make world-writable so anything can connect and send logs.
// This may be be worth locking down more, but as-is unless
// pg_logplexcollector and the Postgres server share the same
// running user common umasks will be useless.
fi, err := os.Stat(sr.P)
if err != nil {
log.Fatalf(
"exiting, cannot stat just created socket %q: %v",
sr.P, err)
}
err = os.Chmod(sr.P, fi.Mode().Perm()|0222)
if err != nil {
log.Fatalf(
"exiting, cannot make just created socket "+
"world-writable %q: %v",
sr.P, err)
}
for {
select {
case <-die:
log.Print("listener exits normally from die request")
return
default:
break
}
conn, err := l.Accept()
if err != nil {
log.Printf("accept error: %v", err)
}
if err != nil {
log.Fatalf("serve database suffers unrecoverable "+
"error: %v", err)
}
go func() {
stream := core.NewBackendStream(conn)
var exit exitFn
exit = func(args ...interface{}) {
if len(args) == 1 {
log.Printf("Disconnect client: %v", args[0])
} else if len(args) > 1 {
if s, ok := args[0].(string); ok {
log.Printf(s, args[1:]...)
} else {
// Not an intended use case, but do
// one's best to print something.
log.Printf("Got a malformed exit: %v", args)
}
}
panic(&exit)
}
// Recovers from panic and exits in an orderly manner if (and
// only if) exit() is called; otherwise propagate the panic
// normally.
defer func() {
conn.Close()
// &exit is used as a sentinel value.
if r := recover(); r != nil && r != &exit {
panic(r)
}
}()
var msgInit msgInit
msgInit = func(m *core.Message, exit exitFn) {
err = stream.Next(m)
if err == io.EOF {
exit("postgres client disconnects")
} else if err != nil {
exit("could not read next message: %v", err)
}
}
// Protocol start-up; packets that are only received once.
processVerMsg(msgInit, exit)
ident := processIdentMsg(msgInit, exit)
log.Printf("client connects with identifier %q", ident)
// Resolve the identifier to a serve
if sr.I != ident {
exit("got unexpected identifier for socket: "+
"path %s, expected %s, got %s", sr.P, sr.I, ident)
}
// Set up client with serve
client := func(cfg logplexc.Config, url *url.URL) *logplexc.Client {
cfg.Logplex = *url
client, err := logplexc.NewClient(&cfg)
if err != nil {
exit(err)
}
return client
}
primary := client(cfg, &sr.u)
var audit *logplexc.Client
if sr.audit != nil {
audit = client(cfg, sr.audit)
}
defer func() {
primary.Close()
if audit != nil {
audit.Close()
}
}()
processLogMsg(die, primary, audit, msgInit, sr, exit)
}()
}
}
// Process a log message, sending it to the client.
func processLogMsg(die dieCh, primary *logplexc.Client, audit *logplexc.Client,
msgInit msgInit, sr *serveRecord, exit exitFn) {
var m core.Message
for {
// Poll request to exit
select {
case <-die:
return
default:
break
}
msgInit(&m, exit)
// Refuse to handle any log message above an arbitrary
// size. Furthermore, exit the worker, closing the0
// connection, so that the client doesn't even bother
// to wait for this process to drain the oversized
// item and anything following it; these will be
// dropped. It's on the client to gracefully handle
// the error and re-connect after this happens.
if m.Size() > 1*MB {
exit("client %q sent oversized log record")
}
payload, err := m.Force()
if err != nil {
exit("could not retrieve payload of message: %v",
err)
}
var lr logRecord
parseLogRecord(&lr, payload, exit)
routeLogRecord(&lr, primary, audit, sr, exit)
}
}
// Process a single logRecord value, buffering it in the logplex
// client.
func routeLogRecord(lr *logRecord, primary *logplexc.Client,
audit *logplexc.Client, sr *serveRecord, exit exitFn) {
var targets []*logplexc.Client
hasAudit := false
// Find error messages that look like connection auditing
// strings and report them to the auditing target *only*.
if audit != nil && lr.ErrMessage != nil {
switch {
case strings.HasPrefix(*lr.ErrMessage, "connection received: "):
fallthrough
case strings.HasPrefix(*lr.ErrMessage, "connection authorized: "):
fallthrough
case strings.HasPrefix(*lr.ErrMessage, "replication connection authorized: "):
targets = []*logplexc.Client{audit}
hasAudit = true
default:
targets = []*logplexc.Client{primary}
}
} else {
targets = []*logplexc.Client{primary}
}
// For interesting SQLState errors, *also* send them to the
// audit endpoint.
if audit != nil && lr.SQLState != nil {
switch {
case strings.HasPrefix(*lr.SQLState, "58"):
fallthrough
case strings.HasPrefix(*lr.SQLState, "F0"):
fallthrough
case strings.HasPrefix(*lr.SQLState, "XX"):
if !hasAudit {
targets = append(targets, audit)
hasAudit = true
}
}
}
for _, tgt := range targets {
emitLogRecord(lr, sr, tgt, tgt == audit, exit)
}
}
func emitLogRecord(lr *logRecord, sr *serveRecord, target *logplexc.Client,
isAudit bool, exit exitFn) {
// Buffer to format the complete log message in.
msgFmtBuf := bytes.Buffer{}
// Helps with formatting a series of nullable strings.
catOptionalField := func(prefix string, maybePresent *string) {
if maybePresent != nil {
if prefix != "" {
msgFmtBuf.WriteString(prefix)
msgFmtBuf.WriteString(": ")
}
msgFmtBuf.WriteString(*maybePresent)
msgFmtBuf.WriteByte('\n')
}
}
if sr.Prefix != "" {
msgFmtBuf.WriteString(sr.Prefix)
msgFmtBuf.WriteString(" ")
}
if isAudit {
// The audit endpoint may be multiplexed, so add the
// identity to help tell log records apart.
msgFmtBuf.WriteString("instance_type=shogun identity=" +
sr.I + " ")
}
catOptionalField("", lr.ErrMessage)
catOptionalField("Detail", lr.ErrDetail)
catOptionalField("Hint", lr.ErrHint)
catOptionalField("Query", lr.UserQuery)
err := target.BufferMessage(134, time.Now(),
"postgres",
"postgres."+strconv.Itoa(int(lr.Pid)),
msgFmtBuf.Bytes())
if err != nil {
exit(err)
}
}
// Read the version message, calling exit if this is not a supported
// version.
func processVerMsg(msgInit msgInit, exit exitFn) {
var m core.Message
msgInit(&m, exit)
if m.MsgType() != 'V' {
exit("expected version ('V') message, "+
"but received %c", m.MsgType())
}
// hard-coded lengh limit, but it's very generous
if m.Size() > 10*KB {
log.Printf("oversized message string, msg size is %d",
m.Size())
}
s, err := buf.ReadCString(m.Payload())
if err != nil {
exit("couldn't read version string: %v", err)
}
if !(strings.HasPrefix(s, "PG-9.0") ||
strings.HasPrefix(s, "PG-9.1") ||
strings.HasPrefix(s, "PG-9.2") ||
strings.HasPrefix(s, "PG-9.3") ||
strings.HasPrefix(s, "PG-9.4") ||
strings.HasPrefix(s, "PG-9.5")) ||
!strings.HasSuffix(s, "/logfebe-1") {
exit("protocol version not supported: %s", s)
}
}
// Process the identity ('I') message, reporting the identity therein.
func processIdentMsg(msgInit msgInit, exit exitFn) string {
var m core.Message
msgInit(&m, exit)
// Read the remote system identifier string
if m.MsgType() != 'I' {
exit("expected identification ('I') message, "+
"but received %c", m.MsgType())
}
// hard-coded lengh limit, but it's very generous
if m.Size() > 10*KB {
log.Printf("oversized message string, msg size is %d",
m.Size())
}
s, err := buf.ReadCString(m.Payload())
if err != nil {
exit("couldn't read identification string: %v",
err)
}
return s
}