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

Commit

Permalink
fix: partial fix for dynamic MIB loading
Browse files Browse the repository at this point in the history
- I have just rewritten a very-basic lookup that works. I don't fully understand
  the mongodb-related part, but at least this should give us a basic point where
  we can start from.
  • Loading branch information
lstoppa authored and lingy1028 committed Jul 20, 2021
1 parent d6a29b9 commit b42ee99
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 36 deletions.
2 changes: 1 addition & 1 deletion splunk_connect_for_snmp_mib_server/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def delete_mib(self, filename):
self._mibs.delete_many({"filename": {"$regex": filename}})

def clear(self):
self._mibs.remove()
self._mibs.delete_many({})


class OidsRepository:
Expand Down
38 changes: 5 additions & 33 deletions splunk_connect_for_snmp_mib_server/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,40 +202,16 @@ def check_mongo(self, oid):

# Translate SNMP PDU varBinds into MIB objects using MIB
def mib_translator(self, var_bind):
# Run var-binds through MIB resolver
try:
name = var_bind["oid"]
val = var_bind["val"]
translated_var_bind = rfc1902.ObjectType(
rfc1902.ObjectIdentity(name), val
).resolveWithMib(self._mib_view_controller)

logger.debug(f"* Translated PDU: {translated_var_bind.prettyPrint()}")
trans_string = translated_var_bind.prettyPrint().replace(" = ", "=")
trans_oid = trans_string.split("=")[0]
trans_val = trans_string.split("=")[1]
untranslated_val_type = var_bind["val_type"]

except Exception as e:
logger.error(f"Error happened in translation: {e} trying to lazy load MIBs")
try:
# Check if oid exists in mongo oids collection and if oid was translated properly
no_mapping_mib_oid = self.check_mongo(name)
logger.debug(f"no_mapping_mib_oid: {no_mapping_mib_oid}")
if not no_mapping_mib_oid and self.is_not_translated(name, trans_oid):
self.find_mib_file(name)

# Check if value exists in mongo oids collection and if value was translated properly when value is OID type
if (
untranslated_val_type == "ObjectIdentifier"
or untranslated_val_type == "ObjectIdentity"
):
no_mapping_mib_val = self.check_mongo(val)
logger.debug(f"no_mapping_mib_val: {no_mapping_mib_val}")
if not no_mapping_mib_val and self.is_not_translated(
val, trans_val
):
self.find_mib_file(val)

# Re-translated with the extra mibs
self.find_mib_file(name)
translated_var_bind = rfc1902.ObjectType(
rfc1902.ObjectIdentity(name), val
).resolveWithMib(self._mib_view_controller)
Expand All @@ -245,13 +221,9 @@ def mib_translator(self, var_bind):

except Exception as e:
logger.debug(f"Error happened during translation checking: {e}")
pass
return None

return translated_var_bind.prettyPrint().replace(" = ", "=")
except Exception as e:
logger.error(f"Error happened in translation: {e}")
finally:
pass
return translated_var_bind.prettyPrint().replace(" = ", "=")

# Translate SNMP PDU varBinds into MIB objects using custom translation table
def custom_translator(self, oid):
Expand Down
6 changes: 4 additions & 2 deletions tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import yaml

from splunk_connect_for_snmp_mib_server.translator import Translator
from splunk_connect_for_snmp_mib_server.snmp_mib_server import upload_mibs
import os
import logging
import json
Expand Down Expand Up @@ -81,9 +82,9 @@ def setUp(self):
server_config = {
"snmp": {
"mibs": {
"dir": "mibs/pysnmp",
"dir": "./mibs/pysnmp",
"load_list": "lookups/mibs_list.csv",
"mibs_path": "mibs",
"mibs_path": "./mibs",
}
},
"mongo": {
Expand All @@ -92,6 +93,7 @@ def setUp(self):
},
}
self.my_translator = Translator(server_config)
upload_mibs(server_config)

@mongomock.patch()
def test_format_trap(self):
Expand Down

0 comments on commit b42ee99

Please sign in to comment.