Skip to content

Commit

Permalink
Add optional MIB source url in ddev translate profile (#7835)
Browse files Browse the repository at this point in the history
* use snmp mibs copy while mibs.snmplabs.com is down

* add option to set custom MIB source in ddev translate profile
  • Loading branch information
pducolin authored Nov 27, 2020
1 parent 1abe47e commit d4c6575
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
from ...console import CONTEXT_SETTINGS


def fetch_mib(mib):
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
Expand All @@ -19,7 +24,8 @@ def fetch_mib(mib):

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

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

mibCompiler.addSources(reader)
Expand All @@ -29,8 +35,13 @@ def fetch_mib(mib):

@click.command(context_settings=CONTEXT_SETTINGS, short_help='Translate MIB name to OIDs in SNMP profiles')
@click.argument('profile_path')
@click.option(
'--mib_source_url',
default='https://raw.githubusercontent.com/projx/snmp-mibs/master/@mib@',
help='Source url to fetch missing MIBS',
)
@click.pass_context
def translate_profile(ctx, profile_path):
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
Expand All @@ -57,7 +68,7 @@ def translate_profile(ctx, profile_path):
try:
mib_view_controller.mibBuilder.loadModule(mib)
except MibNotFoundError:
fetch_mib(mib)
fetch_mib(mib, source_url=mib_source_url)
if 'table' in metric:
table = metric['table']
node = mib_view_controller.mibBuilder.importSymbols(mib, table)[0]
Expand Down

0 comments on commit d4c6575

Please sign in to comment.