Example #1
0
def getMibRevision(mibDir, mibFile):

    mibCompiler = MibCompiler(
        mibParser,
        codeGenerator,
        fileWriter
    )

    mibCompiler.addSources(
        FileReader(mibDir, recursive=False, ignoreErrors=ignoreErrorsFlag),
        *getReadersFromUrls(*mibSources)
    )

    try:
        processed = mibCompiler.compile(
            mibFile, **dict(noDeps=True, rebuild=True, fuzzyMatching=False, ignoreErrors=ignoreErrorsFlag)
        )

    except error.PySmiError:
        sys.stderr.write('ERROR: %s\r\n' % sys.exc_info()[1])
        sys.exit(EX_SOFTWARE)

    for canonicalMibName in processed:
        if (processed[canonicalMibName] == 'compiled' and
                processed[canonicalMibName].path == 'file://' + os.path.join(mibDir, mibFile)):

            try:
                revision = datetime.strptime(processed[canonicalMibName].revision, '%Y-%m-%d %H:%M')

            except Exception:
                revision = datetime.fromtimestamp(0)

            return canonicalMibName, revision

    raise error.PySmiError('Can\'t read or parse MIB "%s"' % os.path.join(mibDir, mibFile))
Example #2
0
    def addMibCompiler(mibBuilder, **kwargs):
        if kwargs.get('ifNotAdded') and mibBuilder.getMibCompiler():
            return

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

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

        compiler.addSearchers(
            StubSearcher(*baseMibs))

        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 DEFAULT_BORROWERS,
                    lowcaseMatching=False)])

        mibBuilder.setMibCompiler(
            compiler, kwargs.get('destination') or DEFAULT_DEST)
Example #3
0
def getMibRevision(mibDir, mibFile):

    mibCompiler = MibCompiler(mibParser, codeGenerator, fileWriter)

    mibCompiler.addSources(
        FileReader(mibDir, recursive=False, ignoreErrors=ignoreErrorsFlag),
        *getReadersFromUrls(*mibSources))

    try:
        processed = mibCompiler.compile(
            mibFile,
            **dict(noDeps=True,
                   rebuild=True,
                   fuzzyMatching=False,
                   ignoreErrors=ignoreErrorsFlag))

    except error.PySmiError:
        sys.stderr.write('ERROR: %s\r\n' % sys.exc_info()[1])
        sys.exit(EX_SOFTWARE)

    for canonicalMibName in processed:
        if (processed[canonicalMibName] == 'compiled'
                and processed[canonicalMibName].path
                == 'file://' + os.path.join(mibDir, mibFile)):

            try:
                revision = datetime.strptime(
                    processed[canonicalMibName].revision, '%Y-%m-%d %H:%M')

            except Exception:
                revision = datetime.fromtimestamp(0)

            return canonicalMibName, revision

    raise error.PySmiError('Can\'t read or parse MIB "%s"' %
                           os.path.join(mibDir, mibFile))
    def fetch_mib(mib):
        target_directory = os.path.dirname(pysnmp_mibs.__file__)

        reader = HttpReader('mibs.snmplabs.com', 80, '/asn1/@mib@')
        mibCompiler = MibCompiler(SmiStarParser(), PySnmpCodeGen(),
                                  PyFileWriter(target_directory))

        mibCompiler.addSources(reader)

        mibCompiler.compile(mib)
Example #5
0
def fetch_mib(mib):
    import pysnmp_mibs
    from pysmi.codegen import PySnmpCodeGen
    from pysmi.compiler import MibCompiler
    from pysmi.parser import SmiStarParser
    from pysmi.reader import HttpReader
    from pysmi.writer import PyFileWriter

    target_directory = os.path.dirname(pysnmp_mibs.__file__)

    reader = HttpReader('mibs.snmplabs.com', 80, '/asn1/@mib@')
    mibCompiler = MibCompiler(SmiStarParser(), PySnmpCodeGen(), PyFileWriter(target_directory))

    mibCompiler.addSources(reader)

    mibCompiler.compile(mib)
