Пример #1
0
    def __init__(self, mib_list, src_dir_list, http_sources=None):
        if "BFT_DEBUG" in os.environ:
            self.dbg = os.environ.get('BFT_DEBUG')
        else:
            self.dbg = ""

        if "yy" in self.dbg:
            # VERY verbose, but essential for spotting
            # possible  ASN.1 errors
            from pysmi import debug
            debug.setLogger(debug.Debug('reader', 'compiler'))

        # Initialize compiler infrastructure
        mibCompiler = MibCompiler(SmiStarParser(), JsonCodeGen(),
                                  CallbackWriter(self.callback_func))

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

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

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

        # run recursive MIB compilation
        mibCompiler.compile(*mib_list)
Пример #2
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)
Пример #3
0
    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)
Пример #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)
Пример #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)
Пример #6
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)
Пример #7
0
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
Пример #8
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))
Пример #9
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))
Пример #10
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
Пример #11
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
Пример #12
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)
Пример #13
0
def _compile_mib_to_json(mib, source_mib_directories, destination_directory):
    from pysmi.codegen import JsonCodeGen
    from pysmi.compiler import MibCompiler
    from pysmi.parser import SmiV1CompatParser
    from pysmi.reader import FileReader, HttpReader
    from pysmi.searcher import AnyFileSearcher, StubSearcher
    from pysmi.writer import FileWriter

    mib_stubs = JsonCodeGen.baseMibs

    compile_documentation = True

    # Compiler infrastructure
    searchers = [
        AnyFileSearcher(destination_directory).setOptions(exts=['.json']),
        StubSearcher(*mib_stubs)
    ]

    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
    for source_directory in source_mib_directories:
        mib_compiler.addSources(FileReader(source_directory))
    # use snmp mibs repo as mibs source
    reader = HttpReader('raw.githubusercontent.com', 80,
                        '/projx/snmp-mibs/master/@mib@')
    mib_compiler.addSources(reader)

    mib_compiler.addSearchers(*searchers)

    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
Пример #14
0
    def __init__(self, mib_list, src_dir_list, http_sources=None):
        if "BFT_DEBUG" in os.environ:
            self.dbg = os.environ.get('BFT_DEBUG')
        else:
            self.dbg = ""

        if "yy" in self.dbg:
            # VERY verbose, but essential for spotting
            # possible  ASN.1 errors
            from pysmi import debug
            debug.setLogger(debug.Debug('reader', 'compiler'))

        # Initialize compiler infrastructure
        mibCompiler = MibCompiler(SmiStarParser(), JsonCodeGen(),
                                  CallbackWriter(self.callback_func))

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

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

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

        # run recursive MIB compilation
        mib_dict = mibCompiler.compile(*mib_list)

        err = False

        if mib_dict is None or mib_dict == {}:
            print(
                "ERROR: failed on mib compilation (mibCompiler.compile returned an empty dictionary)"
            )
            err = True

        for key, value in mib_dict.iteritems():
            if value == 'unprocessed':
                print("ERROR: failed on mib compilation: " + key + ": " +
                      value)
                err = True

        if err:
            raise Exception("SnmpMibs failed to initialize.")
        elif 'BFT_DEBUG' in os.environ:
            print('# %d MIB modules compiled' % len(mib_dict))
Пример #15
0
 def transform_mibs_to_json(self,
                            specific_mibs: object = None,
                            exclude_mibs: object = []) -> object:
     mib_names = []
     if specific_mibs is None:
         for filename in os.listdir(self.local_mib_dir):
             try:
                 filename.rindex('.my', -3)
             except:
                 continue
             mib_names.append(filename)
     else:
         mib_names = specific_mibs.copy()
     logging.info('Compiling MIBs to JSON.')
     try:
         mib_compiler = MibCompiler(
             SmiV1CompatParser(), JsonCodeGen(),
             FileWriter(self.local_json_dir).setOptions(suffix='.json'))
         mib_compiler.addSources(
             FileReader(self.local_mib_dir, recursive=True))
         mib_stubs = JsonCodeGen.baseMibs
         searchers = [
             AnyFileSearcher(
                 self.local_json_dir).setOptions(exts=['.json']),
             StubSearcher(*mib_stubs)
         ]
         mib_compiler.addSearchers(*searchers)
         if not os.path.isdir(self.local_json_dir):
             os.makedirs(self.local_json_dir)
         processed = mib_compiler.compile(
             *mib_names,
             **dict(noDeps=False,
                    rebuild=True,
                    genTexts=True,
                    writeMibs=True,
                    ignoreErrors=True))
         mib_compiler.buildIndex(processed, ignoreErrors=True)
     except error.PySmiError:
         logging.error('ERROR: %s', str(sys.exc_info()[1]))
         sys.exit(1)
     logging.info('Finished compiling MIBs to JSON.')
Пример #16
0
try:
    mibCompiler.addSources(
        *getReadersFromUrls(
            *mibSources, **dict(fuzzyMatching=doFuzzyMatchingFlag)
        )
    )

    mibCompiler.addSearchers(*searchers)

    mibCompiler.addBorrowers(*borrowers)

    processed = mibCompiler.compile(
        *inputMibs, **dict(noDeps=nodepsFlag,
                           rebuild=rebuildFlag,
                           dryRun=dryrunFlag,
                           dstTemplate=dstTemplate,
                           genTexts=genMibTextsFlag,
                           textFilter=keepTextsLayout and (lambda symbol, text: text) or None,
                           writeMibs=writeMibsFlag,
                           ignoreErrors=ignoreErrorsFlag)
    )

    if buildIndexFlag:
        mibCompiler.buildIndex(
            processed,
            dryRun=dryrunFlag,
            ignoreErrors=ignoreErrorsFlag
        )

except error.PySmiError:
    sys.stderr.write('ERROR: %s\r\n' % sys.exc_info()[1])
    sys.exit(EX_SOFTWARE)
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]))
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)
Пример #19
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]))
Пример #20
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]))
Пример #21
0
    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))

    if buildIndexFlag:
        mibCompiler.buildIndex(processed,
                               dryRun=dryrunFlag,
                               ignoreErrors=ignoreErrorsFlag)

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

else:
    if verboseFlag:
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.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]))
Пример #24
0
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]))
try:
    mibCompiler.addSources(
        *getReadersFromUrls(
            *mibSources, **dict(fuzzyMatching=doFuzzyMatchingFlag)
        )
    )

    mibCompiler.addSearchers(*searchers)

    mibCompiler.addBorrowers(*borrowers)

    processed = mibCompiler.compile(
        *inputMibs, **dict(noDeps=nodepsFlag,
                           rebuild=rebuildFlag,
                           dryRun=dryrunFlag,
                           genTexts=genMibTextsFlag,
                           textFilter=keepTextsLayout and (lambda symbol, text: text) or None,
                           writeMibs=writeMibsFlag,
                           ignoreErrors=ignoreErrorsFlag)
    )

    if buildIndexFlag:
        mibCompiler.buildIndex(
            processed,
            dryRun=dryrunFlag,
            ignoreErrors=ignoreErrorsFlag
        )

except error.PySmiError:
    sys.stderr.write('ERROR: %s\r\n' % sys.exc_info()[1])
    sys.exit(EX_SOFTWARE)
Пример #26
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)
Пример #27
0
    )

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

    if buildIndexFlag:
        mibCompiler.buildIndex(
            processed,
            dryRun=dryrunFlag,
            ignoreErrors=ignoreErrorsFlag
        )

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

else:
Пример #28
0
    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:
        print(e)

with open('/tmp/mibs.json', 'wb') as fp:
    json.dump(DATA, fp)