def interface_classification(self, iface): """ Perform interface classification :param iface: Interface instance :return: """ if iface.profile_locked: return try: p_id = self.get_interface_profile(iface) except NotImplementedError: self.logger.error("Uses not implemented rule") return if p_id and p_id != iface.profile.id: # Change profile profile = InterfaceProfile.get_by_id(p_id) if not profile: self.logger.error( "Invalid interface profile '%s' for interface '%s'. " "Skipping", p_id, iface.name, ) return elif profile != iface.profile: self.logger.info("Interface %s has been classified as '%s'", iface.name, profile.name) iface.profile = profile iface.save()
def _get_interface(iface, subs, links, ifcache): r = { "name": qs(iface["name"]), "type": iface["type"], "description": qs(iface.get("description")), "enabled_protocols": iface.get("enabled_protocols") or [], "admin_status": iface.get("admin_status", False), } if iface.get("ifindex"): r["snmp_ifindex"] = iface["ifindex"] if iface.get("mac"): r["mac"] = iface["mac"] if iface.get("aggregated_interface"): r["aggregated_interface"] = ifcache[ iface["aggregated_interface"]][-1] # Apply profile if iface.get("profile"): profile = InterfaceProfile.get_by_id(iface["profile"]) r["profile"] = ManagedObjectDataStream._get_interface_profile( profile) # Apply subinterfaces r["subinterfaces"] = [ ManagedObjectDataStream._get_subinterface(s) for s in sorted(subs, key=lambda x: alnum_key(x["name"])) ] # Apply links if links: r["link"] = ManagedObjectDataStream._get_link( iface, links, ifcache) return r
def handle_apply(self, moo, *args, **kwargs): # sol = config.get("interface_discovery", "get_interface_profile") # @todo Classification pyrule get_profile = None if not get_profile: get_profile = InterfaceClassificationRule get_profile = get_profile.get_classificator() # raise CommandError("No classification solution") pcache = {} for o in self.get_objects(moo): self.stdout.write( "%s (%s):\n" % (o.name, o.platform.name if o.platform else o.profile.name)) ifaces = self.get_interfaces(o) if not ifaces: self.stdout.write("No ifaces on object\n") continue tps = self.get_interface_template(ifaces) for i in ifaces: if not i.profile or not i.profile_locked: pn = get_profile(i) if pn: p = pcache.get(pn) if not p: p = InterfaceProfile.get_by_id(pn) pcache[pn] = p i.profile = p i.save() v = "Set %s" % p.name else: v = "Not matched" self.show_interface(tps, i, v)
def get_interface_profile_metrics(cls, p_id): r = {} ipr = InterfaceProfile.get_by_id(id=p_id) if not ipr: return r for m in ipr.metrics: r[m.metric_type.name] = cls.config_from_settings(m) return r
def get_interface_metrics(self): """ Populate metrics list with interface metrics :return: """ subs = None metrics = [] for i in (Interface._get_collection().with_options( read_preference=ReadPreference.SECONDARY_PREFERRED).find( { "managed_object": self.object.id, "type": "physical" }, { "_id": 1, "name": 1, "ifindex": 1, "profile": 1 }, )): ipr = self.get_interface_profile_metrics(i["profile"]) self.logger.debug("Interface %s. ipr=%s", i["name"], ipr) if not ipr: continue # No metrics configured i_profile = InterfaceProfile.get_by_id(i["profile"]) if i_profile.allow_subinterface_metrics and subs is None: # Resolve subinterfaces subs = self.get_subinterfaces() ifindex = i.get("ifindex") for metric in ipr: if (self.is_box and not ipr[metric].enable_box) or ( self.is_periodic and not ipr[metric].enable_periodic): continue m_id = next(self.id_count) m = { "id": m_id, "metric": metric, "path": ["", "", "", i["name"]] } if ifindex is not None: m["ifindex"] = ifindex metrics += [m] self.id_metrics[m_id] = ipr[metric] if i_profile.allow_subinterface_metrics: for si in subs[i["_id"]]: m_id = next(self.id_count) m = { "id": m_id, "metric": metric, "path": ["", "", "", i["name"], si["name"]], } if si["ifindex"] is not None: m["ifindex"] = si["ifindex"] metrics += [m] self.id_metrics[m_id] = ipr[metric] if not metrics: self.logger.info("Interface metrics are not configured. Skipping") return metrics
def get_interface_metrics(self): """ Populate metrics list with interface metrics :return: """ # @todo: Inject Interface.effective_labels subs = None metrics = [] for i in ( Interface._get_collection() .with_options(read_preference=ReadPreference.SECONDARY_PREFERRED) .find( {"managed_object": self.object.id, "type": "physical"}, { "_id": 1, "name": 1, "ifindex": 1, "profile": 1, "in_speed": 1, "out_speed": 1, "bandwidth": 1, }, ) ): ipr = self.get_interface_profile_metrics(i["profile"]) self.logger.debug("Interface %s. ipr=%s", i["name"], ipr) if not ipr: continue # No metrics configured i_profile = InterfaceProfile.get_by_id(i["profile"]) if i_profile.allow_subinterface_metrics and subs is None: # Resolve subinterfaces subs = self.get_subinterfaces() ifindex = i.get("ifindex") for metric in ipr: if (self.is_box and not ipr[metric].enable_box) or ( self.is_periodic and not ipr[metric].enable_periodic ): continue m_id = next(self.id_count) m = {"id": m_id, "metric": metric, "labels": [f"noc::interface::{i['name']}"]} if ifindex is not None: m["ifindex"] = ifindex metrics += [m] self.id_metrics[m_id] = ipr[metric] if i_profile.allow_subinterface_metrics: for si in subs[i["_id"]]: if si["name"] != i["name"]: m_id = next(self.id_count) m = { "id": m_id, "metric": metric, "labels": [ f"noc::interface::{i['name']}", f"noc::subinterface::{si['name']}", ], } if si["ifindex"] is not None: m["ifindex"] = si["ifindex"] metrics += [m] self.id_metrics[m_id] = ipr[metric] threshold_profile = ipr[metric].threshold_profile if threshold_profile and threshold_profile.value_handler: # Fill window context in_speed: int = i.get("in_speed") or 0 out_speed: int = i.get("out_speed") or 0 bandwidth: int = i.get("bandwidth") or 0 if in_speed and not out_speed: out_speed = in_speed elif not in_speed and out_speed: in_speed = out_speed if not bandwidth: bandwidth = max(in_speed, out_speed) self.id_ctx[m_id] = { "in_speed": in_speed, "out_speed": out_speed, "bandwidth": bandwidth, } if not metrics: self.logger.info("Interface metrics are not configured. Skipping") return metrics