Esempio n. 1
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
Esempio n. 2
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
        )
Esempio 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)
Esempio n. 4
0
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)
Esempio n. 5
0
from pysmi.codegen.pysnmp import PySnmpCodeGen, baseMibs
from pysmi.compiler import MibCompiler
from pysmi import debug

#debug.setLogger(debug.Debug('borrower', 'reader', 'searcher'))

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
])
Esempio n. 6
0
Try various filenames while searching for MIB module: %s
""" % (', '.join(sorted(mibSources)), ', '.join(
        sorted([x[0] for x in mibBorrowers if x[1] == genMibTextsFlag])),
       ', '.join(mibSearchers), dstDirectory, ', '.join(sorted(mibStubs)),
       ', '.join(sorted(inputMibs)), dstFormat, cacheDirectory
       or 'not used', nodepsFlag and 'no' or 'yes', rebuildFlag and 'yes'
       or 'no', dryrunFlag and 'yes' or 'no', pyCompileFlag and 'yes'
       or 'no', pyOptimizationLevel, ignoreErrorsFlag and 'yes'
       or 'no', buildIndexFlag and 'yes' or 'no', genMibTextsFlag and 'yes'
       or 'no', doFuzzyMatchingFlag and 'yes' or 'no'))

# Initialize compiler infrastructure

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(
Esempio n. 7
0
from pysmi import debug

#debug.setLogger(debug.Debug('all'))

inputMibs = [ 'IF-MIB', 'IP-MIB' ]
httpSources = [ 
    ('mibs.snmplabs.com', 80, '/asn1/@mib@')
]
ftpSources = [
    ('ftp.cisco.com', '/pub/mibs/v2/@mib@')
]
dstDirectory = '.pysnmp-mibs'

# Initialize compiler infrastructure

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

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

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

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

print('Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))
Esempio n. 8
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)