Ejemplo n.º 1
0
    def addMibCompiler(mibBuilder, **kwargs):
        if kwargs.get('ifNotAdded') and mibBuilder.getMibCompiler():
            return

        compiler = MibCompiler(
            parserFactory(**smiV1Relaxed)(),
            PySnmpCodeGen(),
            PyFileWriter(kwargs.get('destination') or defaultDest)
        )

        compiler.addSources(*getReadersFromUrls(*kwargs.get('sources') or defaultSources))

        compiler.addSearchers(
            StubSearcher(*baseMibs) # XXX
        )
        compiler.addSearchers(
            *[ PyPackageSearcher(x.fullPath()) for x in mibBuilder.getMibSources() ]
        )
        compiler.addBorrowers(
            *[ PyFileBorrower(x, genTexts=mibBuilder.loadTexts) for x in getReadersFromUrls(*kwargs.get('borrowers') or defaultBorrowers, **dict(lowcaseMatching=False)) ]
        )

        mibBuilder.setMibCompiler(
            compiler, kwargs.get('destination') or defaultDest
        )
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
    def run(self):
        # At this point, dependencies should already be installed, so import the compiler only now
        if os.path.isfile('check_power/NETTRACK-E3METER-SNMP-MIB.py'):
            print("File already compiled, skipping...")
        else:
            from pysmi.reader import getReadersFromUrls
            from pysmi.parser import SmiV1CompatParser
            from pysmi.compiler import MibCompiler
            from pysmi.searcher.stub import StubSearcher
            from pysmi.writer.pyfile import PyFileWriter
            from pysmi.codegen.pysnmp import PySnmpCodeGen, baseMibs
            mibSources = [
                os.path.abspath(os.path.dirname('./e3meter-ipm.mib')),
                'http://mibs.snmplabs.com/asn1/@mib@'
            ]
            mibCompiler = MibCompiler(
                SmiV1CompatParser(), PySnmpCodeGen(),
                PyFileWriter(
                    os.path.join(
                        os.path.abspath(os.path.dirname('./e3meter-ipm.mib')),
                        "check_power")))

            mibCompiler.addSources(*getReadersFromUrls(*mibSources))
            mibCompiler.addSearchers(StubSearcher(*baseMibs))

            results = mibCompiler.compile(os.path.abspath('./e3meter-ipm.mib'))
            print(results)
            build.run(self)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
inputMibs = ['BORROWED-MIB']
httpSources = [('mibs.snmplabs.com', 80, '/asn1/@mib@')]
httpBorrowers = [('mibs.snmplabs.com', 80, '/pysnmp/notexts/@mib@')]
dstDirectory = '.pysnmp-mibs'

# Initialize compiler infrastructure

mibCompiler = MibCompiler(
    parserFactory(**smiV1Relaxed)(), PySnmpCodeGen(),
    PyFileWriter(dstDirectory))

# search for source MIBs at Web sites
mibCompiler.addSources(*[HttpReader(*x) for x in httpSources])

# never recompile MIBs with MACROs
mibCompiler.addSearchers(StubSearcher(*baseMibs))

# check compiled/borrowed MIBs in our own productions
mibCompiler.addSearchers(PyFileSearcher(dstDirectory))

# search for compiled MIBs at Web sites if source is not available or broken
mibCompiler.addBorrowers(*[
    PyFileBorrower(HttpReader(*x)).setOptions(genTexts=False)
    for x in httpBorrowers
])

# run non-recursive MIB compilation
results = mibCompiler.compile(*inputMibs)

