From fb2bb3a696a125224d8288938f6005295277d0da Mon Sep 17 00:00:00 2001 From: Liat Har-Tal Date: Sun, 13 Mar 2016 11:34:10 +0000 Subject: [PATCH] evaluator - bugs fixing: 1. adding state to deduced alarms 2. handle datetime in action executor Change-Id: I26f60ca6fc76e8b80d12000126f2a0d2aabdf536 --- vitrage/common/datetime_utils.py | 4 ++-- vitrage/evaluator/actions/action_executor.py | 2 +- .../evaluator/actions/evaluator_event_transformer.py | 11 +++++++++-- vitrage/evaluator/actions/recipes/raise_alarm.py | 6 ++++++ .../functional/evaluator/test_action_executor.py | 6 +++++- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/vitrage/common/datetime_utils.py b/vitrage/common/datetime_utils.py index ad69c9707..930230ada 100644 --- a/vitrage/common/datetime_utils.py +++ b/vitrage/common/datetime_utils.py @@ -15,9 +15,9 @@ from datetime import datetime from oslo_utils import timeutils -def utcnow(): +def utcnow(with_timezone=True): """Better version of utcnow() that returns utcnow with a correct TZ.""" - return timeutils.utcnow(True) + return timeutils.utcnow(with_timezone) def change_time_str_format(timestamp_str, old_format, new_format): diff --git a/vitrage/evaluator/actions/action_executor.py b/vitrage/evaluator/actions/action_executor.py index aa21f1e88..4db8656b0 100644 --- a/vitrage/evaluator/actions/action_executor.py +++ b/vitrage/evaluator/actions/action_executor.py @@ -112,7 +112,7 @@ class ActionExecutor(object): event[SyncProps.SYNC_MODE] = SyncMode.UPDATE event[SyncProps.SYNC_TYPE] = EntityType.VITRAGE - event[EProps.UPDATE_TIMESTAMP] = str(datetime_utils.utcnow()) + event[EProps.UPDATE_TIMESTAMP] = str(datetime_utils.utcnow(False)) @staticmethod def _register_action_recipes(): diff --git a/vitrage/evaluator/actions/evaluator_event_transformer.py b/vitrage/evaluator/actions/evaluator_event_transformer.py index f2ac3538c..6b98b6bf1 100644 --- a/vitrage/evaluator/actions/evaluator_event_transformer.py +++ b/vitrage/evaluator/actions/evaluator_event_transformer.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. from oslo_log import log as logging +from vitrage.common import datetime_utils from vitrage.common.constants import EdgeLabels from vitrage.common.constants import EdgeProperties as EProps @@ -46,10 +47,15 @@ class EvaluatorEventTransformer(transformer_base.TransformerBase): event_type = event[EVALUATOR_EVENT_TYPE] + timestamp = datetime_utils.change_time_str_format( + event[VProps.UPDATE_TIMESTAMP], + '%Y-%m-%d %H:%M:%S.%f', + transformer_base.TIMESTAMP_FORMAT) + if event_type == UPDATE_VERTEX: properties = { VProps.VITRAGE_STATE: event[VProps.VITRAGE_STATE], - VProps.UPDATE_TIMESTAMP: event[VProps.UPDATE_TIMESTAMP] + VProps.UPDATE_TIMESTAMP: timestamp } return Vertex(event[VProps.VITRAGE_ID], properties) @@ -58,7 +64,8 @@ class EvaluatorEventTransformer(transformer_base.TransformerBase): metadata = { VProps.UPDATE_TIMESTAMP: event[VProps.UPDATE_TIMESTAMP], VProps.NAME: event[TFields.ALARM_NAME], - VProps.SEVERITY: event[TFields.SEVERITY] + VProps.SEVERITY: event[TFields.SEVERITY], + VProps.STATE: event[VProps.STATE] } return graph_utils.create_vertex( self.extract_key(event), diff --git a/vitrage/evaluator/actions/recipes/raise_alarm.py b/vitrage/evaluator/actions/recipes/raise_alarm.py index 2e520eb37..feb6fae58 100644 --- a/vitrage/evaluator/actions/recipes/raise_alarm.py +++ b/vitrage/evaluator/actions/recipes/raise_alarm.py @@ -12,11 +12,14 @@ # License for the specific language governing permissions and limitations # under the License. +from vitrage.common.constants import VertexProperties as VProps from vitrage.evaluator.actions.recipes.action_steps import ADD_VERTEX from vitrage.evaluator.actions.recipes.action_steps import NOTIFY from vitrage.evaluator.actions.recipes.action_steps import REMOVE_VERTEX from vitrage.evaluator.actions.recipes import base from vitrage.evaluator.actions.recipes.base import ActionStepWrapper +from vitrage.synchronizer.plugins.base.alarm.properties \ + import AlarmProperties as AlarmProps class RaiseAlarm(base.Recipe): @@ -24,6 +27,9 @@ class RaiseAlarm(base.Recipe): @staticmethod def get_do_recipe(action_spec): + params = RaiseAlarm._get_vertex_params(action_spec) + params[VProps.STATE] = AlarmProps.ALARM_STATE + add_vertex_step = ActionStepWrapper( ADD_VERTEX, RaiseAlarm._get_vertex_params(action_spec)) diff --git a/vitrage/tests/functional/evaluator/test_action_executor.py b/vitrage/tests/functional/evaluator/test_action_executor.py index a3e3402f9..ed8fa18d6 100644 --- a/vitrage/tests/functional/evaluator/test_action_executor.py +++ b/vitrage/tests/functional/evaluator/test_action_executor.py @@ -27,9 +27,12 @@ from vitrage.evaluator.actions.base import ActionType from vitrage.evaluator.template import ActionSpecs from vitrage.evaluator.template_fields import TemplateFields as TFields from vitrage.service import load_plugin +from vitrage.synchronizer.plugins.base.alarm.properties \ + import AlarmProperties as AlarmProps from vitrage.tests.functional.entity_graph.base import \ TestEntityGraphFunctionalBase + LOG = logging.getLogger(__name__) @@ -155,7 +158,8 @@ class TestActionExecutor(TestEntityGraphFunctionalBase): targets = {TFields.TARGET: host.vertex_id} props = { TFields.ALARM_NAME: 'VM_CPU_SUBOPTIMAL_PERFORMANCE', - TFields.SEVERITY: 'critical' + TFields.SEVERITY: 'critical', + VProps.STATE: AlarmProps.ALARM_STATE } action_spec = ActionSpecs(ActionType.RAISE_ALARM, targets, props)