def handle_make_collection(self, mib_name, bump=False, *args, **kwargs): if len(mib_name) != 1: self.print("Specify one MIB") self.die("") # Get MIB mib = MIB.get_by_name(mib_name[0]) if not mib: self.print("MIB not found: %s" % mib_name[0]) self.die("") # Prepare MIB data mib_data = list( sorted( [{ "oid": dd.oid, "name": dd.name, "description": dd.description, "syntax": dd.syntax, } for dd in MIBData.objects.filter(mib=mib.id)] + [{ "oid": dd.oid, "name": next((a for a in dd.aliases if a.startswith(mib.name + "::"))), "description": dd.description, "syntax": dd.syntax, } for dd in MIBData.objects.filter(aliases__startswith="%s::" % mib.name)], key=lambda x: x["oid"], )) # Prepare MIB if mib.last_updated: last_updated = mib.last_updated.strftime("%Y-%m-%d") else: last_updated = "1970-01-01" version = mib.version if bump: # Bump to next version version += 1 data = { "name": mib.name, "description": mib.description, "last_updated": last_updated, "version": version, "depends_on": mib.depends_on, "typedefs": mib.typedefs, "data": mib_data, } # Serialize and write with self.open_output(kwargs.get("output")) as f: f(ujson.dumps(data))
def handle_make_cmib(self, mib_name, *args, **kwargs): if len(mib_name) != 1: self.print("Specify one MIB") self.die("") # Get MIB mib = MIB.get_by_name(mib_name[0]) if not mib: self.print("MIB not found: %s" % mib_name[0]) self.die("") # Build cmib year = datetime.date.today().year r = [ "# -*- coding: utf-8 -*-", "# ----------------------------------------------------------------------", "# %s" % mib, "# Compiled MIB", "# Do not modify this file directly", "# Run ./noc mib make-cmib instead", "# ----------------------------------------------------------------------", "# Copyright (C) 2007-%s The NOC Project" % year, "# See LICENSE for details", "# ----------------------------------------------------------------------", "", "# MIB Name", 'NAME = "%s"' % mib, "", "# Metadata", 'LAST_UPDATED = "%s"' % mib.last_updated.isoformat().split("T")[0], 'COMPILED = "%s"' % datetime.date.today().isoformat(), "", "# MIB Data: name -> oid", "MIB = {", ] r += [ ",\n".join(' "%s": "%s"' % (md.name, md.oid) for md in sorted( MIBData.objects.filter(mib=mib.id), key=lambda x: [int(y) for y in x.oid.split(".")], )) ] r[-1] += "," r += ["}", ""] data = "\n".join(r) + "\n" with self.open_output(kwargs.get("output")) as f: f(data)
def compile(self, data): """ Compile MIB, upload to database and store MIB file :param data: MIB text :return: """ if not config.path.smilint or not os.path.exists(config.path.smilint): return { "status": False, "msg": "smilint is missed", "error": ERR_MIB_TOOL_MISSED } if not config.path.smilint or not os.path.exists(config.path.smidump): return { "status": False, "msg": "smidump is missed", "error": ERR_MIB_TOOL_MISSED } # Put data to temporary file with temporary_file(data) as tmp_path: # Pass MIB through smilint to detect missed modules self.logger.debug( "Pass MIB through smilint to detect missed modules") f = subprocess.Popen([config.path.smilint, "-m", tmp_path], stderr=subprocess.PIPE, env=self.SMI_ENV).stderr for l in f: match = self.rx_module_not_found.search(l.strip()) if match: return { "status": False, "msg": "Required MIB missed: %s" % match.group(1), "code": ERR_MIB_MISSED, } self.logger.debug("Convert MIB to python module and load") # Convert MIB to python module and load with temporary_file() as py_path: subprocess.check_call( [ config.path.smidump, "-k", "-q", "-f", "python", "-o", py_path, tmp_path ], env=self.SMI_ENV, ) with open(py_path) as f: p_data = unicode(f.read(), "ascii", "ignore").encode("ascii") with open(py_path, "w") as f: f.write(p_data) m = imp.load_source("mib", py_path) # NOW we can deduce module name mib_name = m.MIB["moduleName"] # Check module dependencies depends_on = {} # MIB Name -> Object ID self.logger.debug("Check module dependencies: %s", m.MIB.get("imports", "")) if "imports" in m.MIB: for i in m.MIB["imports"]: if "module" not in i: continue rm = i["module"] if rm in depends_on: continue md = MIB.get_by_name(rm) if md is None: return { "status": False, "msg": "Required MIB missed: %s" % rm, "code": ERR_MIB_MISSED, } depends_on[rm] = md # Get MIB latest revision date try: last_updated = datetime.datetime.strptime( sorted([x["date"] for x in m.MIB[mib_name]["revisions"]])[-1], "%Y-%m-%d %H:%M") except ValueError: last_updated = datetime.datetime(year=1970, month=1, day=1) self.logger.debug("Extract MIB typedefs") # Extract MIB typedefs typedefs = {} if "typedefs" in m.MIB: for t in m.MIB["typedefs"]: typedefs[t] = MIB.parse_syntax(m.MIB["typedefs"][t]) # Check mib already uploaded mib_description = m.MIB[mib_name].get("description", None) mib = MIB.objects.filter(name=mib_name).first() if mib is not None: mib.description = mib_description mib.last_updated = last_updated mib.depends_on = sorted(depends_on) mib.typedefs = typedefs mib.save() # Delete all MIB Data mib.clean() else: # Create MIB mib = MIB( name=mib_name, description=mib_description, last_updated=last_updated, depends_on=sorted(depends_on), typedefs=typedefs, ) mib.save() # Upload MIB data cdata = [] for i in ["nodes", "notifications"]: if i in m.MIB: cdata += [{ "name": "%s::%s" % (mib_name, node), "oid": v["oid"], "description": v.get("description"), "syntax": v["syntax"]["type"] if "syntax" in v else None, } for node, v in six.iteritems(m.MIB[i])] mib.load_data(cdata) # Move file to permanent place safe_rewrite(self.get_path(mib_name), data) return {"status": True, "mib": mib_name}
def handle_make_cmib(self, mib_name, *args, **kwargs): def has_worth_hint(syntax): if not syntax: return False hint = syntax.get("display_hint") if not hint: return False base_type = syntax["base_type"] if base_type == "Integer32" and hint == "d": return False if base_type == "OctetString" and hint == "255a": return False return True if len(mib_name) != 1: self.print("Specify one MIB") self.die("") # Get MIB mib = MIB.get_by_name(mib_name[0]) if not mib: self.print("MIB not found: %s" % mib_name[0]) self.die("") # Build cmib year = datetime.date.today().year r = [ "# ----------------------------------------------------------------------", "# %s" % mib, "# Compiled MIB", "# Do not modify this file directly", "# Run ./noc mib make-cmib instead", "# ----------------------------------------------------------------------", "# Copyright (C) 2007-%s The NOC Project" % year, "# See LICENSE for details", "# ----------------------------------------------------------------------", "", "# MIB Name", 'NAME = "%s"' % mib, "", "# Metadata", 'LAST_UPDATED = "%s"' % mib.last_updated.isoformat().split("T")[0], 'COMPILED = "%s"' % datetime.date.today().isoformat(), "", "# MIB Data: name -> oid", "MIB = {", ] mib_data = list( sorted( MIBData.objects.filter(mib=mib.id), key=lambda x: [int(y) for y in x.oid.split(".")], )) r += [ "\n".join(' "%s": "%s",' % (md.name, md.oid) for md in mib_data) ] r += ["}", "", "DISPLAY_HINTS = {"] r += [ "\n".join(' "%s": ("%s", "%s"), # %s' % (md.oid, md.syntax["base_type"], md.syntax["display_hint"], md.name) for md in mib_data if has_worth_hint(md.syntax)) ] r += ["}", ""] data = "\n".join(r) with self.open_output(kwargs.get("output")) as f: f(data)