print('Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))
Ejemplo n.º 6
0
mibCompiler = MibCompiler(
    parserFactory(**smiV1Relaxed)(tempdir=cacheDirectory), NetSnmpCodeGen(),
    PyFileWriter(dstDirectory).setOptions(
        pyCompile=pyCompileFlag, pyOptimizationLevel=pyOptimizationLevel))

try:
    mibCompiler.addSources(*getReadersFromUrls(
        *mibSources, **dict(fuzzyMatching=doFuzzyMatchingFlag)))

    mibCompiler.addSearchers(PyFileSearcher(dstDirectory))

    for mibSearcher in mibSearchers:
        mibCompiler.addSearchers(PyPackageSearcher(mibSearcher))

    mibCompiler.addSearchers(StubSearcher(*mibStubs))

    mibCompiler.addBorrowers(*[
        PyFileBorrower(x[1], genTexts=mibBorrowers[x[0]][1])
        for x in enumerate(
            getReadersFromUrls(*[m[0] for m in mibBorrowers],
                               **dict(lowcaseMatching=False)))
    ])

    processed = mibCompiler.compile(
        *inputMibs,
        **dict(noDeps=nodepsFlag,
               rebuild=rebuildFlag,
               dryRun=dryrunFlag,
               genTexts=genMibTextsFlag,
               ignoreErrors=ignoreErrorsFlag))
Ejemplo n.º 7
0
def storeMib(config, mib, mibdir=None, fetchRemote=False):
    """
    A function to compile, store new mibs

    Mostly got the code from
    https://raw.githubusercontent.com/etingof/pysmi/master/scripts/mibdump.py
    """
    cacheDir = '/tmp/'
    log.debug("Collecting MIB resources")
    mibSearchers = defaultMibPackages
    log.debug("Searches")
    mibStubs = [x for x in baseMibs if x not in fakeMibs]
    log.debug("Stubs")
    mibSources = ["file://{}".format(x) for x in config["mibs"]["locations"]]
    log.debug("MIB sources from config")
    if mibdir != None:
        mibSources.append("file://{}".format(mibdir))

    log.debug("MIB source from param")
    # if "mib" is a path, add it to the sources.
    if os.path.sep in mib:
        mibSources.append(os.path.abspath(os.path.dirname(mib)))
        log.debug("MIB source from '{}'".format(mib))
    if fetchRemote:
        mibSources.append('http://mibs.snmplabs.com/asn1/@mib@')
        log.debug("Added remote mib source.")
    log.info("Using MIB sources: [{}]".format(", ".join(mibSources)))
    log.info("Using dest: {}".format(config['mibs']['compiled']))
    log.info("Initialize compiler")
    try:
        mibCompiler = MibCompiler(
            parserFactory(**smiV1Relaxed)(tempdir=cacheDir), PySnmpCodeGen(),
            PyFileWriter(config['mibs']['compiled']).setOptions(
                pyCompile=True, pyOptimizationLevel=0))
        print(mibSources)
    except Exception as e:
        log.error("Exception! {}".format(e))
    log.debug("Adding sources to compiler")
    try:
        mibCompiler.addSources(
            *getReadersFromUrls(*mibSources, **dict(fuzzyMatching=True)))
        mibCompiler.addSearchers(PyFileSearcher(config['mibs']['compiled']))
        for mibSearcher in mibSearchers:
            mibCompiler.addSearchers(PyPackageSearcher(mibSearcher))
        mibCompiler.addSearchers(StubSearcher(*mibStubs))

        log.debug("Starting compilation of {}".format(mib))
        processed = mibCompiler.compile(
            *[mib],
            **dict(noDeps=False,
                   rebuild=False,
                   dryRun=False,
                   genTexts=False,
                   ignoreErrors=False))
        mibCompiler.buildIndex(processed, dryRun=False, ignoreErrors=False)
    except smiError.PySmiError as e:
        log.error("Compilation failed: {}".format(e))
        raise exceptions.MibCompileError(e)

    errors = [(x, processed[x].error) for x in sorted(processed)
              if processed[x] == 'failed']
    compiled = [(x, processed[x].alias) for x in sorted(processed)
                if processed[x] == 'compiled']
    missing = [x for x in sorted(processed) if processed[x] == 'missing']
    for mib in compiled:
        log.info("Compiled {} ({})".format(mib[0], mib[1]))
    if len(errors) > 0 or len(missing) > 0:
        for error in errors:
            log.error("Could not process {} MIB: {}".format(
                error[0], error[1]))
        for mis in missing:
            log.error("Could not find {}".format(mis))

        raise exceptions.MibCompileFailed(errors)
    log.info("Done without errors")
    print(processed)