Example #6
0
def fetch_mib(mib, source_url):
    try:
        from urllib.parse import urlparse
    except ImportError:
        from urlparse import urlparse

    import pysnmp_mibs
    from pysmi.codegen import PySnmpCodeGen
    from pysmi.compiler import MibCompiler
    from pysmi.parser import SmiStarParser
    from pysmi.reader import HttpReader
    from pysmi.writer import PyFileWriter

    target_directory = os.path.dirname(pysnmp_mibs.__file__)

    parsed_url = urlparse(source_url)
    reader = HttpReader(parsed_url.netloc, 80, parsed_url.path)
    mibCompiler = MibCompiler(SmiStarParser(), PySnmpCodeGen(),
                              PyFileWriter(target_directory))

    mibCompiler.addSources(reader)

    mibCompiler.compile(mib)
# debug.setLogger(debug.Debug('reader', 'compiler'))

inputMibs = ['IF-MIB', 'IP-MIB']
srcDirectories = ['/usr/share/snmp/mibs']
httpSources = [('mibs.snmplabs.com', 80, '/asn1/@mib@')]


def printOut(mibName, jsonDoc, cbCtx):
    print('\n\n# MIB module %s' % mibName)
    print(jsonDoc)


# Initialize compiler infrastructure

mibCompiler = MibCompiler(SmiStarParser(), JsonCodeGen(),
                          CallbackWriter(printOut))

# search for source MIBs here
mibCompiler.addSources(*[FileReader(x) for x in srcDirectories])

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

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

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

print('\n# Results: %s' %
      ', '.join(['%s:%s' % (x, results[x]) for x in results]))
Example #8
0
import sys
from pysmi.reader import CallbackReader
from pysmi.searcher import StubSearcher
from pysmi.writer import CallbackWriter
from pysmi.parser import SmiV2Parser
from pysmi.codegen import PySnmpCodeGen
from pysmi.compiler import MibCompiler

inputMibs = ['IF-MIB', 'IP-MIB']
srcDir = '/usr/share/snmp/mibs/'  # we will read MIBs from here

# Initialize compiler infrastructure

mibCompiler = MibCompiler(
    SmiV2Parser(),
    PySnmpCodeGen(),
    # out own callback function stores results in its own way
    CallbackWriter(lambda m, d, c: sys.stdout.write(d)))

# our own callback function serves as a MIB source here
mibCompiler.addSources(
    CallbackReader(lambda m, c: open(srcDir + m + '.txt').read()))

# never recompile MIBs with MACROs
mibCompiler.addSearchers(StubSearcher(*PySnmpCodeGen.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]))
Example #9
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
Example #10
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)
Example #11
0
from pysmi.borrower.pyfile import PyFileBorrower
from pysmi.writer.pyfile import PyFileWriter
from pysmi.parser.null import NullParser
from pysmi.codegen.null import NullCodeGen
from pysmi.compiler import MibCompiler
from pysmi import debug

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

inputMibs = ['BORROWED-MIB']

httpBorrowers = [('mibs.snmplabs.com', 80, '/pysnmp/notexts/@mib@')]
dstDirectory = '.pysnmp-mibs'

# Initialize compiler infrastructure

mibCompiler = MibCompiler(NullParser(), NullCodeGen(),
                          PyFileWriter(dstDirectory))

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

# search for precompiled MIBs at Web sites
mibCompiler.addBorrowers(
    *[PyFileBorrower(HttpReader(*x)) for x in httpBorrowers])

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

