def mib2pysnmp2(mib_file, output_dir): """ The 'build-pysnmp-mib' script we previously used is no longer available Latest pysmi has the ability to generate a .py file from .mib automatically :param mib_file: path to the .mib file we want to compile :param output_dir: path to the output directory :return: True if we successfully compile the .mib to a .py """ logger.debug('Compiling mib file: %s', mib_file) # create a mib compiler with output dir mibCompiler = MibCompiler(SmiV2Parser(), PySnmpCodeGen(), PyFileWriter(output_dir)) # add default sources and mib_file's location mibCompiler.addSources(FileReader('/usr/share/mibs/ietf')) mibCompiler.addSources(FileReader('/usr/share/mibs/iana')) mibCompiler.addSources( FileReader(os.path.dirname(os.path.abspath(mib_file)))) # add searchers mibCompiler.addSearchers(PyFileSearcher(output_dir)) mibCompiler.addSearchers(PyPackageSearcher('pysnmp.mibs')) mibCompiler.addSearchers(StubSearcher(*baseMibs)) # compile, there should be a MIBFILE.py generated under output_dir mibName = os.path.basename(mib_file).replace('.mib', '') results = mibCompiler.compile(mibName) if results[mibName] == 'compiled' or results[mibName] == 'untouched': return True return False
def get_mib_symbols(name): mib_parser = InMemoryMibParser() mibCompiler = MibCompiler(SmiV2Parser(), SymtableCodeGen(), mib_parser) # Add Unity-MIB mib file to source dir mib_dir_path = os.path.join(os.path.dirname(__file__), 'mib_files') mibCompiler.addSources(FileReader(mib_dir_path)) mibCompiler.addSearchers(StubSearcher(*baseMibs)) mibCompiler.compile(name) return mib_parser.getOids(name)
def _compile_cloudify_mib(): mibs_dir = "pysnmp_mibs" if os.path.exists('{0}/{1}.py'.format(mibs_dir, CLOUDIFY_MIB)): return mib_compiler = MibCompiler(SmiV2Parser(), PySnmpCodeGen(), PyFileWriter(mibs_dir)) cloudify_mib_dir = os.path.dirname(os.path.realpath(__file__)) mib_compiler.addSources(FileReader(cloudify_mib_dir)) mib_compiler.addSources(HttpReader('mibs.snmplabs.com', 80, '/asn1/@mib@')) mib_compiler.addSearchers(PyFileSearcher(mibs_dir)) mib_compiler.compile(CLOUDIFY_MIB)
def getReadersFromUrls(*sourceUrls, **options): readers = [] for sourceUrl in sourceUrls: mibSource = urlparse.urlparse(sourceUrl) if sys.version_info[0:2] < (2, 5): class ParseResult(tuple): pass mibSource = ParseResult(mibSource) for k, v in zip( ('scheme', 'netloc', 'path', 'params', 'query', 'fragment', 'username', 'password', 'hostname', 'port'), mibSource + ('', '', '', None)): setattr(mibSource, k, v) if not mibSource.scheme or mibSource.scheme == 'file': readers.append(FileReader(mibSource.path).setOptions(**options)) elif mibSource.scheme in ('http', 'https'): readers.append( HttpReader( mibSource.hostname or mibSource.netloc, mibSource.port or 80, mibSource.path, ssl=mibSource.scheme == 'https').setOptions(**options)) elif mibSource.scheme in ('ftp', 'sftp'): readers.append( FtpReader(mibSource.hostname or mibSource.netloc, mibSource.path, ssl=mibSource.scheme == 'sftp', port=mibSource.port or 21, user=mibSource.username or 'anonymous', password=mibSource.password or 'anonymous@').setOptions(**options)) else: raise error.PySmiError('Unsupported URL scheme %s' % sourceUrl) return readers
def _get_reader_from_source(source): from pysmi.reader.localfile import FileReader if os.path.exists(source): return FileReader(source) return _get_reader_from_url(source)
from pysmi.compiler import MibCompiler from pysmi import debug #debug.setLogger(debug.Debug('reader', 'compiler')) inputMibs = ['IF-MIB', 'IP-MIB'] srcDirectories = ['/usr/share/snmp/mibs'] dstDirectory = '.pysnmp-mibs' # Initialize compiler infrastructure mibCompiler = MibCompiler( parserFactory(**smiV1Relaxed)(), PySnmpCodeGen(), PyFileWriter(dstDirectory)) # search for source MIBs here mibCompiler.addSources(*[FileReader(x) for x in srcDirectories]) # check compiled MIBs in our own productions mibCompiler.addSearchers(PyFileSearcher(dstDirectory)) # ...and at default PySNMP MIBs packages mibCompiler.addSearchers(*[PyPackageSearcher(x) for x in defaultMibPackages]) # never recompile MIBs with MACROs mibCompiler.addSearchers(StubSearcher(*baseMibs)) # run [possibly recursive] MIB compilation results = mibCompiler.compile(*inputMibs) #, rebuild=True, genTexts=True) print('Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))