Пример #1
0
 def handle_apply_confdb(self, moo, *args, **kwargs):
     default_profile = InterfaceProfile.get_default_profile()
     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))
         ifmap = {i.name: i for i in self.get_interfaces(o)}
         if not ifmap:
             self.stdout.write("No ifaces on object\n")
             continue
         tps = self.get_interface_template(ifmap.values())
         proccessed = set()
         selectors_skipping = set()  # if selectors has not match
         cdb = o.get_confdb()
         ifprofilemap = {}
         for icr in InterfaceClassificationRule.objects.filter(
                 is_active=True).order_by("order"):
             if icr.selector.id in selectors_skipping:
                 continue
             r = next(cdb.query(icr.selector.get_confdb_query), None)
             if r is None:
                 # Selectors already fail check
                 selectors_skipping.add(icr.selector.id)
                 continue
             self.print("[%s] Check selector" % icr)
             for match in cdb.query(icr.get_confdb_query):
                 if match["ifname"] in proccessed or match[
                         "ifname"] not in ifmap:
                     continue
                 self.print("[%s] Match %s" % (icr, match["ifname"]))
                 iface = ifmap[match["ifname"]]
                 proccessed.add(match["ifname"])
                 if iface.profile_locked:
                     continue
                 ifprofilemap[iface.name] = icr.profile
         # Set profile
         for ifname in ifmap:
             i = ifmap[ifname]
             if ifname in ifprofilemap and i.profile.id != ifprofilemap[
                     ifname].id:
                 i.profile = ifprofilemap[ifname]
                 i.save()
                 v = "Set %s" % ifprofilemap[ifname].name
             elif ifname in ifprofilemap and i.profile.id == ifprofilemap[
                     ifname].id:
                 v = "Already set %s" % ifprofilemap[ifname].name
             else:
                 v = "Not matched"
                 if kwargs.get(
                         "reset_default") and i.profile != default_profile:
                     i.profile = default_profile
                     i.save()
                     v = "Not matched. Reset to default"
             self.show_interface(tps, i, v)
Пример #2
0
 def handle_reset(self, moo, *args, **kwargs):
     for o in self.get_objects(moo):
         self.stdout.write(
             "%s (%s):\n" %
             (o.name,
              (o.platform.name if o.platform else None) or o.profile.name))
         for i in Interface.objects.filter(managed_object=o.id):
             if i.profile:
                 self.stdout.write(
                     "    resetting profile on %s to default\n" % i.name)
                 i.profile = InterfaceProfile.get_default_profile()
                 i.save()
Пример #3
0
 def handle_apply(self, moo, *args, **kwargs):
     # sol = config.get("interface_discovery", "get_interface_profile")
     # @todo Classification pyrule
     default_profile = InterfaceProfile.get_default_profile()
     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"
                     if kwargs.get("reset_default"
                                   ) and i.profile != default_profile:
                         i.profile = default_profile
                         i.save()
                         v = "Not matched. Reset to default"
                 self.show_interface(tps, i, v)
