Exemplo n.º 1
0
 def get_group_by_instance(self, instance_id):
     """return a group by instance id, return None if no group was found"""
     # Shujuquanxian, join
     with session_scope() as session:
         group_to_instance = session.query(GroupToInstance).filter_by(instance_id=instance_id).first()
         user, country = session.query(AlarmGroup, GroupToInstance.country).join(Country).filter(User.user_email == '*****@*****.**').first()
         return session.query(AlarmGroup).get(group_to_instance.group_id)
Exemplo n.º 2
0
    def detach_instance(self, alarm_group, instance_id):
        try:
            with session_scope() as session:
                if self.tenant_id != alarm_group.tenant_id:
                    return

                group_to_instance = session.query(GroupToInstance).get(instance_id)
                if not group_to_instance or group_to_instance.group_id != alarm_group.id:
                    return

                for alarm_to_instance in session.query(AlarmToInstance).filter_by(group_id=alarm_group.id, instance_id=instance_id):
                    if not alarm_to_instance.ceilometer_alarm_id:
                        """Delete anything related to this alarm since ceilometer alarm hasn't been created yet"""
                        session.query(AlarmToInstanceJob).filter(AlarmToInstanceJob.alarm_to_instance_id==alarm_to_instance.id).delete()
                        session.delete(alarm_to_instance)
                        continue

                    if self.__delete_ceilometer_alarm(alarm_to_instance.ceilometer_alarm_id):
                        print "A ceilometer alarm has been deleted"
                    else:
                        self.__create_alarm_to_instance_job(session, action="delete", ceilometer_alarm_id=alarm_to_instance.ceilometer_alarm_id)
                        print "Failed to deltete a ceilometer alarm, a delete job has been created, will need a cleanup later"

                    session.delete(alarm_to_instance)
                session.delete(group_to_instance)
        except Exception, e:
            print "Failed to detach intance from group:", e
Exemplo n.º 3
0
    def add_threshold_alarm_rule(self, alarm_group, name, threshold, meter_name, **kwargs):
        try:
            if self.tenant_id != alarm_group.id:
                return

            kwargs['name'] = name
            kwargs['threshold'] = threshold
            kwargs['meter_name'] = meter_name
            kwargs['tenant_id'] = self.tenant_id
            kwargs["group_id"] = alarm_group.id
            alarm_rule = ThresholdAlarmRule(**kwargs)
            with session_scope() as session:
                session.add(alarm_rule)
                group_to_instances = session.query(GroupToInstance).filter_by(group_id=alarm_group.id)
                for instance in group_to_instances:
                    alarm_to_instance = self.__create_alarm_to_instance(session, alarm_group.id, instance.instance_id, alarm_rule.id)
                    ceilometer_alarm_id = self.__create_ceilometer_alarm(alarm_rule.get_payload(instance.instance_id))
                    if ceilometer_alarm_id:
                        alarm_to_instance.ceilometer_alarm_id = ceilometer_alarm_id
                        print "Ceilometer alarm has been created"
                    else:
                        session.flush()
                        self.__create_alarm_to_instance_job(session, "create", alarm_rule_id=alarm_rule_id, instance_id=alarm_to_instance.instance_id, alarm_to_instance_id=alarm_to_instance.id)
                        print "Failed to create a ceilometer alarm, a create job has been created, will need to synchronize later"

                return alarm_rule

        except Exception as e:
            print "Failed to add rule into group:", e
            raise
Exemplo n.º 4
0
    def get(self, group_id):
        with session_scope() as session:
            group = session.query(AlarmGroup).get(group_id)
            if not group or self.tenant_id != group.tenant_id:
                return None

            return group
Exemplo n.º 5
0
def synchronize_alarms(sync_job_id):
    print "Start synchronizing alarms with Ceilometer server..."
    try:
        with session_scope() as session:
            sync_job = session.query(SyncJob).get(sync_job_id)
            current_sync_time = datetime.utcnow()
            last_sync_time = datetime.min if sync_job.last_run_at is None else sync_job.last_run_at
            alarm_to_instances = session.query(AlarmToInstance).filter(AlarmToInstance.updated_at >= last_sync_time and AlarmToInstance.updated_at <= current_sync_time).all()
            for alarm_to_instance in alarm_to_instances:
                if not alarm_to_instance.ceilometer_alarm_id:
                    _create_ceilometer_alarm(alarm_to_instance)
                    continue

                if alarm_to_instance.exists:
                    """update the alarm: call update api"""
                    result = _update_ceilometer_alarm(alarm_to_instance)
                else:
                    """delete the alarm: call delete api"""
                    result = _delete_ceilometer_alarm(alarm_to_instance)
                    if result is True:
                        session.delete(alarm_to_instance)
                    else:
                        alarm_to_instance.updated_at = datetime.utcnow()

            sync_job.last_run_at = current_sync_time
    except Exception as e:
        print "Sychronize alarms with Ceilometer server failed: %s!!!!!", e
