forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
922 lines (782 loc) · 22.4 KB
/
mysql.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
package mysql
import (
"errors"
"expvar"
"fmt"
"strings"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/packetbeat/procs"
"github.com/elastic/beats/packetbeat/protos"
"github.com/elastic/beats/packetbeat/protos/tcp"
"github.com/elastic/beats/packetbeat/publish"
)
// Packet types
const (
mysqlCmdQuery = 3
)
const maxPayloadSize = 100 * 1024
var (
unmatchedRequests = expvar.NewInt("mysql.unmatched_requests")
unmatchedResponses = expvar.NewInt("mysql.unmatched_responses")
)
type mysqlMessage struct {
start int
end int
ts time.Time
isRequest bool
packetLength uint32
seq uint8
typ uint8
numberOfRows int
numberOfFields int
size uint64
fields []string
rows [][]string
tables string
isOK bool
affectedRows uint64
insertID uint64
isError bool
errorCode uint16
errorInfo string
query string
ignoreMessage bool
direction uint8
isTruncated bool
tcpTuple common.TCPTuple
cmdlineTuple *common.CmdlineTuple
raw []byte
notes []string
}
type mysqlTransaction struct {
tuple common.TCPTuple
src common.Endpoint
dst common.Endpoint
responseTime int32
ts time.Time
query string
method string
path string // for mysql, Path refers to the mysql table queried
bytesOut uint64
bytesIn uint64
notes []string
mysql common.MapStr
requestRaw string
responseRaw string
}
type mysqlStream struct {
data []byte
parseOffset int
parseState parseState
isClient bool
message *mysqlMessage
}
type parseState int
const (
mysqlStateStart parseState = iota
mysqlStateEatMessage
mysqlStateEatFields
mysqlStateEatRows
mysqlStateMax
)
var stateStrings = []string{
"Start",
"EatMessage",
"EatFields",
"EatRows",
}
func (state parseState) String() string {
return stateStrings[state]
}
type mysqlPlugin struct {
// config
ports []int
maxStoreRows int
maxRowLength int
sendRequest bool
sendResponse bool
transactions *common.Cache
transactionTimeout time.Duration
results publish.Transactions
// function pointer for mocking
handleMysql func(mysql *mysqlPlugin, m *mysqlMessage, tcp *common.TCPTuple,
dir uint8, raw_msg []byte)
}
func init() {
protos.Register("mysql", New)
}
func New(
testMode bool,
results publish.Transactions,
cfg *common.Config,
) (protos.Plugin, error) {
p := &mysqlPlugin{}
config := defaultConfig
if !testMode {
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
}
if err := p.init(results, &config); err != nil {
return nil, err
}
return p, nil
}
func (mysql *mysqlPlugin) init(results publish.Transactions, config *mysqlConfig) error {
mysql.setFromConfig(config)
mysql.transactions = common.NewCache(
mysql.transactionTimeout,
protos.DefaultTransactionHashSize)
mysql.transactions.StartJanitor(mysql.transactionTimeout)
mysql.handleMysql = handleMysql
mysql.results = results
return nil
}
func (mysql *mysqlPlugin) setFromConfig(config *mysqlConfig) {
mysql.ports = config.Ports
mysql.maxRowLength = config.MaxRowLength
mysql.maxStoreRows = config.MaxRows
mysql.sendRequest = config.SendRequest
mysql.sendResponse = config.SendResponse
mysql.transactionTimeout = config.TransactionTimeout
}
func (mysql *mysqlPlugin) getTransaction(k common.HashableTCPTuple) *mysqlTransaction {
v := mysql.transactions.Get(k)
if v != nil {
return v.(*mysqlTransaction)
}
return nil
}
func (mysql *mysqlPlugin) GetPorts() []int {
return mysql.ports
}
func (stream *mysqlStream) prepareForNewMessage() {
stream.data = stream.data[stream.parseOffset:]
stream.parseState = mysqlStateStart
stream.parseOffset = 0
stream.isClient = false
stream.message = nil
}
func mysqlMessageParser(s *mysqlStream) (bool, bool) {
logp.Debug("mysqldetailed", "MySQL parser called. parseState = %s", s.parseState)
m := s.message
for s.parseOffset < len(s.data) {
switch s.parseState {
case mysqlStateStart:
m.start = s.parseOffset
if len(s.data[s.parseOffset:]) < 5 {
logp.Warn("MySQL Message too short. Ignore it.")
return false, false
}
hdr := s.data[s.parseOffset : s.parseOffset+5]
m.packetLength = uint32(hdr[0]) | uint32(hdr[1])<<8 | uint32(hdr[2])<<16
m.seq = hdr[3]
m.typ = hdr[4]
logp.Debug("mysqldetailed", "MySQL Header: Packet length %d, Seq %d, Type=%d", m.packetLength, m.seq, m.typ)
if m.seq == 0 {
// starts Command Phase
if m.typ == mysqlCmdQuery {
// parse request
m.isRequest = true
m.start = s.parseOffset
s.parseState = mysqlStateEatMessage
} else {
// ignore command
m.ignoreMessage = true
s.parseState = mysqlStateEatMessage
}
if !s.isClient {
s.isClient = true
}
} else if !s.isClient {
// parse response
m.isRequest = false
if hdr[4] == 0x00 || hdr[4] == 0xfe {
logp.Debug("mysqldetailed", "Received OK response")
m.start = s.parseOffset
s.parseState = mysqlStateEatMessage
m.isOK = true
} else if hdr[4] == 0xff {
logp.Debug("mysqldetailed", "Received ERR response")
m.start = s.parseOffset
s.parseState = mysqlStateEatMessage
m.isError = true
} else if m.packetLength == 1 {
logp.Debug("mysqldetailed", "Query response. Number of fields %d", hdr[4])
m.numberOfFields = int(hdr[4])
m.start = s.parseOffset
s.parseOffset += 5
s.parseState = mysqlStateEatFields
} else {
// something else. ignore
m.ignoreMessage = true
s.parseState = mysqlStateEatMessage
}
} else {
// something else, not expected
logp.Debug("mysql", "Unexpected MySQL message of type %d received.", m.typ)
return false, false
}
case mysqlStateEatMessage:
if len(s.data[s.parseOffset:]) < int(m.packetLength)+4 {
// wait for more data
return true, false
}
s.parseOffset += 4 //header
s.parseOffset += int(m.packetLength)
m.end = s.parseOffset
if m.isRequest {
m.query = string(s.data[m.start+5 : m.end])
} else if m.isOK {
// affected rows
affectedRows, off, complete, err := readLinteger(s.data, m.start+5)
if !complete {
return true, false
}
if err != nil {
logp.Debug("mysql", "Error on read_linteger: %s", err)
return false, false
}
m.affectedRows = affectedRows
// last insert id
insertID, _, complete, err := readLinteger(s.data, off)
if !complete {
return true, false
}
if err != nil {
logp.Debug("mysql", "Error on read_linteger: %s", err)
return false, false
}
m.insertID = insertID
} else if m.isError {
// int<1>header (0xff)
// int<2>error code
// string[1] sql state marker
// string[5] sql state
// string<EOF> error message
m.errorCode = uint16(s.data[m.start+6])<<8 | uint16(s.data[m.start+5])
m.errorInfo = string(s.data[m.start+8:m.start+13]) + ": " + string(s.data[m.start+13:])
}
m.size = uint64(m.end - m.start)
logp.Debug("mysqldetailed", "Message complete. remaining=%d",
len(s.data[s.parseOffset:]))
return true, true
case mysqlStateEatFields:
if len(s.data[s.parseOffset:]) < 4 {
// wait for more
return true, false
}
hdr := s.data[s.parseOffset : s.parseOffset+4]
m.packetLength = uint32(hdr[0]) | uint32(hdr[1])<<8 | uint32(hdr[2])<<16
m.seq = hdr[3]
logp.Debug("mysqldetailed", "Fields: packet length %d, packet number %d", m.packetLength, m.seq)
if len(s.data[s.parseOffset:]) >= int(m.packetLength)+4 {
s.parseOffset += 4 // header
if s.data[s.parseOffset] == 0xfe {
logp.Debug("mysqldetailed", "Received EOF packet")
// EOF marker
s.parseOffset += int(m.packetLength)
s.parseState = mysqlStateEatRows
} else {
_ /* catalog */, off, complete, err := readLstring(s.data, s.parseOffset)
if !complete {
return true, false
}
if err != nil {
logp.Debug("mysql", "Error on read_lstring: %s", err)
return false, false
}
db /*schema */, off, complete, err := readLstring(s.data, off)
if !complete {
return true, false
}
if err != nil {
logp.Debug("mysql", "Error on read_lstring: %s", err)
return false, false
}
table /* table */, _ /*off*/, complete, err := readLstring(s.data, off)
if !complete {
return true, false
}
if err != nil {
logp.Debug("mysql", "Error on read_lstring: %s", err)
return false, false
}
dbTable := string(db) + "." + string(table)
if len(m.tables) == 0 {
m.tables = dbTable
} else if !strings.Contains(m.tables, dbTable) {
m.tables = m.tables + ", " + dbTable
}
logp.Debug("mysqldetailed", "db=%s, table=%s", db, table)
s.parseOffset += int(m.packetLength)
// go to next field
}
} else {
// wait for more
return true, false
}
case mysqlStateEatRows:
if len(s.data[s.parseOffset:]) < 4 {
// wait for more
return true, false
}
hdr := s.data[s.parseOffset : s.parseOffset+4]
m.packetLength = uint32(hdr[0]) | uint32(hdr[1])<<8 | uint32(hdr[2])<<16
m.seq = hdr[3]
logp.Debug("mysqldetailed", "Rows: packet length %d, packet number %d", m.packetLength, m.seq)
if len(s.data[s.parseOffset:]) < int(m.packetLength)+4 {
// wait for more
return true, false
}
s.parseOffset += 4 //header
if s.data[s.parseOffset] == 0xfe {
logp.Debug("mysqldetailed", "Received EOF packet")
// EOF marker
s.parseOffset += int(m.packetLength)
if m.end == 0 {
m.end = s.parseOffset
} else {
m.isTruncated = true
}
if !m.isError {
// in case the response was sent successfully
m.isOK = true
}
m.size = uint64(m.end - m.start)
return true, true
}
s.parseOffset += int(m.packetLength)
if m.end == 0 && s.parseOffset > maxPayloadSize {
// only send up to here, but read until the end
m.end = s.parseOffset
}
m.numberOfRows++
// go to next row
}
}
return true, false
}
// messageGap is called when a gap of size `nbytes` is found in the
// tcp stream. Returns true if there is already enough data in the message
// read so far that we can use it further in the stack.
func (mysql *mysqlPlugin) messageGap(s *mysqlStream, nbytes int) (complete bool) {
m := s.message
switch s.parseState {
case mysqlStateStart, mysqlStateEatMessage:
// not enough data yet to be useful
return false
case mysqlStateEatFields, mysqlStateEatRows:
// enough data here
m.end = s.parseOffset
if m.isRequest {
m.notes = append(m.notes, "Packet loss while capturing the request")
} else {
m.notes = append(m.notes, "Packet loss while capturing the response")
}
return true
}
return true
}
type mysqlPrivateData struct {
data [2]*mysqlStream
}
// Called when the parser has identified a full message.
func (mysql *mysqlPlugin) messageComplete(tcptuple *common.TCPTuple, dir uint8, stream *mysqlStream) {
// all ok, ship it
msg := stream.data[stream.message.start:stream.message.end]
if !stream.message.ignoreMessage {
mysql.handleMysql(mysql, stream.message, tcptuple, dir, msg)
}
// and reset message
stream.prepareForNewMessage()
}
func (mysql *mysqlPlugin) ConnectionTimeout() time.Duration {
return mysql.transactionTimeout
}
func (mysql *mysqlPlugin) Parse(pkt *protos.Packet, tcptuple *common.TCPTuple,
dir uint8, private protos.ProtocolData) protos.ProtocolData {
defer logp.Recover("ParseMysql exception")
priv := mysqlPrivateData{}
if private != nil {
var ok bool
priv, ok = private.(mysqlPrivateData)
if !ok {
priv = mysqlPrivateData{}
}
}
if priv.data[dir] == nil {
priv.data[dir] = &mysqlStream{
data: pkt.Payload,
message: &mysqlMessage{ts: pkt.Ts},
}
} else {
// concatenate bytes
priv.data[dir].data = append(priv.data[dir].data, pkt.Payload...)
if len(priv.data[dir].data) > tcp.TCPMaxDataInStream {
logp.Debug("mysql", "Stream data too large, dropping TCP stream")
priv.data[dir] = nil
return priv
}
}
stream := priv.data[dir]
for len(stream.data) > 0 {
if stream.message == nil {
stream.message = &mysqlMessage{ts: pkt.Ts}
}
ok, complete := mysqlMessageParser(priv.data[dir])
//logp.Debug("mysqldetailed", "mysqlMessageParser returned ok=%b complete=%b", ok, complete)
if !ok {
// drop this tcp stream. Will retry parsing with the next
// segment in it
priv.data[dir] = nil
logp.Debug("mysql", "Ignore MySQL message. Drop tcp stream. Try parsing with the next segment")
return priv
}
if complete {
mysql.messageComplete(tcptuple, dir, stream)
} else {
// wait for more data
break
}
}
return priv
}
func (mysql *mysqlPlugin) GapInStream(tcptuple *common.TCPTuple, dir uint8,
nbytes int, private protos.ProtocolData) (priv protos.ProtocolData, drop bool) {
defer logp.Recover("GapInStream(mysql) exception")
if private == nil {
return private, false
}
mysqlData, ok := private.(mysqlPrivateData)
if !ok {
return private, false
}
stream := mysqlData.data[dir]
if stream == nil || stream.message == nil {
// nothing to do
return private, false
}
if mysql.messageGap(stream, nbytes) {
// we need to publish from here
mysql.messageComplete(tcptuple, dir, stream)
}
// we always drop the TCP stream. Because it's binary and len based,
// there are too few cases in which we could recover the stream (maybe
// for very large blobs, leaving that as TODO)
return private, true
}
func (mysql *mysqlPlugin) ReceivedFin(tcptuple *common.TCPTuple, dir uint8,
private protos.ProtocolData) protos.ProtocolData {
// TODO: check if we have data pending and either drop it to free
// memory or send it up the stack.
return private
}
func handleMysql(mysql *mysqlPlugin, m *mysqlMessage, tcptuple *common.TCPTuple,
dir uint8, rawMsg []byte) {
m.tcpTuple = *tcptuple
m.direction = dir
m.cmdlineTuple = procs.ProcWatcher.FindProcessesTuple(tcptuple.IPPort())
m.raw = rawMsg
if m.isRequest {
mysql.receivedMysqlRequest(m)
} else {
mysql.receivedMysqlResponse(m)
}
}
func (mysql *mysqlPlugin) receivedMysqlRequest(msg *mysqlMessage) {
tuple := msg.tcpTuple
trans := mysql.getTransaction(tuple.Hashable())
if trans != nil {
if trans.mysql != nil {
logp.Debug("mysql", "Two requests without a Response. Dropping old request: %s", trans.mysql)
unmatchedRequests.Add(1)
}
} else {
trans = &mysqlTransaction{tuple: tuple}
mysql.transactions.Put(tuple.Hashable(), trans)
}
trans.ts = msg.ts
trans.src = common.Endpoint{
IP: msg.tcpTuple.SrcIP.String(),
Port: msg.tcpTuple.SrcPort,
Proc: string(msg.cmdlineTuple.Src),
}
trans.dst = common.Endpoint{
IP: msg.tcpTuple.DstIP.String(),
Port: msg.tcpTuple.DstPort,
Proc: string(msg.cmdlineTuple.Dst),
}
if msg.direction == tcp.TCPDirectionReverse {
trans.src, trans.dst = trans.dst, trans.src
}
// Extract the method, by simply taking the first word and
// making it upper case.
query := strings.Trim(msg.query, " \n\t")
index := strings.IndexAny(query, " \n\t")
var method string
if index > 0 {
method = strings.ToUpper(query[:index])
} else {
method = strings.ToUpper(query)
}
trans.query = query
trans.method = method
trans.mysql = common.MapStr{}
trans.notes = msg.notes
// save Raw message
trans.requestRaw = msg.query
trans.bytesIn = msg.size
}
func (mysql *mysqlPlugin) receivedMysqlResponse(msg *mysqlMessage) {
trans := mysql.getTransaction(msg.tcpTuple.Hashable())
if trans == nil {
logp.Debug("mysql", "Response from unknown transaction. Ignoring.")
unmatchedResponses.Add(1)
return
}
// check if the request was received
if trans.mysql == nil {
logp.Debug("mysql", "Response from unknown transaction. Ignoring.")
unmatchedResponses.Add(1)
return
}
// save json details
trans.mysql.Update(common.MapStr{
"affected_rows": msg.affectedRows,
"insert_id": msg.insertID,
"num_rows": msg.numberOfRows,
"num_fields": msg.numberOfFields,
"iserror": msg.isError,
"error_code": msg.errorCode,
"error_message": msg.errorInfo,
})
trans.bytesOut = msg.size
trans.path = msg.tables
trans.responseTime = int32(msg.ts.Sub(trans.ts).Nanoseconds() / 1e6) // resp_time in milliseconds
// save Raw message
if len(msg.raw) > 0 {
fields, rows := mysql.parseMysqlResponse(msg.raw)
trans.responseRaw = common.DumpInCSVFormat(fields, rows)
}
trans.notes = append(trans.notes, msg.notes...)
mysql.publishTransaction(trans)
mysql.transactions.Delete(trans.tuple.Hashable())
logp.Debug("mysql", "Mysql transaction completed: %s", trans.mysql)
logp.Debug("mysql", "%s", trans.responseRaw)
}
func (mysql *mysqlPlugin) parseMysqlResponse(data []byte) ([]string, [][]string) {
length, err := readLength(data, 0)
if err != nil {
logp.Warn("Invalid response: %v", err)
return []string{}, [][]string{}
}
if length < 1 {
logp.Warn("Warning: Skipping empty Response")
return []string{}, [][]string{}
}
fields := []string{}
rows := [][]string{}
if len(data) < 5 {
logp.Warn("Invalid response: data less than 4 bytes")
return []string{}, [][]string{}
}
if data[4] == 0x00 {
// OK response
} else if data[4] == 0xff {
// Error response
} else {
offset := 5
logp.Debug("mysql", "Data len: %d", len(data))
// Read fields
for {
length, err = readLength(data, offset)
if err != nil {
logp.Warn("Invalid response: %v", err)
return []string{}, [][]string{}
}
if len(data[offset:]) < 5 {
logp.Warn("Invalid response.")
return []string{}, [][]string{}
}
if data[offset+4] == 0xfe {
// EOF
offset += length + 4
break
}
_ /* catalog */, off, complete, err := readLstring(data, offset+4)
if err != nil || !complete {
logp.Debug("mysql", "Reading field: %v %v", err, complete)
return fields, rows
}
_ /*database*/, off, complete, err = readLstring(data, off)
if err != nil || !complete {
logp.Debug("mysql", "Reading field: %v %v", err, complete)
return fields, rows
}
_ /*table*/, off, complete, err = readLstring(data, off)
if err != nil || !complete {
logp.Debug("mysql", "Reading field: %v %v", err, complete)
return fields, rows
}
_ /*org table*/, off, complete, err = readLstring(data, off)
if err != nil || !complete {
logp.Debug("mysql", "Reading field: %v %v", err, complete)
return fields, rows
}
name, off, complete, err := readLstring(data, off)
if err != nil || !complete {
logp.Debug("mysql", "Reading field: %v %v", err, complete)
return fields, rows
}
_ /* org name */, _ /*off*/, complete, err = readLstring(data, off)
if err != nil || !complete {
logp.Debug("mysql", "Reading field: %v %v", err, complete)
return fields, rows
}
fields = append(fields, string(name))
offset += length + 4
if len(data) < offset {
logp.Warn("Invalid response.")
return []string{}, [][]string{}
}
}
// Read rows
for offset < len(data) {
var row []string
var rowLen int
if len(data[offset:]) < 5 {
logp.Warn("Invalid response.")
break
}
if data[offset+4] == 0xfe {
// EOF
offset += length + 4
break
}
length, err = readLength(data, offset)
if err != nil {
logp.Warn("Invalid response: %v", err)
break
}
off := offset + 4 // skip length + packet number
start := off
for off < start+length {
var text []byte
if data[off] == 0xfb {
text = []byte("NULL")
off++
} else {
var err error
var complete bool
text, off, complete, err = readLstring(data, off)
if err != nil || !complete {
logp.Debug("mysql", "Error parsing rows: %s %b", err, complete)
// nevertheless, return what we have so far
return fields, rows
}
}
if rowLen < mysql.maxRowLength {
if rowLen+len(text) > mysql.maxRowLength {
text = text[:mysql.maxRowLength-rowLen]
}
row = append(row, string(text))
rowLen += len(text)
}
}
logp.Debug("mysqldetailed", "Append row: %v", row)
rows = append(rows, row)
if len(rows) >= mysql.maxStoreRows {
break
}
offset += length + 4
}
}
return fields, rows
}
func (mysql *mysqlPlugin) publishTransaction(t *mysqlTransaction) {
if mysql.results == nil {
return
}
logp.Debug("mysql", "mysql.results exists")
event := common.MapStr{}
event["type"] = "mysql"
if t.mysql["iserror"].(bool) {
event["status"] = common.ERROR_STATUS
} else {
event["status"] = common.OK_STATUS
}
event["responsetime"] = t.responseTime
if mysql.sendRequest {
event["request"] = t.requestRaw
}
if mysql.sendResponse {
event["response"] = t.responseRaw
}
event["method"] = t.method
event["query"] = t.query
event["mysql"] = t.mysql
event["path"] = t.path
event["bytes_out"] = t.bytesOut
event["bytes_in"] = t.bytesIn
if len(t.notes) > 0 {
event["notes"] = t.notes
}
event["@timestamp"] = common.Time(t.ts)
event["src"] = &t.src
event["dst"] = &t.dst
mysql.results.PublishTransaction(event)
}
func readLstring(data []byte, offset int) ([]byte, int, bool, error) {
length, off, complete, err := readLinteger(data, offset)
if err != nil {
return nil, 0, false, err
}
if !complete || len(data[off:]) < int(length) {
return nil, 0, false, nil
}
return data[off : off+int(length)], off + int(length), true, nil
}
func readLinteger(data []byte, offset int) (uint64, int, bool, error) {
if len(data) < offset+1 {
return 0, 0, false, nil
}
switch data[offset] {
case 0xfe:
if len(data[offset+1:]) < 8 {
return 0, 0, false, nil
}
return uint64(data[offset+1]) | uint64(data[offset+2])<<8 |
uint64(data[offset+2])<<16 | uint64(data[offset+3])<<24 |
uint64(data[offset+4])<<32 | uint64(data[offset+5])<<40 |
uint64(data[offset+6])<<48 | uint64(data[offset+7])<<56,
offset + 9, true, nil
case 0xfd:
if len(data[offset+1:]) < 3 {
return 0, 0, false, nil
}
return uint64(data[offset+1]) | uint64(data[offset+2])<<8 |
uint64(data[offset+3])<<16, offset + 4, true, nil
case 0xfc:
if len(data[offset+1:]) < 2 {
return 0, 0, false, nil
}
return uint64(data[offset+1]) | uint64(data[offset+2])<<8, offset + 3, true, nil
}
if uint64(data[offset]) >= 0xfb {
return 0, 0, false, fmt.Errorf("Unexpected value in read_linteger")
}
return uint64(data[offset]), offset + 1, true, nil
}
// Read a mysql length field (3 bytes LE)
func readLength(data []byte, offset int) (int, error) {
if len(data[offset:]) < 3 {
return 0, errors.New("Data too small to contain a valid length")
}
length := uint32(data[offset]) |
uint32(data[offset+1])<<8 |
uint32(data[offset+2])<<16
return int(length), nil
}