Skip to content

Commit

Permalink
[Packetbeat] [MongoDB] Report unknown opcodes once (elastic#10878) (e…
Browse files Browse the repository at this point in the history
…lastic#10886)

This changes the mongoDB decoder reporting unknown opcodes to report
each unknown opcode only once, to avoid flooding the log file with
errors.

(cherry picked from commit 755e2aa)
  • Loading branch information
adriansr authored Feb 22, 2019
1 parent 2c1c424 commit 32deb26
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ https://github.com/elastic/beats/compare/v6.6.1...6.6[Check the HEAD diff]

*Packetbeat*

- Avoid reporting unknown MongoDB opcodes more than once. {pull}10878[10878]

*Winlogbeat*

*Functionbeat*
Expand Down
13 changes: 12 additions & 1 deletion packetbeat/protos/mongodb/mongodb_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ import (
"encoding/json"
"errors"
"strings"
"sync"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"

"gopkg.in/mgo.v2/bson"
)

var (
unknownOpcodes = map[opCode]struct{}{}
mutex sync.Mutex
)

func mongodbMessageParser(s *stream) (bool, bool) {
d := newDecoder(s.data)

Expand Down Expand Up @@ -56,7 +62,12 @@ func mongodbMessageParser(s *stream) (bool, bool) {
opCode := opCode(code)

if !validOpcode(opCode) {
logp.Err("Unknown operation code: %v", opCode)
mutex.Lock()
defer mutex.Unlock()
if _, reported := unknownOpcodes[opCode]; !reported {
logp.Err("Unknown operation code: %v", opCode)
unknownOpcodes[opCode] = struct{}{}
}
return false, false
}

Expand Down
Binary file not shown.
12 changes: 12 additions & 0 deletions packetbeat/tests/system/test_0025_mongodb_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,15 @@ def test_request_after_reply(self):
o = objs[0]
assert o["type"] == "mongodb"
assert o["responsetime"] >= 0

def test_unknown_opcode_flood(self):
"""
Tests that a repeated unknown opcode is reported just once.
"""
self.render_config_template(
mongodb_ports=[9991]
)
self.run_packetbeat(pcap="mongodb_op_msg_opcode.pcap",
debug_selectors=["mongodb"])
num_msgs = self.log_contains_count('Unknown operation code: ')
assert num_msgs == 1, "Unknown opcode reported more than once: {0}".format(num_msgs)

0 comments on commit 32deb26

Please sign in to comment.