Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 483ad2a

Browse files
authored
Merge pull request #66 from splunk/bug/addon-43531-changed-mongo
[ADDON-43531] new mongo way
2 parents bf747d1 + c43a5bf commit 483ad2a

File tree

5 files changed

+38
-34
lines changed

5 files changed

+38
-34
lines changed

splunk_connect_for_snmp_mib_server/mib_server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ def translator():
5959
logger.debug(request.json)
6060
var_binds = request.json.get("var_binds")
6161
data_format = request.args.get("data_format")
62-
logger.debug(f"type of var_binds: {type(var_binds)}")
63-
logger.debug(f"var_binds: {var_binds}")
64-
logger.debug(f"requested data format: {str(data_format)} ")
62+
logger.debug(
63+
f"type of var_binds: {type(var_binds)}\n"
64+
f"var_binds: {var_binds}\n"
65+
f"requested data format: {str(data_format)}"
66+
)
6567
if data_format in self.data_format:
6668
method = self.data_format.get(data_format)
6769
result = method(var_binds)

splunk_connect_for_snmp_mib_server/mongo.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# ########################################################################
1616
import logging
1717
import os
18+
import re
1819
import time
1920

2021
import pymongo
@@ -37,7 +38,7 @@ def __init__(self):
3738

3839

3940
class MibsRepository(MongoRepository):
40-
is_text_index_created = False
41+
oid_regex = re.compile(r"\(((?:1, 3, 6, 1, )(?:\d+,?\s?)+)\)")
4142

4243
def __init__(self, mongo_config):
4344
super().__init__()
@@ -49,31 +50,41 @@ def upload_files(self, mib_files_dir):
4950
file_path = mib_files_dir + "/" + filename
5051
with open(file_path, "r") as mib_file:
5152
try:
53+
read_file = mib_file.read()
5254
self._mibs.insert_one(
53-
dict(content=mib_file.read(), filename=filename, _id=filename)
55+
dict(
56+
content=read_file,
57+
filename=filename,
58+
_id=filename,
59+
mibs=self.parse_iods(read_file),
60+
)
5461
)
62+
self.parse_iods(read_file)
5563
except Exception:
5664
logger.exception(
5765
"Error happened during insert mib files %s into mongo", filename
5866
)
5967
toc = time.perf_counter()
6068
logger.info(f"Uploading files took - {toc - tic:0.4f} seconds")
6169

62-
def create_text_index(self):
70+
@staticmethod
71+
def parse_iods(read_file):
72+
return re.findall(MibsRepository.oid_regex, read_file)
73+
74+
def create_index_on_oids(self):
6375
tic = time.perf_counter()
6476
try:
65-
self._mibs.create_index([("content", pymongo.TEXT)], name="oid_index")
77+
self._mibs.create_index("mibs", name="oid_index")
6678
except Exception:
6779
logger.exception(
68-
"Failed to create the index, searches will be performed with regex"
80+
"Failed to create the index, searches will be performed without it"
6981
)
7082
return
7183
toc = time.perf_counter()
72-
MibsRepository.is_text_index_created = True
7384
logger.info(f"Creating index took - {toc - tic:0.4f} seconds")
7485

7586
def search_oid(self, oid):
76-
data = self.perform_correct_search(oid)
87+
data = self.perform_search(oid)
7788
if data:
7889
mib_list = []
7990
for item in data:
@@ -82,31 +93,23 @@ def search_oid(self, oid):
8293
else:
8394
return None
8495

85-
def perform_correct_search(self, oid):
86-
tic = time.perf_counter()
87-
if MibsRepository.is_text_index_created:
88-
data = self._mibs.find({"$text": {"$search": f'"{oid}"'}})
89-
else:
90-
data = self._mibs.find({"content": {"$regex": oid}})
91-
toc = time.perf_counter()
92-
logger.debug(
93-
f"We searched with {'Index' if MibsRepository.is_text_index_created else 'Regex'} "
94-
f"and the search took - {toc - tic:0.7f} seconds"
95-
)
96+
def perform_search(self, oid):
9697
tic = time.perf_counter()
97-
queried_cursor = list(data)
98+
data = list(self._mibs.find({"mibs": oid}))
9899
toc = time.perf_counter()
99100
logger.debug(
100-
f"data that we found {len(queried_cursor)}"
101-
f"and the query of the cursor take - {toc - tic:0.7f} seconds"
101+
f"We searched for oid - {oid} and it took - {toc - tic:0.7f} seconds"
102102
)
103-
return queried_cursor
103+
return data
104104

