Beispiel #1
0
def _compile_mib_to_json(mib, source_mib_directories, destination_directory,
                         source, compiled_mibs_path):
    from pysmi.borrower import AnyFileBorrower
    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, StubSearcher
    from pysmi.writer import FileWriter

    mib_stubs = JsonCodeGen.baseMibs

    compile_documentation = True

    # Compiler infrastructure

    code_generator = JsonCodeGen()

    file_writer = FileWriter(destination_directory).setOptions(suffix='.json')

    mib_compiler = MibCompiler(SmiV1CompatParser(tempdir=''), code_generator,
                               file_writer)

    # use source_mib_directories as mibs source
    sources = [source]
    sources.extend(source_mib_directories)
    mib_compiler.addSources(
        *getReadersFromUrls(*sources, **dict(fuzzyMatching=True)))

    searchers = [
        AnyFileSearcher(destination_directory).setOptions(exts=['.json']),
        StubSearcher(*mib_stubs)
    ]
    mib_compiler.addSearchers(*searchers)

    # borrowers, aka compiled mibs source
    borrowers = [
        AnyFileBorrower(borrower_reader,
                        genTexts=True).setOptions(exts=['.json'])
        for borrower_reader in getReadersFromUrls(
            *[compiled_mibs_path], **dict(lowcaseMatching=False))
    ]
    mib_compiler.addBorrowers(borrowers)

    processed = mib_compiler.compile(
        mib,
        **dict(
            noDeps=False,
            rebuild=False,
            dryRun=False,
            dstTemplate=None,
            genTexts=compile_documentation,
            textFilter=False and (lambda symbol, text: text) or None,
            writeMibs=True,
            ignoreErrors=False,
        ))

    return processed
Beispiel #2
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))
Beispiel #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))
Beispiel #4
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)
    if not mibBorrowers:
        mibBorrowers = [('http://mibs.snmplabs.com/pysnmp/notexts/@mib@', False),
                        ('http://mibs.snmplabs.com/pysnmp/fulltexts/@mib@', True)]

    if not dstDirectory:
        dstDirectory = os.path.expanduser("~")
        if sys.platform[:3] == 'win':
            dstDirectory = os.path.join(dstDirectory, 'PySNMP Configuration', 'mibs')
        else:
            dstDirectory = os.path.join(dstDirectory, '.pysnmp', 'mibs')

    # Compiler infrastructure

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

    searchers = [PyFileSearcher(dstDirectory)]

    for mibSearcher in mibSearchers:
        searchers.append(PyPackageSearcher(mibSearcher))

    searchers.append(StubSearcher(*mibStubs))

    codeGenerator = PySnmpCodeGen()

    fileWriter = PyFileWriter(dstDirectory).setOptions(pyCompile=pyCompileFlag,
                                                       pyOptimizationLevel=pyOptimizationLevel)

elif dstFormat == 'json':
    if not mibStubs:
Beispiel #6
0
        ]

    if not dstDirectory:
        dstDirectory = os.path.expanduser("~")
        if sys.platform[:3] == 'win':
            dstDirectory = os.path.join(dstDirectory, 'PySNMP Configuration',
                                        'mibs')
        else:
            dstDirectory = os.path.join(dstDirectory, '.pysnmp', 'mibs')

    # Compiler infrastructure

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

    searchers = [PyFileSearcher(dstDirectory)]

    for mibSearcher in mibSearchers:
        searchers.append(PyPackageSearcher(mibSearcher))

    searchers.append(StubSearcher(*mibStubs))

    codeGenerator = PySnmpCodeGen()

    fileWriter = PyFileWriter(dstDirectory).setOptions(
        pyCompile=pyCompileFlag, pyOptimizationLevel=pyOptimizationLevel)

elif dstFormat == 'json':
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)))
Beispiel #8
0
    if not mibBorrowers:
        mibBorrowers = [('http://mibs.snmplabs.com/pysnmp/notexts/@mib@', False),
                        ('http://mibs.snmplabs.com/pysnmp/fulltexts/@mib@', True)]

    if not dstDirectory:
        dstDirectory = os.path.expanduser("~")
        if sys.platform[:3] == 'win':
            dstDirectory = os.path.join(dstDirectory, 'PySNMP Configuration', 'mibs')
        else:
            dstDirectory = os.path.join(dstDirectory, '.pysnmp', 'mibs')

    # Compiler infrastructure

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

    searchers = [PyFileSearcher(dstDirectory)]

    for mibSearcher in mibSearchers:
        searchers.append(PyPackageSearcher(mibSearcher))

    searchers.append(StubSearcher(*mibStubs))

    codeGenerator = PySnmpCodeGen()

    fileWriter = PyFileWriter(dstDirectory).setOptions(pyCompile=pyCompileFlag,
                                                       pyOptimizationLevel=pyOptimizationLevel)

elif dstFormat == 'json':
    if not mibStubs: