Skip to content

Commit

Permalink
Add isolated MIB loaders to support multiple sets of MIBs
Browse files Browse the repository at this point in the history
  • Loading branch information
iwanb committed Feb 5, 2017
1 parent 123ca18 commit 86cb8d6
Show file tree
Hide file tree
Showing 6 changed files with 1,075 additions and 445 deletions.
34 changes: 21 additions & 13 deletions snimpy/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import inspect
from time import time
from collections import MutableMapping, Container, Iterable, Sized
from collections import MutableMapping, Container, Iterable, Sized, defaultdict
from snimpy import snmp, mib, basictypes


Expand Down Expand Up @@ -234,7 +234,8 @@ def __init__(self,
secname=None,
authprotocol=None, authpassword=None,
privprotocol=None, privpassword=None,
contextname=None):
contextname=None,
mibloader=None):
"""Create a new SNMP manager. Some of the parameters are explained in
:meth:`snmp.Session.__init__`.
Expand Down Expand Up @@ -271,7 +272,12 @@ def __init__(self,
:param bulk: Max-repetition to use to speed up MIB walking
with `GETBULK`. Set to `0` to disable.
:type bulk: int
:param mibloader: The MibLoader instance which has the loaded MIBs
:type mibloader: MibLoader
"""
if mibloader is None:
mibloader = mib._mibloader
self._mibloader = mibloader
if host is None:
host = Manager._host
self._host = host
Expand All @@ -293,7 +299,7 @@ def __init__(self,
if none:
self._session = NoneSession(self._session)
self._loose = loose
self._loaded = loaded
self._loaded = loaded[self._mibloader]

# To be able to clone, we save the arguments provided to the
# constructor in a generic way
Expand All @@ -306,7 +312,7 @@ def __init__(self,
def _locate(self, attribute):
for m in self._loaded:
try:
a = mib.get(m, attribute)
a = self._mibloader.get(m, attribute)
return (m, a)
except mib.SMIException:
pass
Expand Down Expand Up @@ -345,7 +351,7 @@ def __setattr__(self, attribute, value):

def __getitem__(self, modulename):
modulename = modulename.encode('ascii')
for m in loaded:
for m in loaded[self._mibloader]:
if modulename == m:
return MibRestrictedManager(self, [m])
raise KeyError("{0} is not a loaded module".format(modulename))
Expand Down Expand Up @@ -568,20 +574,22 @@ def iteritems(self, table_filter=None):
return ProxyIter.iteritems(self, resulting_filter)


loaded = []
loaded = defaultdict(list)


def load(mibname):
def load(mibname, mibloader=None):
"""Load a MIB in memory.
:param mibname: MIB name or filename
:type mibname: str
"""
m = mib.load(mibname)
if m not in loaded:
loaded.append(m)
if mibloader is None:
mibloader = mib._mibloader
m = mibloader.load(mibname)
if m not in loaded[mibloader]:
loaded[mibloader].append(m)
if Manager._complete:
for o in mib.getScalars(m) + \
mib.getColumns(m) + \
mib.getTables(m):
for o in mibloader.getScalars(m) + \
mibloader.getColumns(m) + \
mibloader.getTables(m):
setattr(Manager, str(o), 1)
Loading

0 comments on commit 86cb8d6

Please sign in to comment.