Пример #4
0
 def handler(self):
     self.logger.info("NRI Service Mapper")
     if not self.object.remote_system:
         self.logger.info(
             "Created directly. No NRI integration. Skipping check")
         return
     if not self.object.remote_system.enable_service:
         self.logger.info(
             "NRI does not provide link information. Skipping check")
         return
     # Check object has interfaces
     if not self.has_capability("DB | Interfaces"):
         self.logger.info(
             "No interfaces discovered. Skipping interface status check")
         return
     # Get services related to Managed object
     scol = Service._get_collection()
     slist = [
         s for s in scol.find(
             {
                 "managed_object": self.object.id,
                 "nri_port": {
                     "$exists": True
                 }
             },
             {
                 "_id": 1,
                 "nri_port": 1,
                 "profile": 1
             },
         )
     ]
     # nri_port -> service_id
     smap = {s["nri_port"]: s["_id"] for s in slist}
     # service id -> service profile
     prof_map = {
         s["_id"]: ServiceProfile.get_by_id(s["profile"])
         for s in slist
     }
     icol = Interface._get_collection()
     nmap = {}
     bulk = []
     for i in icol.find({
             "managed_object": self.object.id,
             "nri_name": {
                 "$exists": True
             }
     }):
         if not i.get("nri_name"):
             continue
         if i["nri_name"] in smap:
             svc = smap[i["nri_name"]]
             p = prof_map.get(svc)
             if svc != i.get("service"):
                 self.logger.info("Binding service %s to interface %s", svc,
                                  i["name"])
                 op = {"service": svc}
                 if p and p.interface_profile:
                     op["profile"] = p.interface_profile.id
                 bulk += [UpdateOne({"_id": i["_id"]}, {"$set": op})]
             elif p and p.interface_profile and p.interface_profile.id != i[
                     "profile"]:
                 self.logger.info("Replace profile to %s on intertace %s",
                                  p.interface_profile, i["name"])
                 bulk += [
                     UpdateOne(
                         {"_id": i["_id"]},
                         {"$set": {
                             "profile": p.interface_profile.id
                         }})
                 ]
             del smap[i["nri_name"]]
         elif i.get("service"):
             self.logger.info("Removing service %s from interface %s",
                              i["service"], i["name"])
             op = {"$unset": {"service": ""}}
             if i["service"] in prof_map:
                 op["$set"] = {
                     "profile": InterfaceProfile.get_default_profile().id
                 }
             bulk += [UpdateOne({"_id": i["_id"]}, op)]
         nmap[i["nri_name"]] = i
     # Report hanging interfaces
     for n in smap:
         svc = smap[n]
         if n not in nmap:
             self.logger.info(
                 "Cannot bind service %s. Cannot find NRI interface %s",
                 svc, n)
             continue
         i = nmap[n]
         self.logger.info("Binding service %s to interface %s", svc,
                          i["name"])
         op = {"service": svc}
         p = prof_map.get(svc)
         if p:
             op["profile"] = p.interface_profile.id
         bulk += [UpdateOne({"_id": i["_id"]}, {"$set": op})]
     if bulk:
         self.logger.info("Sending %d updates", len(bulk))
         icol.bulk_write(bulk)
         ServiceSummary.refresh_object(self.object.id)
         change_tracker.register([("managedobject", self.object.id)])
Пример #5
0
class CheckLinkJob(AlarmJob):
    name = "check_link"
    map_task = "get_interface_status"

    def get_map_task_params(self):
        return {"interface": self.data["interface"]}

    def handler(self, object, result):
        """
        Process result like
        <object>, [{'interface': 'Gi 1/0', 'status': True}]
        :param object:
        :param result:
        :return:
        """
        self.logger.debug("check_link returns %s (checking %s)", result,
                          self.data)
        for r in result:
            if (r["status"] and r["interface"] == self.data["interface"]):
                self.clear_alarm("Interface '%s' is up" %
                                 (self.data["interface"]))
                break
        return True

    def get_effective_intervals(self):
        def parse(x):
            x = x.strip()
            if not x:
                return []
            parts = [p.strip() for p in x.split(",")]
            if not parts:
                return []
            if len(parts) % 2:
                self.error(
                    "Invalid interval description '%s': Must be even size" % x)
                return []
            try:
                parts = [int(p) if p else None for p in parts]
            except ValueError, why:
                self.error("Invalid interval description '%s': %s" % (x, why))
                return []
            if parts[-2] is not None:
                self.error(
                    "Invalid interval description '%s': Next to last element must be empty"
                )
                return []
            # @todo: Check times are in ascending order
            # @todo: Check for additional Nones
            return parts

        mo = self.get_managed_object()
        ifname = mo.profile.convert_interface_name(self.data["interface"])
        iface = Interface.objects.get(managed_object=mo.id,
                                      name=ifname).first()
        # Check interface profile first
        if iface and iface.profile:
            if iface.profile.check_link_interval:
                return parse(iface.profile.check_link_interval)
        # Check Managed object's profile
        if mo.object_profile.check_link_interval:
            return parse(iface.profile.check_link_interval)
        # Fallback to default interface profile
        # when interface profile doesn't set
        if iface and not iface.profile:
            dp = InterfaceProfile.get_default_profile()
            if dp and dp.check_link_interval:
                return parse(dp.check_link_interval)
        # Job disabled
        return []