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
|
# MIB modules loader
import os
from pysnmp.smi import error
try:
import pysnmp_mibs
except ImportError:
pysnmp_mibs = None
from pysnmp import debug
class MibBuilder:
def __init__(self):
self.lastBuildId = self._autoName = 0L
paths = (
os.path.join(os.path.split(error.__file__)[0], 'mibs','instances'),
os.path.join(os.path.split(error.__file__)[0], 'mibs')
)
if os.environ.has_key('PYSNMP_MIB_DIR'):
paths = paths + (
os.path.join(os.path.split(os.environ['PYSNMP_MIB_DIR'])[0]),
)
if pysnmp_mibs:
paths = paths + (
os.path.join(os.path.split(pysnmp_mibs.__file__)[0]),
)
self.mibSymbols = {}
self.__modSeen = {}
self.__modPathsSeen = {}
apply(self.setMibPath, paths)
# MIB modules management
def setMibPath(self, *mibPaths):
self.__mibPaths = map(os.path.normpath, mibPaths)
debug.logger & debug.flagBld and debug.logger('setMibPath: new MIB path %s' % (self.__mibPaths,))
def getMibPath(self): return tuple(self.__mibPaths)
def loadModules(self, *modNames):
# Build a list of available modules
if not modNames:
modNames = {}
for mibPath in self.__mibPaths:
try:
for modName in os.listdir(mibPath):
if modName == '__init__.py' or modName[-3:] != '.py':
continue
modNames[modName[:-3]] = None
except OSError:
continue
modNames = modNames.keys()
if not modNames:
raise error.SmiError(
'No MIB module to load at %s' % (self,)
)
for modName in modNames:
for mibPath in self.__mibPaths:
modPath = os.path.join(
mibPath, modName + '.py'
)
debug.logger & debug.flagBld and debug.logger('loadModules: trying %s' % modPath)
try:
open(modPath).close()
except IOError, why:
debug.logger & debug.flagBld and debug.logger('loadModules: open() %s' % why)
continue
if self.__modPathsSeen.has_key(modPath):
debug.logger & debug.flagBld and debug.logger('loadModules: seen %s' % modPath)
continue
else:
self.__modPathsSeen[modPath] = 1
g = { 'mibBuilder': self }
try:
execfile(modPath, g)
except StandardError, why:
del self.__modPathsSeen[modPath]
raise error.SmiError(
'MIB module \"%s\" load error: %s' % (modPath, why)
)
self.__modSeen[modName] = modPath
debug.logger & debug.flagBld and debug.logger('loadModules: loaded %s' % modPath)
break
if not self.__modSeen.has_key(modName):
raise error.SmiError(
'MIB file \"%s.py\" not found in search path' % modName
)
return self
def unloadModules(self, *modNames):
if not modNames:
modNames = self.mibSymbols.keys()
for modName in modNames:
if not self.mibSymbols.has_key(modName):
raise error.SmiError(
'No module %s at %s' % (modName, self)
)
self.unexportSymbols(modName)
del self.__modPathsSeen[self.__modSeen[modName]]
del self.__modSeen[modName]
debug.logger & debug.flagBld and debug.logger('unloadModules: ' % (modName))
return self
def importSymbols(self, modName, *symNames):
r = ()
for symName in symNames:
if not self.mibSymbols.has_key(modName):
self.loadModules(modName)
if not self.mibSymbols.has_key(modName):
raise error.SmiError(
'No module %s loaded at %s' % (modName, self)
)
if not self.mibSymbols[modName].has_key(symName):
raise error.SmiError(
'No symbol %s::%s at %s' % (modName, symName, self)
)
r = r + (self.mibSymbols[modName][symName],)
return r
def exportSymbols(self, modName, *anonymousSyms, **namedSyms):
if not self.mibSymbols.has_key(modName):
self.mibSymbols[modName] = {}
mibSymbols = self.mibSymbols[modName]
for symObj in anonymousSyms:
debug.logger & debug.flagBld and debug.logger('exportSymbols: anonymous symbol %s::__pysnmp_%ld' % (modName, self._autoName))
mibSymbols['__pysnmp_%ld' % self._autoName] = symObj
self._autoName = self._autoName + 1
for symName, symObj in namedSyms.items():
if mibSymbols.has_key(symName):
raise error.SmiError(
'Symbol %s already exported at %s' % (symName, modName)
)
if hasattr(symObj, 'label') and symObj.label:
symName = symObj.label
mibSymbols[symName] = symObj
debug.logger & debug.flagBld and debug.logger('exportSymbols: symbol %s::%s' % (modName, symName))
self.lastBuildId = self.lastBuildId + 1
def unexportSymbols(self, modName, *symNames):
if not self.mibSymbols.has_key(modName):
raise error.SmiError(
'No module %s at %s' % (modName, self)
)
mibSymbols = self.mibSymbols[modName]
if not symNames:
symNames = mibSymbols.keys()
for symName in symNames:
if not mibSymbols.has_key(symName):
raise error.SmiError(
'No symbol %s::%s at %s' % (modName, symName, self)
)
del mibSymbols[symName]
debug.logger & debug.flagBld and debug.logger('unexportSymbols: symbol %s::%s' % (modName, symName))
self.lastBuildId = self.lastBuildId + 1
|