Exemplo n.º 6
0
def delete_group(group):
    """doesn't allow detach when a group is using it"""
    with session_scope() as session:
        # group = session.query(AlarmGroup).get(group.id)
        if not group.instances:
            session.delete(group)
        else:
            raise Exception("Can't delete a group that still has instances attached.")
Exemplo n.º 7
0
def create_group(group_name):
    try:
        group = AlarmGroup(name=group_name)
        with session_scope() as session:
            session.add(group)

        return group.id
    except Exception, e:
        raise e
Exemplo n.º 8
0
    def delete(self, group_id):
        with session_scope() as session:
            group = session.query(AlarmGroup).get(group_id)
            if not group or self.tenant_id != group.tenant_id:
                return

            if not group.intances:
                session.delete(group)
            else:
                raise DeleteAttachedAlarmGroupException("Can't delete a group that still has instances attached.")
Exemplo n.º 9
0
def create_alarm_rule(name=ALARM_NAME):
    alarm_rule = ThresholdAlarmRule(name=name,
            comparison_operator=COMPARISON_OPERATOR,
            threshold=THRESHOLD,
            meter_name=METER_NAME,
            repeat_actions=True)
    with session_scope() as session:
        session.add(alarm_rule)

    return alarm_rule
Exemplo n.º 10
0
    def update(self, group_id, name=None, description=None):
        with session_scope() as session:
            group = session.query(AlarmGroup).get(group_id)
            if not group or self.tenant_id != group.tenant_id:
                return None

            if name:
                group.name = name
            if description:
                group.description = description

            return group
Exemplo n.º 11
0
def attach_instance_to_group(group_id, instance_id):
    with session_scope() as session:
        instance = session.query(Instance).get(instance_id)
        group = session.query(AlarmGroup).get(group_id)
        group.instances.append(instance)
        alarm_rules = group.alarm_rules
        for alarm_rule in alarm_rules:
            alarm_to_instance = _create_alarm_to_instance(session, group_id, instance_id)
            alarm_rule.alarm_to_instances.append(alarm_to_instance)
            result = _create_ceilometer_alarm(alarm_to_instance)
            if result is True:
                print "Succeed to create a ceilometer alarm"
            else:
                print "Failed to create a ceilometer alarm, will need to synchronize later"
Exemplo n.º 12
0
def detach_instance_from_group(group_id, instance_id):
    with session_scope() as session:
        instance = session.query(Instance).get(instance_id)
        group = session.query(AlarmGroup).get(group_id)
        group.instances.remove(instance)
        for alarm_to_instance in session.query(AlarmToInstance).filter_by(group_id=group_id, instance_id=instance_id):
            alarm_to_instance.exists = False
            result = _delete_ceilometer_alarm(alarm_to_instance)
            if result is True:
                print "Succeed to delete a ceilometer alarm"
                session.delete(alarm_to_instance)
            else:
                alarm_to_instance.updated_at = datetime.utcnow()
                print "Failed to deltete a ceilometer alarm, will need a cleanup later"
Exemplo n.º 13
0
    def synchronize(self):
        print "Start synchronizing alarms with Ceilometer server..."
        jobs = None
        with session_scope() as session:
            jobs = session.query(AlarmToInstanceJob).all()

        for job in jobs:
            with session_scope() as session:
                try:
                    if job.action == "create":
                        """Will there be a problem if the rule is deleted????"""
                        ceilometer_alarm_id = self.__create_ceilometer_alarm(session.query(AlarmRule).get(job.alarm_rule_id).get_payload(job.instance_id))
                        if ceilometer_alarm_id:
                            alarm_to_instance = session.query(AlarmToInstance).get(job.alarm_to_instance_id)
                            if alarm_to_instance:
                                alarm_to_instance.ceilometer_alarm_id = ceilometer_alarm_id
                            else:
                                self.__create_alarm_to_instance_job(session, "delete", ceilometer_alarm_id=ceilometer_alarm_id)
                            session.delete(job)
                            print "Creating a ceilometer alarm job succeed"
                        else:
                            print "Creating a ceilometer alarm job failed"
                    elif job.action == "delete":
                        if self.__delete_ceilometer_alarm(job.ceilometer_alarm_id):
                            session.delete(job)
                            print "Deletinbg a ceilometer alarm job succeed"
                        else:
                            print "Deletinbg a ceilometer alarm job failed"
                    elif job.action == "update":
                        """update"""
                        if self.__update_ceilometer_alarm(session.query(AlarmRule).get(job.alarm_rule_id).get_payload(job.instance_id), job.ceilometer_alarm_id):
                            session.delete(job)
                            print "Updating ceilometer alarm job succeed"
                        else:
                            print "Updating ceilometer alarm job failed"
                except Exception as e:
                    print "Synchronize alarms with Ceilometer server job failed:", e