print('Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))
from pysmi.searcher.stub import StubSearcher
from pysmi.writer.callback import CallbackWriter
from pysmi.parser.smi import parserFactory
from pysmi.codegen.pysnmp import PySnmpCodeGen, baseMibs
from pysmi.compiler import MibCompiler

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

inputMibs = ['IF-MIB', 'IP-MIB']
srcDir = '/usr/share/snmp/mibs/'  # we will read MIBs from here

# Initialize compiler infrastructure

mibCompiler = MibCompiler(
    parserFactory()(),
    PySnmpCodeGen(),
    # out own callback function stores results in its own way
    CallbackWriter(lambda m, d, c: sys.stdout.write(d))
)

# our own callback function serves as a MIB source here
mibCompiler.addSources(
  CallbackReader(lambda m, c: open(srcDir+m+'.txt').read())
)

# 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]))
Example #13
0
def writer(mib_name, json_doc, context):

    data = json.loads(json_doc)

    for k,v in data.items():
        DATA[v.get('oid')] = v.get('name')
        DATA[v.get('name')] = v.get('oid')

for inputMib in inputMibs:
    print(inputMib)

    try:

        mibCompiler = MibCompiler(
            SmiStarParser(), JsonCodeGen(), CallbackWriter(writer)
        )

        # search for source MIBs here
        mibCompiler.addSources(*[FileReader(x) for x in srcDirectories])

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

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

        # run recursive MIB compilation
        results = mibCompiler.compile(inputMib)

    except Exception as e:
Example #14
0
Generate texts in MIBs: %s
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])
Example #15
0
from pysmi.parser.dialect import smiV1Relaxed
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
from pysmi.parser.smi import parserFactory
from pysmi.parser.dialect import smiV1Relaxed
from pysmi.codegen.pysnmp import PySnmpCodeGen, defaultMibPackages, baseMibs
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)
#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 ])

# run non-recursive MIB compilation
Example #18
0
from pysmi.parser.smi import parserFactory
from pysmi.parser.dialect import smiV1Relaxed
from pysmi.codegen.pysnmp import PySnmpCodeGen, defaultMibPackages, baseMibs
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)
def generate_traps_db(mib_sources, output_dir, output_file, output_format,
                      no_descr, debug, mib_files):
    """Generate yaml or json formatted documents containing various information about traps. These files can be used by
    the Datadog Agent to enrich trap data.
    This command is intended for "Network Devices Monitoring" users who need to enrich traps that are not automatically
    supported by Datadog.

    The expected workflow is as such:\n
    1- Identify a type of device that is sending traps that Datadog does not already recognize.\n
    2- Fetch all the MIBs that Datadog does not support.\n
    3- Run `ddev meta snmp generate-traps-db -o ./output_dir/ /path/to/my/mib1 /path/to/my/mib2`\n

    You'll need to install pysmi manually beforehand.
    """
    from pysmi.codegen import JsonCodeGen
    from pysmi.compiler import MibCompiler
    from pysmi.parser import SmiV1CompatParser
    from pysmi.reader import getReadersFromUrls
    from pysmi.searcher import AnyFileSearcher
    from pysmi.writer import FileWriter

    if debug:
        set_debug()
        from pysmi import debug

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

    # Defaulting to github.com/DataDog/mibs.snmplabs.com/
    mib_sources = [mib_sources] if mib_sources else [MIB_SOURCE_URL]

    if output_file:
        allowed_extensions = ALLOWED_EXTENSIONS_BY_FORMAT[output_format]
        if not any(output_file.endswith(x) for x in allowed_extensions):
            abort("Output file {} does not end with an allowed extension '{}'".
                  format(output_file, ", ".join(allowed_extensions)))

    if output_dir and output_file:
        abort(
            "Do not set both --output-dir and --output-file at the same time.")
    elif not output_file and not output_dir:
        abort("Need to set one of --output-dir or --output-file")

    with TempDir('ddev_mibs') as compiled_mibs_sources:
        compiled_mibs_sources = os.path.abspath(compiled_mibs_sources)
        echo_info("Writing intermediate compiled MIBs to {}".format(
            compiled_mibs_sources))

        mibs_sources_dir = os.path.join(compiled_mibs_sources, 'mibs_sources')
        if not os.path.isdir(mibs_sources_dir):
            os.mkdir(mibs_sources_dir)

        mib_sources = (sorted(
            set([
                os.path.abspath(os.path.dirname(x))
                for x in mib_files if os.path.sep in x
            ])) + mib_sources)

        mib_files = [os.path.basename(x) for x in mib_files]
        searchers = [
            AnyFileSearcher(compiled_mibs_sources).setOptions(exts=['.json'])
        ]
        code_generator = JsonCodeGen()
        file_writer = FileWriter(compiled_mibs_sources).setOptions(
            suffix='.json')
        mib_compiler = MibCompiler(SmiV1CompatParser(tempdir=''),
                                   code_generator, file_writer)
        mib_compiler.addSources(
            *getReadersFromUrls(*mib_sources, **dict(fuzzyMatching=True)))
        mib_compiler.addSearchers(*searchers)

        compiled_mibs, compiled_dependencies_mibs = compile_and_report_status(
            mib_files, mib_compiler)

        # Move all the parent MIBs that had to be compiled but were not requested in the command to a subfolder.
        for mib_file_name in compiled_dependencies_mibs:
            os.replace(
                os.path.join(compiled_mibs_sources, mib_file_name + '.json'),
                os.path.join(mibs_sources_dir, mib_file_name + '.json'),
            )

        # Only generate trap_db with `mib_files` unless explicitly asked. Used to ignore other files that may be
        # present "compiled_mibs_sources"
        compiled_mibs = [
            os.path.join(compiled_mibs_sources, x + '.json')
            for x in compiled_mibs
        ]

        # Generate the trap database based on the compiled MIBs.
        trap_db_per_mib = generate_trap_db(compiled_mibs, mibs_sources_dir,
                                           no_descr)

        use_json = output_format == "json"
        if output_file:
            # Compact representation, only one file
            write_compact_trap_db(trap_db_per_mib,
                                  output_file,
                                  use_json=use_json)
            echo_success("Wrote trap data to {}".format(
                os.path.abspath(output_file)))
        else:
            # Expanded representation, one file per MIB.
            write_trap_db_per_mib(trap_db_per_mib,
                                  output_dir,
                                  use_json=use_json)
            echo_success("Wrote trap data to {}".format(
                os.path.abspath(output_dir)))
