Пример #1
0
 def get_reboots(start_date=None, stop_date=None):
     match = {"ts": {"$gte": start_date, "$lte": stop_date}}
     pipeline = [
         {
             "$match": match
         },
         {
             "$group": {
                 "_id": "$object",
                 "count": {
                     "$sum": 1
                 }
             }
         },
         {
             "$sort": {
                 "count": -1
             }
         },
     ]
     data = (Reboot._get_collection().with_options(
         read_preference=ReadPreference.SECONDARY_PREFERRED).aggregate(
             pipeline))
     # data = data["result"]
     return dict((rb["_id"], rb["count"]) for rb in data)
Пример #2
0
 def extract(self):
     nr = 0
     for d in (Reboot._get_collection().find(
         {
             "ts": {
                 "$gt": self.start,
                 "$lte": self.stop
             }
         },
             no_cursor_timeout=True).sort("ts")):
         mo = ManagedObject.get_by_id(d["object"])
         if not mo:
             continue
         self.reboot_stream.push(
             ts=d["ts"],
             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,
         )
         nr += 1
         self.last_ts = d["ts"]
     self.reboot_stream.finish()
     return nr
Пример #3
0
 def get_start(cls):
     d = Reboot._get_collection().find_one({}, {
         "_id": 0,
         "ts": 1
     },
                                           sort=[("ts", 1)])
     if not d:
         return None
     return d.get("ts")
Пример #4
0
    def get_data(self, interval, **kwargs):
        interval = int(interval)
        ts = datetime.datetime.now() - datetime.timedelta(days=interval)
        pipeline = [{
            "$match": {
                "ts": {
                    "$gte": ts
                }
            }
        }, {
            "$group": {
                "_id": "$object",
                "count": {
                    "$sum": 1
                }
            }
        }, {
            "$sort": {
                "count": -1
            }
        }]
        data = Reboot._get_collection().aggregate(pipeline)
        data = data["result"]
        # Get managed objects
        ids = [x["_id"] for x in data]
        mo_names = {}  # mo id -> mo name
        cursor = connection.cursor()
        while ids:
            chunk = [str(x) for x in ids[:500]]
            ids = ids[500:]
            cursor.execute("""
                SELECT id, name
                FROM sa_managedobject
                WHERE id IN (%s)""" % ", ".join(chunk))
            mo_names.update(dict(cursor))
        #
        data = [(mo_names.get(x["_id"], "---"), x["count"]) for x in data]

        return self.from_dataset(title=self.title,
                                 columns=[
                                     "Managed Object",
                                     TableColumn("Reboots",
                                                 align="right",
                                                 format="numeric",
                                                 total="sum")
                                 ],
                                 data=data,
                                 enumerate=True)
Пример #5
0
    def get_data(self,
                 request,
                 interval,
                 from_date=None,
                 to_date=None,
                 **kwargs):
        interval = int(interval)
        if not from_date:
            interval = 1
        if interval:
            ts = datetime.datetime.now() - datetime.timedelta(days=interval)
            match = {"ts": {"$gte": ts}}
        else:
            t0 = datetime.datetime.strptime(from_date, "%d.%m.%Y")
            if not to_date:
                t1 = datetime.datetime.now()
            else:
                t1 = datetime.datetime.strptime(
                    to_date, "%d.%m.%Y") + datetime.timedelta(days=1)
            match = {"ts": {"$gte": t0, "$lte": t1}}
        pipeline = [
            {
                "$match": match
            },
            {
                "$group": {
                    "_id": "$object",
                    "count": {
                        "$sum": 1
                    }
                }
            },
            {
                "$sort": {
                    "count": -1
                }
            },
        ]
        data = list(Reboot._get_collection().aggregate(pipeline))
        # Get managed objects
        if not request.user.is_superuser:
            id_perm = [
                mo.id for mo in ManagedObject.objects.filter(
                    administrative_domain__in=UserAccess.get_domains(
                        request.user))
            ]
            ids = [x["_id"] for x in data if x["_id"] in id_perm]
        else:
            ids = [x["_id"] for x in data]
        mo_names = {}  # mo id -> mo name
        cursor = connection.cursor()
        while ids:
            chunk = [str(x) for x in ids[:500]]
            ids = ids[500:]
            cursor.execute("""
                SELECT id, address, name
                FROM sa_managedobject
                WHERE id IN (%s)""" % ", ".join(chunk))
            mo_names.update(dict((c[0], c[1:3]) for c in cursor))
        #
        if not request.user.is_superuser:
            data = [(mo_names.get(x["_id"], "---")[1],
                     mo_names.get(x["_id"], "---")[0], x["count"])
                    for x in data if x["_id"] in id_perm]
        else:
            data = [(mo_names.get(x["_id"], "---")[1],
                     mo_names.get(x["_id"], "---")[0], x["count"])
                    for x in data]

        return self.from_dataset(
            title=self.title,
            columns=[
                _("Managed Object"),
                _("Address"),
                TableColumn(_("Reboots"),
                            align="right",
                            format="numeric",
                            total="sum"),
            ],
            data=data,
            enumerate=True,
        )
Пример #6
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
Пример #7
0
 def clean(self, force=False):
     if force:
         print("Clean Reboots collection before %s" % self.clean_ts)
         Reboot._get_collection().remove({"ts": {"$lte": self.clean_ts}})