Ejemplo n.º 1
0
Archivo: alarm.py Proyecto: nbashev/noc
 def _apply_total_services(cls, alarm, r):
     r["total_services"] = []
     for si in alarm.total_services:
         p = ServiceProfile.get_by_id(si.profile)
         if p:
             r["total_services"] += [
                 {"profile": {"id": str(p.id), "name": cls.qs(p.name)}, "summary": si.summary}
             ]
Ejemplo n.º 2
0
 def extract(self, *args, **options):
     nr = 0
     # Get reboots
     r = Reboot._get_collection().aggregate([
         {
             "$match": {
                 "ts": {
                     "$gt": self.start - self.reboot_interval,
                     "$lte": self.stop
                 }
             }
         },
         {
             "$sort": {
                 "ts": 1
             }
         },
         {
             "$group": {
                 "_id": "$object",
                 "reboots": {
                     "$push": "$ts"
                 }
             }
         },
     ])
     # object -> [ts1, .., tsN]
     reboots = {d["_id"]: d["reboots"] for d in r}
     #
     for d in self.iter_data():
         mo = ManagedObject.get_by_id(d["managed_object"])
         if not mo:
             continue
         # Process reboot data
         o_reboots = reboots.get(d["managed_object"], [])
         n_reboots = hits_in_range(o_reboots,
                                   d["timestamp"] - self.reboot_interval,
                                   d["clear_timestamp"])
         #
         self.alarm_stream.push(
             ts=d["timestamp"],
             close_ts=d["clear_timestamp"],
             duration=max(
                 0,
                 int((d["clear_timestamp"] -
                      d["timestamp"]).total_seconds())),
             alarm_id=str(d["_id"]),
             root=str(d.get("root") or ""),
             rca_type=d.get("rca_type") or 0,
             alarm_class=AlarmClass.get_by_id(d["alarm_class"]),
             severity=d["severity"],
             reopens=d.get("reopens") or 0,
             direct_services=sum(ss["summary"]
                                 for ss in d.get("direct_services", [])),
             direct_subscribers=sum(
                 ss["summary"] for ss in d.get("direct_subscribers", [])),
             total_objects=sum(ss["summary"]
                               for ss in d.get("total_objects", [])),
             total_services=sum(ss["summary"]
                                for ss in d.get("total_services", [])),
             total_subscribers=sum(
                 ss["summary"] for ss in d.get("total_subscribers", [])),
             escalation_ts=d.get("escalation_ts"),
             escalation_tt=d.get("escalation_tt"),
             managed_object=mo,
             pool=mo.pool,
             ip=mo.address,
             profile=mo.profile,
             object_profile=mo.object_profile,
             vendor=mo.vendor,
             platform=mo.platform,
             version=mo.version,
             administrative_domain=mo.administrative_domain,
             segment=mo.segment,
             container=mo.container,
             x=mo.x,
             y=mo.y,
             reboots=n_reboots,
             services=[{
                 "profile": ServiceProfile.get_by_id(ss["profile"]).bi_id,
                 "summary": ss["summary"],
             } for ss in d.get("direct_services", [])],
             subscribers=[{
                 "profile":
                 SubscriberProfile.get_by_id(ss["profile"]).bi_id,
                 "summary":
                 ss["summary"],
             } for ss in d.get("direct_subscribers", [])],
             # location=mo.container.get_address_text()
             ack_user=d.get("ack_user", ""),
             ack_ts=d.get("ack_ts"),
         )
         nr += 1
         self.last_ts = d["clear_timestamp"]
     self.alarm_stream.finish()
     return nr
Ejemplo n.º 3
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)])