Exemplo n.º 14
0
def remove_alarm_from_group(alarm_rule_id):
    with session_scope() as session:
        alarm_rule = session.query(AlarmRule).get(alarm_rule_id)
        for alarm_to_instance in alarm_rule.alarm_to_instances:
            if not alarm_to_instance.exists:
                continue

            alarm_to_instance.exists = False
            result = _delete_ceilometer_alarm(alarm_to_instance)
            if result is True:
                session.delete(alarm_to_instance)
                print "Succeed to delete a database alarm"
            else:
                print "Failed to delete a ceilometer alarm, will need to synchronize later"

        session.delete(alarm_rule)
Exemplo n.º 15
0
def add_alarm_to_group(group_id, alarm_rule_id, session=None):
    ###
    with session_scope() as session:
        group = session.query(AlarmGroup).get(group_id)
        instances = group.instances
        alarm_rule = session.query(AlarmRule).get(alarm_rule_id)
        group.alarm_rules.append(alarm_rule)
        for instance in instances:
            instance_id = instance.instance_id
            alarm_to_instance = _create_alarm_to_instance(session, group_id, instance_id)
            # query or not?
            alarm_rule.alarm_to_instances.append(alarm_to_instance)
            result = _create_ceilometer_alarm(alarm_to_instance)
            if result is True:
                print "Succeed to create a ceilometer alarm"
            else:
                print "Failed to create a ceilometer alarm, will need to synchronize later"
Exemplo n.º 16
0
    def remove_alarm_rule(self, alarm_rule_id):
        with session_scope() as session:
            alarm_rule = session.query(AlarmRule).get(alarm_rule_id)
            if not alarm_rule or self.tenant_id != alarm_rule.tenant_id:
                return

            for alarm_to_instance in alarm_rule.alarm_to_instances:
                if not alarm_to_instance.ceilometer_alarm_id:
                    """Delete anything related to this alarm since ceilometer alarm hasn't been created yet"""
                    session.query(AlarmToInstanceJob).filter(AlarmToInstanceJob.alarm_to_instance_id==alarm_to_instance.id).delete()
                    session.delete(alarm_to_instance)
                    continue

                if self.__delete_ceilometer_alarm(alarm_to_instance.ceilometer_alarm_id):
                    print "A ceilometer alarm has been deleted"
                else:
                    self.__create_alarm_to_instance_job(session, "delete", ceilometer_alarm_id=alarm_to_instance.ceilometer_alarm_id)
                    print "Failed to delete a ceilometer alarm, a delete job has been created, will need to synchronize later"

                session.delete(alarm_to_instance)
            session.delete(alarm_rule)
Exemplo n.º 17
0
    def attach_instance(self, alarm_group, instance_id):
        try:
            with session_scope() as session:
                if session.query(GroupToInstance).get(instance_id):
                    raise AttachAttachedIntanceException("Attempt to attach an instance that already has a group attached")

                instance = GroupToInstance(instance_id=instance_id, group_id=alarm_group.id)
                session.add(instance)
                alarm_rules = session.query(AlarmRule).filter_by(group_id=alarm_group.id).all()
                for alarm_rule in alarm_rules:
                    alarm_to_instance = self.__create_alarm_to_instance(session, alarm_group.id, instance_id, alarm_rule.id)
                    ceilometer_alarm_id = self.__create_ceilometer_alarm(alarm_rule.get_payload(instance_id))
                    if ceilometer_alarm_id:
                        alarm_to_instance.ceilometer_alarm_id = ceilometer_alarm_id
                        print "Ceilometer alarm has been created"
                    else:
                        session.flush()
                        self.__create_alarm_to_instance_job(session, "create", alarm_rule_id=alarm_rule.id, instance_id=instance_id, alarm_to_instance_id=alarm_to_instance.id)
                        print "Failed to create a ceilometer alarm, a create job has been created, will need to synchronize later"
        except Exception, e:
            print "Failed to attach intance to a group:", e
Exemplo n.º 18
0
def create_instance(instance_id):
    instance = Instance(instance_id=instance_id)
    with session_scope() as session:
        session.add(instance)

    return instance
Exemplo n.º 19
0
def create_sync_job():
    sync_job = SyncJob()
    with session_scope() as session:
        session.add(sync_job)

    return sync_job
Exemplo n.º 20
0
def delete_instance(instance_id, group_id):
    with session_scope() as session:
        group = session.query(AlarmGroup).get(group_id)
        session.query(Instance).filter_by(instance_id=instance_id).delete()
        print group.instances.count()
Exemplo n.º 21
0
 def create(self, name, description=None):
     """create an alarm group"""
     with session_scope() as session:
         group = AlarmGroup(tenant_id=self.tenant_id, name=name, description=description)
         session.add(group)
         return group
Exemplo n.º 22
0
def update_alarm_in_group(alarm_rule_id):
    with session_scope() as session:
        alarm_rule = session.query(AlarmRule).get(alarm_rule_id)
        for alarm_to_instance in alarm_rule.alarm_to_instances:
            alarm_to_instance.updated_at = datetime.utcnow()
Exemplo n.º 23
0
 def list_alarm_group(self):
     with session_scope() as session:
         return session.query(AlarmGroup).filter_by(tenant_id=self.tenant_id).all()