Ejemplo n.º 1
0
 def handler(self):
     self.logger.info("NRI Portmapper")
     if not self.object.remote_system:
         self.logger.info("Created directly. No NRI integration. Skipping check")
         return
     nri = self.object.remote_system.name
     # Check object has interfaces
     if not self.has_capability("DB | Interfaces"):
         self.logger.info("No interfaces discovered. " "Skipping interface status check")
         return
     # Get portmapper instance
     portmapper = portmapper_loader.get_loader(self.object.remote_system.name)(self.object)
     if not portmapper:
         self.logger.info("[%s] No portmapper for NRI. Skipping checks", nri)
         return
     # Process interfaces
     bulk = []
     icol = Interface._get_collection()
     for d in icol.find(
         {"managed_object": self.object.id, "type": "physical"},
         {"_id": 1, "name": 1, "nri_name": 1},
     ):
         nri_name = portmapper.to_remote(d["name"])
         self.logger.debug("[%s] Port mapping %s <-> %s", nri, d["name"], nri_name)
         if not nri_name:
             self.logger.info("[%s] Cannot map port name '%s'", nri, d["name"])
         elif d.get("nri_name") != nri_name:
             self.logger.info("[%s] Mapping '%s' to '%s'", nri, nri_name, d["name"])
             bulk += [UpdateOne({"_id": d["_id"]}, {"$set": {"nri_name": nri_name}})]
     if bulk:
         self.logger.info("Sending %d updates", len(bulk))
         icol.bulk_write(bulk)
Ejemplo n.º 2
0
 def handle_portmap(self, portmap_objects=[]):
     for po in portmap_objects:
         for o in ManagedObjectSelector.get_objects_from_expression(po):
             if not o.remote_system:
                 self.stdout.write("%s (%s, %s) NRI: N/A\n" %
                                   (o.name, o.address, o.platform))
                 continue
             portmapper = loader.get_loader(o.remote_system.name)(o)
             nri = o.remote_system.name
             self.stdout.write("%s (%s, %s) NRI: %s\n" %
                               (o.name, o.address, o.platform, nri))
             r = []
             for i in Interface._get_collection().find(
                 {
                     "managed_object": o.id,
                     "type": "physical"
                 },
                 {
                     "_id": 1,
                     "name": 1,
                     "nri_name": 1
                 },
             ):
                 rn = portmapper.to_remote(i["name"]) or self.PORT_ERROR
                 if rn == self.PORT_ERROR:
                     ln = self.PORT_ERROR
                 else:
                     ln = portmapper.to_local(rn) or self.PORT_ERROR
                 if i.get("nri_name") == rn and ln != self.PORT_ERROR:
                     status = "OK"
                 elif not i.get("nri_name") and ln != self.PORT_ERROR:
                     status = "Not in database"
                 elif rn == self.PORT_ERROR:
                     status = "Failed to convert to remote name"
                 else:
                     self.print(ln, rn, i.get("nri_name"))
                     status = "Failed to convert to local name"
                 r += [(i["name"], rn, i.get("nri_name", "--"), status)]
             r = [("Local", "Remote", "Interface NRI", "Status")] + list(
                 sorted(r, key=lambda x: alnum_key(x[0])))
             self.stdout.write(
                 "%s\n" %
                 format_table([0, 0, 0, 0], r, sep=" | ", hsep="-+-"))
Ejemplo n.º 3
0
 def get_port_mapper(mo):
     if mo.remote_system:
         return loader.get_loader(mo.remote_system.name)
     return None
Ejemplo n.º 4
0
 def handler(self):
     self.logger.info("NRI Portmapper")
     if not self.object.remote_system:
         self.logger.info(
             "Created directly. No NRI integration. Skipping check")
         return
     nri = self.object.remote_system.name
     # Check object has interfaces
     if not self.has_capability("DB | Interfaces"):
         self.logger.info("No interfaces discovered. "
                          "Skipping interface status check")
         return
     # Get portmapper instance
     pm_cls = portmapper_loader.get_loader(self.object.remote_system.name)
     if not pm_cls:
         self.logger.info("[%s] No portmapper for NRI. Skipping checks",
                          nri)
         return
     portmapper = pm_cls(self.object)
     # Process interfaces
     bulk = []
     icol = Interface._get_collection()
     ifaces_hints = tuple(
         IFHint(name=iface["name"], ifindex=iface.get("ifindex"))
         for iface in icol.find(
             {
                 "managed_object": self.object.id,
                 "type": "physical"
             },
             {
                 "_id": 1,
                 "name": 1,
                 "ifindex": 1
             },
         ))
     for d in icol.find(
         {
             "managed_object": self.object.id,
             "type": "physical"
         },
         {
             "_id": 1,
             "name": 1,
             "nri_name": 1
         },
     ):
         try:
             nri_name = portmapper.to_remote(d["name"],
                                             iface_hints=ifaces_hints)
         except Exception as e:
             self.logger.error(
                 "[%s] Unhandled exception on portmapper handler '%s'. Skipping checks.",
                 nri, e)
             break
         self.logger.debug("[%s] Port mapping %s <-> %s", nri, d["name"],
                           nri_name)
         if not nri_name:
             self.logger.info("[%s] Cannot map port name '%s'", nri,
                              d["name"])
         elif d.get("nri_name") != nri_name:
             self.logger.info("[%s] Mapping '%s' to '%s'", nri, nri_name,
                              d["name"])
             bulk += [
                 UpdateOne({"_id": d["_id"]},
                           {"$set": {
                               "nri_name": nri_name
                           }})
             ]
     if bulk:
         self.logger.info("Sending %d updates", len(bulk))
         icol.bulk_write(bulk)