def _get_alarms(self, *args, **kwargs): """Finds all the alarms with project_id Finds all the alarms which has the project_id. In case the tenant is admin then project_id can also be None. :rtype: list """ alarms = self.db.history_facade.get_alarms(*args, **kwargs) for alarm in alarms: start_timestamp = \ self.db.history_facade.add_utc_timezone(alarm.start_timestamp) alarm.payload[HProps.START_TIMESTAMP] = str(start_timestamp) if alarm.end_timestamp <= db_time(): end_timestamp = \ self.db.history_facade.add_utc_timezone( alarm.end_timestamp) alarm.payload[HProps.END_TIMESTAMP] = str(end_timestamp) # change operational severity of ended alarms to 'OK' # TODO(annarez): in next version use only 'state' alarm.payload[VProps.VITRAGE_OPERATIONAL_SEVERITY] = \ OperationalAlarmSeverity.OK # TODO(annarez): implement state change in processor and DB alarm.payload[VProps.STATE] = AProps.INACTIVE_STATE return alarms
def disable_alarms_in_history(self): end_time = db_time() active_alarms = self.get_alarms(limit=0) changes_to_add = [alarm.vitrage_id for alarm in active_alarms] self._alarms.end_all_alarms(end_time) self._edges.end_all_edges(end_time) self._changes.add_end_changes(changes_to_add, end_time)
def count_active_alarms(self, project_id=None, is_admin_project=False): session = self._engine_facade.get_session() query = session.query(models.Alarm) query = query.filter(models.Alarm.end_timestamp > db_time()) query = self._add_project_filtering_to_query(query, project_id, is_admin_project) query_severe = query.filter( models.Alarm.vitrage_operational_severity == OSeverity.SEVERE) query_critical = query.filter( models.Alarm.vitrage_operational_severity == OSeverity.CRITICAL) query_warning = query.filter( models.Alarm.vitrage_operational_severity == OSeverity.WARNING) query_ok = query.filter( models.Alarm.vitrage_operational_severity == OSeverity.OK) query_na = query.filter( models.Alarm.vitrage_operational_severity == OSeverity.NA) counts = { OSeverity.SEVERE: query_severe.count(), OSeverity.CRITICAL: query_critical.count(), OSeverity.WARNING: query_warning.count(), OSeverity.OK: query_ok.count(), OSeverity.NA: query_na.count() } return counts
def get_rca(self, ctx, root, all_tenants): LOG.debug("RcaApis get_rca - root: %s, all_tenants=%s", root, all_tenants) project_id = ctx.get(TenantProps.TENANT, None) is_admin_project = ctx.get(TenantProps.IS_ADMIN, False) if all_tenants: db_nodes, db_edges = self.db.history_facade.alarm_rca(root) else: db_nodes, db_edges = self.db.history_facade.alarm_rca( root, project_id=project_id, admin=is_admin_project) for n in db_nodes: start_timestamp = \ self.db.history_facade.add_utc_timezone(n.start_timestamp) n.payload[HProps.START_TIMESTAMP] = str(start_timestamp) if n.end_timestamp <= db_time(): end_timestamp = \ self.db.history_facade.add_utc_timezone(n.end_timestamp) n.payload[HProps.END_TIMESTAMP] = str(end_timestamp) # TODO(annarez): implement state change in processor and DB n.payload[VProps.STATE] = AProps.INACTIVE_STATE vertices = [ Vertex(vertex_id=n.vitrage_id, properties=n.payload) for n in db_nodes ] edges = [ Edge(source_id=e.source_id, target_id=e.target_id, label=e.label, properties=e.payload) for e in db_edges ] rca_graph = NXGraph(vertices=vertices, edges=edges) json_graph = rca_graph.json_output_graph( inspected_index=self._find_rca_index(rca_graph, root)) return json_graph
def get_alarms(self, start=None, end=None, limit=LIMIT, sort_by=(HProps.START_TIMESTAMP, HProps.VITRAGE_ID), sort_dirs=(ASC, ASC), filter_by=None, filter_vals=None, next_page=True, marker=None, only_active_alarms=False, project_id=None, is_admin_project=False): """Return alarms that match all filters sorted by the given keys. Deleted alarms will be returned when only_active_alarms=False. filtering and sorting are possible on each row of alarms table (pay attantion: it is not recommended to filter by start_timestamp and end_timestamp when start or end arguments are passed): vitrage_id, start_timestamp, end_timestamp, name, vitrage_type, vitrage_aggregated_severity, project_id, vitrage_resource_type, vitrage_resource_id, vitrage_resource_project_id, payload Time Frame: start and end arguments gives the time frame for required alarms. Required format is the format that can be parsed by timeutils library. If both arguments are given, returned alarms are the alarms that where active sometime during given time frame (including active and inactive alarms): 1. start_ts------------end_ts 2. start_ts------------end_ts 3. start_ts------------end_ts 4. start_ts---------------------------------------end_ts start end |_______________________________| If only start is given, all alarms that started after this time will be returned (including active and inactive alarms): 1. start_ts------------end_ts 2. start_ts------ start now |_______________________________| note1: end argument can't be used without start argument note2: time frame can't be used with flag only_active_alarms=True Filtering: filter_by represents parameters to filter on, and filter_vals contains the values to filter on in corresponding order to the order of parameters in filter_by. The filtering is according to SQL 'like' statement. It's possible to filter on each row of alarms table The filtering is also possible on list of values. examples: 1. In the following example: | filter_by = ['vitrage_type', 'vitrage_resource_type'] | filter_vals = ['zabbix', 'nova'] which will be evaluated to: Alarm.vitrage_type like '%zabbix%' and Alarm.vitrage_resource_type like '%nova%' Tthe filtering will be done so the query returns all the alarms in the DB with vitrage type containing the string 'zabbix' and vitrage resource type containing the string 'nova' 2. Following example is filtering list of values for one same property: | filter_by = ['vitrage_type', 'vitrage_id'] | filter_vals = ['zabbix', ['123', '456', '789']] It will be evaluated to: Alarm.vitrage_type like '%zabbix%' and Alarm.vitrage_resource_type like '%123%' or like '%456%' or like '%789%' Tthe filtering will be done so the query returns all the alarms in the DB with vitrage type containing the string 'zabbix' and with one of vitrage_ids that are in the list in filter_vals[1] :param start: start of time frame :param end: end of time frame :param limit: maximum number of items to return, if limit=0 the method will return all matched items in alarms table, if limit is bigger then default parameter LIMIT, the number of items that will be returned will be defined by the default parameter LIMIT :param sort_by: array of attributes by which results should be sorted :param sort_dirs: per-column array of sort_dirs, corresponding to sort_keys ('asc' or 'desc'). :param filter_by: array of attributes by which results will be filtered :param filter_vals: per-column array of filter values corresponding to filter_by :param next_page: if True will return next page when marker is given, if False will return previous page when marker is given, otherwise, returns first page if no marker was given. :param marker: if None returns first page, else if vitrage_id is given and next_page is True, return next #limit results after marker, else, if next page is False,return #limit results before marker. :param only_active_alarms: if True, returns only active alarms, if False return active and non-active alarms. :param project_id: if None there is no filtering by project_id (equals to All Tenants=True), if id is given, query will be fillter alarms by project id. :param is_admin_project: True to return alarms with project_id=None or resource_project_id=None """ session = self._engine_facade.get_session() query = session.query(models.Alarm) query = self._add_project_filtering_to_query(query, project_id, is_admin_project) self.assert_args(start, end, filter_by, filter_vals, only_active_alarms, sort_dirs) if only_active_alarms: query = query.filter(models.Alarm.end_timestamp > db_time()) elif (start and end) or start: query = self._add_time_frame_to_query(query, start, end) query = self._add_filtering_to_query(query, filter_by, filter_vals) if limit: query = self._generate_alarms_paginate_query( query, limit, sort_by, sort_dirs, next_page, marker) elif limit == 0: sort_dir_func = { ASC: sqlalchemy.asc, DESC: sqlalchemy.desc, } for i in range(len(sort_by)): query.order_by(sort_dir_func[sort_dirs[i]](getattr( models.Alarm, sort_by[i]))) return query.all()