(ctx, profile_path, mib_source_url)
Do OID translation in a SNMP profile. This isn't a plain replacement, as it
    doesn't preserve comments and indent, but it should automate most of the
    work.

    You'll need to install pysnmp and pysnmp-mibs manually beforehand.
Do OID translation in a SNMP profile. This isn't a plain replacement, as it
    doesn't preserve comments and indent, but it should automate most of the
    work.
Do OID translation in a SNMP profile . This isn t a plain replacement as it doesn t preserve comments and indent but it should automate most of the work .
def translate_profile(ctx, profile_path, mib_source_url):
    """
    Do OID translation in a SNMP profile. This isn't a plain replacement, as it
    doesn't preserve comments and indent, but it should automate most of the
    work.

    You'll need to install pysnmp and pysnmp-mibs manually beforehand.
    """
    # Leave imports in function to not add the dependencies
    from pysnmp import hlapi
    from pysnmp.smi import view
    from pysnmp.smi.error import MibNotFoundError

    snmp_engine = hlapi.SnmpEngine()
    mib_builder = snmp_engine.getMibBuilder()

    mib_view_controller = view.MibViewController(mib_builder)

    with open(profile_path) as f:
        data = yaml.safe_load(f.read())

    output = []
    metrics = data.get('metrics', [])
    for metric in metrics:
        mib = metric['MIB']
        try:
            mib_view_controller.mibBuilder.loadModule(mib)
        except MibNotFoundError:
            fetch_mib(mib, source_url=mib_source_url)
        if 'table' in metric:
            table = metric['table']
            if not isinstance(table, str):
                continue
            node = mib_view_controller.mibBuilder.importSymbols(mib, table)[0]
            value = '.'.join([str(i) for i in node.getName()])
            table = {'name': table, 'OID': value}
            symbols = []
            for symbol in metric['symbols']:
                if not isinstance(symbol, str):
                    continue
                node = mib_view_controller.mibBuilder.importSymbols(mib, symbol)[0]
                value = '.'.join([str(i) for i in node.getName()])
                symbols.append({'name': symbol, 'OID': value})
            tags = []
            for tag in metric['metric_tags']:
                if 'column' in tag:
                    tag_mib = tag.get('MIB', mib)
                    key = tag['column']
                    if not isinstance(key, str):
                        continue
                    node = mib_view_controller.mibBuilder.importSymbols(tag_mib, key)[0]
                    value = '.'.join([str(i) for i in node.getName()])
                    tag = tag.copy()
                    tag['column'] = {'name': key, 'OID': value}
                    tags.append(tag)
                else:
                    tags.append(tag)
            element = {'MIB': mib, 'table': table, 'symbols': symbols, 'metric_tags': tags}
            if 'forced_type' in metric:
                element['forced_type'] = metric['forced_type']
            output.append(element)

        elif 'symbol' in metric:
            key = metric['symbol']

            node = mib_view_controller.mibBuilder.importSymbols(mib, key)[0]
            value = '.'.join([str(i) for i in node.getName()])
            element = {'MIB': mib, 'symbol': {'name': key, 'OID': value}}
            if 'forced_type' in metric:
                element['forced_type'] = metric['forced_type']
            output.append(element)
    print(yaml.dump({'metrics': output}))
def translate_profile ( ctx , profile_path , mib_source_url ) : # Leave imports in function to not add the dependencies from pysnmp import hlapi from pysnmp . smi import view from pysnmp . smi . error import MibNotFoundError snmp_engine = hlapi . SnmpEngine ( ) mib_builder = snmp_engine . getMibBuilder ( ) mib_view_controller = view . MibViewController ( mib_builder ) with open ( profile_path ) as f : data = yaml . safe_load ( f . read ( ) ) output = [ ] metrics = data . get ( 'metrics' , [ ] ) for metric in metrics : mib = metric [ 'MIB' ] try : mib_view_controller . mibBuilder . loadModule ( mib ) except MibNotFoundError : fetch_mib ( mib , source_url = mib_source_url ) if 'table' in metric : table = metric [ 'table' ] if not isinstance ( table , str ) : continue node = mib_view_controller . mibBuilder . importSymbols ( mib , table ) [ 0 ] value = '.' . join ( [ str ( i ) for i in node . getName ( ) ] ) table = { 'name' : table , 'OID' : value } symbols = [ ] for symbol in metric [ 'symbols' ] : if not isinstance ( symbol , str ) : continue node = mib_view_controller . mibBuilder . importSymbols ( mib , symbol ) [ 0 ] value = '.' . join ( [ str ( i ) for i in node . getName ( ) ] ) symbols . append ( { 'name' : symbol , 'OID' : value } ) tags = [ ] for tag in metric [ 'metric_tags' ] : if 'column' in tag : tag_mib = tag . get ( 'MIB' , mib ) key = tag [ 'column' ] if not isinstance ( key , str ) : continue node = mib_view_controller . mibBuilder . importSymbols ( tag_mib , key ) [ 0 ] value = '.' . join ( [ str ( i ) for i in node . getName ( ) ] ) tag = tag . copy ( ) tag [ 'column' ] = { 'name' : key , 'OID' : value } tags . append ( tag ) else : tags . append ( tag ) element = { 'MIB' : mib , 'table' : table , 'symbols' : symbols , 'metric_tags' : tags } if 'forced_type' in metric : element [ 'forced_type' ] = metric [ 'forced_type' ] output . append ( element ) elif 'symbol' in metric : key = metric [ 'symbol' ] node = mib_view_controller . mibBuilder . importSymbols ( mib , key ) [ 0 ] value = '.' . join ( [ str ( i ) for i in node . getName ( ) ] ) element = { 'MIB' : mib , 'symbol' : { 'name' : key , 'OID' : value } } if 'forced_type' in metric : element [ 'forced_type' ] = metric [ 'forced_type' ] output . append ( element ) print ( yaml . dump ( { 'metrics' : output } ) )