Exemple #1
0
 def api_test(self, request, id):
     r = []
     data = [
         d for d in Maintenance._get_collection().aggregate([
             {
                 "$match": {
                     "_id": bson.ObjectId(id)
                 }
             },
             {
                 "$project": {
                     "objects": "$affected_objects.object"
                 },
             },
         ])
     ]
     for mo in (ManagedObject.objects.filter(
             is_managed=True,
             id__in=data[0].get("objects")).values("id", "name",
                                                   "is_managed", "profile",
                                                   "address", "description",
                                                   "tags").distinct()):
         r += [{
             "id": mo["id"],
             "name": mo["name"],
             "is_managed": mo["is_managed"],
             "profile": Profile.get_by_id(mo["profile"]).name,
             "address": mo["address"],
             "description": mo["description"],
             "tags": mo["tags"],
         }]
     out = {"total": len(r), "success": True, "data": r}
     return self.response(out, status=self.OK)
Exemple #2
0
 def bulk_field_isinmaintenance(self, data):
     if not data:
         return data
     if data[0]["status"] == "A":
         mtc = set(Maintenance.currently_affected())
         for x in data:
             x["isInMaintenance"] = x["managed_object"] in mtc
     else:
         mos = [x["managed_object"] for x in data]
         pipeline = [
             {"$match": {"affected_objects.object": {"$in": mos}}},
             {"$unwind": "$affected_objects"},
             {
                 "$project": {
                     "_id": 0,
                     "managed_object": "$affected_objects.object",
                     "interval": ["$start", "$stop"],
                 }
             },
             {"$group": {"_id": "$managed_object", "intervals": {"$push": "$interval"}}},
         ]
         mtc = {
             x["_id"]: x["intervals"] for x in Maintenance._get_collection().aggregate(pipeline)
         }
         for x in data:
             if x["managed_object"] in mtc:
                 left, right = list(zip(*mtc[x["managed_object"]]))
                 x["isInMaintenance"] = bisect.bisect(
                     right, dateutil.parser.parse(x["timestamp"]).replace(tzinfo=None)
                 ) != bisect.bisect(
                     left, dateutil.parser.parse(x["clear_timestamp"]).replace(tzinfo=None)
                 )
             else:
                 x["isInMaintenance"] = False
     return data
Exemple #3
0
def fix():
    max_value = Maintenance._get_collection().estimated_document_count()
    for m in progressbar.progressbar(Maintenance.objects.filter(),
                                     max_value=max_value):
        try:
            m.save()
        except Exception as e:
            print("[%s] %s" % (m.id, e))
Exemple #4
0
def start_maintenance(maintenance_id):
    logger.info("[%s] Start maintenance", maintenance_id)
    m = Maintenance.get_by_id(maintenance_id)
    if not m:
        logger.info("[%s] Not found, skipping")
        return
    if not m.escalate_managed_object:
        logger.info("[%s] No managed object to escalate", maintenance_id)
        return
    if m.escalation_tt:
        logger.info("[%s] Already escalated as TT %s", maintenance_id, m.escalation_tt)
        return
    # Get external TT system
    tts_id = m.escalate_managed_object.tt_system_id
    if not tts_id:
        logger.info(
            "[%s] No TT mapping for object %s(%s)",
            maintenance_id,
            m.escalate_managed_object.name,
            m.escalate_managed_object.address,
        )
        return
    tt_system = m.escalate_managed_object.tt_system
    if not tt_system:
        logger.info("[%s] Cannot find TT system '%s'", maintenance_id, m.escalate_managed_object)
        return
    tts = tt_system.get_system()
    try:
        logger.info("[%s] Creating TT", maintenance_id)
        tt_id = tts.create_tt(
            queue=1,
            obj=tts_id,
            reason=0,
            subject=m.subject,
            body=m.description or m.subject,
            login="******",
            timestamp=m.start,
        )
        logger.info("[%s] TT %s created", maintenance_id, tt_id)
        if tts.promote_group_tt:
            gtt = tts.create_group_tt(tt_id, m.start)
            d = Maintenance._get_collection().find_one(
                {"_id": m.id}, {"_id": 0, "affected_objects": 1}
            )
            if d:
                objects = [x["object"] for x in d["affected_objects"]]
                for d in ManagedObject.objects.filter(id__in=list(objects)):
                    logger.info("[%s] Appending object %s to group TT %s", maintenance_id, d, gtt)
                    tts.add_to_group_tt(gtt, d.tt_system_id)
        metrics["maintenance_tt_create"] += 1
    except tts.TTError as e:
        logger.error("[%s] Failed to escalate: %s", maintenance_id, e)
        metrics["maintenance_tt_fail"] += 1
Exemple #5
0
 def get_maintenance(objects: List[int]) -> Set[int]:
     """
     Returns a set of objects currently in maintenance
     :param objects:
     :return:
     """
     now = datetime.datetime.now()
     so = set(objects)
     mnt_objects = set()
     for m in Maintenance._get_collection().find(
         {"is_completed": False, "start": {"$lte": now}}, {"_id": 0, "affected_objects": 1}
     ):
         mnt_objects |= so & {x["object"] for x in m["affected_objects"]}
     return mnt_objects
Exemple #6
0
 def get_maintenance(objects):
     """
     Returns a set of objects currently in maintenance
     :param objects:
     :return:
     """
     now = datetime.datetime.now()
     so = set(objects)
     r = set()
     for m in Maintenance._get_collection().find(
         {
             "is_completed": False,
             "start": {
                 "$lte": now
             }
         }, {
             "_id": 0,
             "affected_objects": 1
         }):
         mo = set(r["object"] for r in m["affected_objects"])
         r |= so & mo
     return r