inputMibs = ['IF-MIB', 'IP-MIB']
srcDirectories = ['/usr/share/snmp/mibs']
httpSources = [
    ('mibs.snmplabs.com', 80, '/asn1/@mib@')
]


def printOut(mibName, jsonDoc, cbCtx):
    print('\n\n# MIB module %s' % mibName)
    print(jsonDoc)

# Initialize compiler infrastructure

mibCompiler = MibCompiler(
    SmiStarParser(), JsonCodeGen(), CallbackWriter(printOut)
)

# search for source MIBs here
mibCompiler.addSources(*[FileReader(x) for x in srcDirectories])

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

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

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

print('\n# Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))
Example #21
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]))
Example #22
0
       rebuildFlag and 'yes' or 'no',
       dryrunFlag and 'yes' or 'no',
       writeMibsFlag and 'yes' or 'no',
       dstFormat == 'pysnmp' and pyCompileFlag and 'yes' or 'no',
       dstFormat == 'pysnmp' and pyOptimizationLevel and 'yes' or 'no',
       ignoreErrorsFlag and 'yes' or 'no',
       buildIndexFlag and 'yes' or 'no',
       genMibTextsFlag and 'yes' or 'no',
       keepTextsLayout and 'yes' or 'no',
       doFuzzyMatchingFlag and 'yes' or 'no'))

# Initialize compiler infrastructure