105105
def delete_mib(self, filename):
106106
self._mibs.delete_many({"filename": {"$regex": filename}})
107107

108108
def clear(self):
109-
self._mibs.delete_many({})
109+
tic = time.perf_counter()
110+
self._mibs.drop()
111+
toc = time.perf_counter()
112+
logger.info(f"Cleaning took - {toc - tic:0.4f} seconds")
110113

111114

112115
class OidsRepository(MongoRepository):

splunk_connect_for_snmp_mib_server/snmp_mib_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def upload_mibs(server_config):
4343

4444
# Upload all mib files in specific dir into mongo
4545
mibs_collection.upload_files(mib_files_dir)
46-
mibs_collection.create_text_index()
46+
mibs_collection.create_index_on_oids()
4747
logger.debug("Uploaded all mib files into mongo!")
4848

4949

splunk_connect_for_snmp_mib_server/translator.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,28 @@ def find_mib_file(self, oid):
129129
mib_list_without_index = None
130130

131131
try:
132-
mib_list = self._mongo_mibs_coll.search_oid(value_tuple)
133132
# Find mib module for OID without index (remove the last part of OID)
134133
# Handle the scenario that tries to translate an OID which has index append at the end.
135134
# e.g 1.3.6.1.2.1.25.1.6.0, where 0 is index and it's not part of the OID object
136135
# So we cannot find mapping MIBs for it
137136
# Instead, 1.3.6.1.2.1.25.1.6 is actually the OID that needed to be used for searching MIBs
138137
# Therefore, we should remove index (0) and search the real oid (1.3.6.1.2.1.25.1.6) to detect the MIBs
139-
logger.debug(f"[-] oid_without_index: {oid_without_index}")
138+
139+
mib_list = self._mongo_mibs_coll.search_oid(value_tuple)
140140
mib_list_without_index = self._mongo_mibs_coll.search_oid(
141141
changed_oid_without_index
142142
)
143143
except Exception:
144144
logger.exception(
145-
f"Error happened during search for the oid - {oid} in mongo mibs collection"
145+
f"Error happened during search for the oids - {oid},"
146+
f" {changed_oid_without_index} in mongo mibs collection"
146147
)
147148
self.add_oid_to_db(mib_list, oid)
148-
self.add_oid_to_db(mib_list_without_index, oid)
149+
self.add_oid_to_db(mib_list_without_index, changed_oid_without_index)
149150
if mib_list:
150151
self.load_extra_mibs(mib_list, oid)
151152
if mib_list_without_index:
152-
self.load_extra_mibs(mib_list_without_index, oid)
153+
self.load_extra_mibs(mib_list_without_index, changed_oid_without_index)
153154

154155
def add_oid_to_db(self, mib_list, oid):
155156
if not mib_list:

tests/test_translator.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
)
2828
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType
2929

30-
from splunk_connect_for_snmp_mib_server.mongo import MibsRepository
3130
from splunk_connect_for_snmp_mib_server.snmp_mib_server import upload_mibs
3231
from splunk_connect_for_snmp_mib_server.translator import Translator
3332

@@ -101,7 +100,6 @@ def setUp(self):
101100
# server_config["snmp"]["mibs"]["dir"] = "../mibs/pysnmp"
102101
self.my_translator = Translator(server_config)
103102
upload_mibs(server_config)
104-
MibsRepository.is_text_index_created = False
105103

106104
@mongomock.patch()
107105
def test_format_trap(self):

0 commit comments

Comments
 (0)