mibCompiler = MibCompiler(
    SmiV1CompatParser(tempdir=cacheDirectory),
    codeGenerator,
    fileWriter
)

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

    mibCompiler.addSearchers(*searchers)

    mibCompiler.addBorrowers(*borrowers)

    processed = mibCompiler.compile(
from pysmi.reader import HttpReader
from pysmi.searcher import StubSearcher
from pysmi.writer import CallbackWriter
from pysmi.parser import SmiStarParser
from pysmi.codegen import JsonCodeGen
from pysmi.compiler import MibCompiler

inputMibs = ['IF-MIB', 'IP-MIB']

httpSources = [('mibs.snmplabs.com', 80, '/asn1/@mib@')]


# store compiled MIBs by calling this function
def store_mibs(mibName, jsonDoc, cbCtx):
    print('# MIB module %s' % mibName)
    print(jsonDoc)


mibCompiler = MibCompiler(SmiStarParser(), JsonCodeGen(),
                          CallbackWriter(store_mibs))

# pull ASN.1 MIBs over HTTP
mibCompiler.addSources(*[HttpReader(*x) for x in httpSources])

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

status = mibCompiler.compile(*inputMibs)

print(status)
def mib2pysnmp(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 sources from where we fetch dependencies
    mibCompiler.addSources(HttpReader('mibs.snmplabs.com', 80, '/asn1/@mib@'))
    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
from pysmi.reader.httpclient import HttpReader
from pysmi.searcher.pyfile import PyFileSearcher
from pysmi.borrower.pyfile import PyFileBorrower
from pysmi.writer.pyfile import PyFileWriter
from pysmi.parser.null import NullParser
from pysmi.codegen.null import NullCodeGen
from pysmi.compiler import MibCompiler
from pysmi import debug

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

inputMibs = ["BORROWED-MIB"]

httpBorrowers = [("mibs.snmplabs.com", 80, "/pysnmp/notexts/@mib@")]
dstDirectory = ".pysnmp-mibs"

# Initialize compiler infrastructure

mibCompiler = MibCompiler(NullParser(), NullCodeGen(), PyFileWriter(dstDirectory))

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

# search for precompiled MIBs at Web sites
mibCompiler.addBorrowers(*[PyFileBorrower(HttpReader(*x)) for x in httpBorrowers])

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

print("Results: %s" % ", ".join(["%s:%s" % (x, results[x]) for x in results]))
"""

from pysmi.reader import FileReader
from pysmi.searcher import PyFileSearcher, PyPackageSearcher, StubSearcher
from pysmi.writer import PyFileWriter
from pysmi.parser import SmiStarParser
from pysmi.codegen import PySnmpCodeGen
from pysmi.compiler import MibCompiler

inputMibs = ['MIKROTIK-MIB']
srcDirectories = ['.mikrotik-mibs']
dstDirectory = '.pysnmp-mibs'

# Initialize compiler infrastructure

mibCompiler = MibCompiler(SmiStarParser(), 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 PySnmpCodeGen.defaultMibPackages])

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

# run [possibly recursive] MIB compilation
results = mibCompiler.compile(*inputMibs)  # , rebuild=True, genTexts=True)
       rebuildFlag and 'yes' or 'no',
       dryrunFlag and 'yes' or 'no',
       writeMibsFlag and 'yes' or 'no',
       dstFormat == 'pysnmp' and pyCompileFlag and 'yes' or 'no',
       dstFormat == 'pysnmp' and pyOptimizationLevel and 'yes' or 'no',
       ignoreErrorsFlag and 'yes' or 'no',
       buildIndexFlag and 'yes' or 'no',
       genMibTextsFlag and 'yes' or 'no',
       keepTextsLayout and 'yes' or 'no',
       doFuzzyMatchingFlag and 'yes' or 'no'))

# Initialize compiler infrastructure

mibCompiler = MibCompiler(
    SmiV1CompatParser(tempdir=cacheDirectory),
    codeGenerator,
    fileWriter
)

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

    mibCompiler.addSearchers(*searchers)

    mibCompiler.addBorrowers(*borrowers)

    processed = mibCompiler.compile(
Example #28
0
from pysmi.reader import FtpReader
from pysmi.reader import FileReader
from pysmi.searcher import StubSearcher
from pysmi.writer import PyFileWriter
from pysmi.parser import SmiStarParser
from pysmi.codegen import PySnmpCodeGen
from pysmi.compiler import MibCompiler

inputMibs = ['IF-MIB', 'BRIDGE-MIB', 'LUXL-POE-MIB', 'LUXL-SMI', 'LUXL-TC']
httpSources = [('mibs.snmplabs.com', 80, '/asn1/@mib@')]
ftpSources = [('ftp.cisco.com', '/pub/mibs/v2/@mib@')]
srcDirectories = [('./LUXL_MIBs_ALL')]
dstDirectory = './lib/python3.7/site-packages/pysnmp/smi/mibs/'

# Initialize compiler infrastructure

mibCompiler = MibCompiler(SmiStarParser(), 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])
mibCompiler.addSources(*[FileReader(x) for x in srcDirectories])

# never recompile MIBs with MACROs
mibCompiler.addSearchers(StubSearcher(*PySnmpCodeGen.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]))
Example #29
0
       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))
Example #30
0
       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
writer = CFileWriter(dstDirectory)

mibCompiler = MibCompiler(parserFactory(**smiV1Relaxed)(tempdir=cacheDirectory),
    NetSnmpCodeGen(writer,**dict(mappingFile=mappingFile,
                                 clangFormatPath=clangFormatPath,
                                 jsonTables=jsonTables,scalarMapping=scalarMapping)),
    writer)

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))) ])