From: Atul Agarwal Date: Sun, 6 Jun 2021 08:36:48 +0000 (+0000) Subject: Feature 10339 - Enhanced Alarm Mgmt. (SOL005 FM Interface) X-Git-Tag: v10.0.0^0 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F67%2F10967%2F1;p=osm%2FMON.git Feature 10339 - Enhanced Alarm Mgmt. (SOL005 FM Interface) Change-Id: I8f9c1ea2c80c41c99b6016926afa1709e817d216 Signed-off-by: Atul Agarwal --- diff --git a/osm_mon/core/common_db.py b/osm_mon/core/common_db.py index df8db60..cf52d85 100644 --- a/osm_mon/core/common_db.py +++ b/osm_mon/core/common_db.py @@ -157,9 +157,12 @@ class CommonDbClient: return self.common_db.get_one("projects", {"_id": project_id}) def create_alarm(self, alarm: Alarm): + action_data = {"uuid": alarm.uuid, "action": alarm.action} + self.common_db.create("alarms_action", action_data) return self.common_db.create("alarms", alarm.to_dict()) def delete_alarm(self, alarm_uuid: str): + self.common_db.del_one("alarms_action", {"uuid": alarm_uuid}) return self.common_db.del_one("alarms", {"uuid": alarm_uuid}) def get_alarms(self) -> List[Alarm]: @@ -169,6 +172,13 @@ class CommonDbClient: alarms.append(Alarm.from_dict(alarm_dict)) return alarms + def update_alarm_status(self, alarm_state: str, uuid): + modified_count = self.common_db.set_one("alarms", {"uuid": uuid}, {"alarm_status": alarm_state}) + return modified_count + + def get_alarm_by_uuid(self, uuid: str): + return self.common_db.get_one("alarms", {"uuid": uuid}) + def get_user(self, username: str): return self.common_db.get_one("users", {"username": username}) diff --git a/osm_mon/core/models.py b/osm_mon/core/models.py index 8fe3b6b..48fe8c4 100644 --- a/osm_mon/core/models.py +++ b/osm_mon/core/models.py @@ -32,7 +32,9 @@ class Alarm: operation: str = None, statistic: str = None, metric: str = None, + action: str = None, tags: dict = {}, + alarm_status: str = "ok", ): self.uuid = str(uuid.uuid4()) self.name = name @@ -41,7 +43,9 @@ class Alarm: self.operation = operation self.statistic = statistic self.metric = metric + self.action = action self.tags = tags + self.alarm_status = alarm_status def to_dict(self) -> dict: alarm = { @@ -53,6 +57,7 @@ class Alarm: "metric": self.metric, "tags": self.tags, "operation": self.operation, + "alarm_status": self.alarm_status, } return alarm @@ -67,4 +72,5 @@ class Alarm: alarm.metric = data.get("metric") alarm.tags = data.get("tags") alarm.operation = data.get("operation") + alarm.alarm_status = data.get("alarm_status") return alarm diff --git a/osm_mon/evaluator/evaluator.py b/osm_mon/evaluator/evaluator.py index 2d319c9..732f8ac 100644 --- a/osm_mon/evaluator/evaluator.py +++ b/osm_mon/evaluator/evaluator.py @@ -72,6 +72,9 @@ class Evaluator: self.loop.run_until_complete( self.msg_bus.aiowrite("alarm_response", "notify_alarm", resp_message) ) + evaluator_service = EvaluatorService(self.conf) + evaluator_service.update_alarm_status(status.value, alarm.uuid) + return def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus): log.debug("_build_alarm_response") diff --git a/osm_mon/evaluator/service.py b/osm_mon/evaluator/service.py index 2ba0625..9049d95 100644 --- a/osm_mon/evaluator/service.py +++ b/osm_mon/evaluator/service.py @@ -39,6 +39,7 @@ class AlarmStatus(Enum): ALARM = "alarm" OK = "ok" INSUFFICIENT = "insufficient-data" + DISABLED = "disabled" class EvaluatorService: @@ -55,20 +56,27 @@ class EvaluatorService: def _evaluate_metric(self, alarm: Alarm): log.debug("_evaluate_metric") metric_value = self._get_metric_value(alarm.metric, alarm.tags) - if metric_value is None: - log.warning("No metric result for alarm %s", alarm.uuid) - self.queue.put((alarm, AlarmStatus.INSUFFICIENT)) - else: - if alarm.operation.upper() == "GT": - if metric_value > alarm.threshold: - self.queue.put((alarm, AlarmStatus.ALARM)) - else: - self.queue.put((alarm, AlarmStatus.OK)) - elif alarm.operation.upper() == "LT": - if metric_value < alarm.threshold: - self.queue.put((alarm, AlarmStatus.ALARM)) - else: - self.queue.put((alarm, AlarmStatus.OK)) + if alarm.alarm_status.upper() != AlarmStatus.DISABLED.value.upper(): + if metric_value is None: + log.warning("No metric result for alarm %s", alarm.uuid) + self.queue.put((alarm, AlarmStatus.INSUFFICIENT)) + else: + if alarm.operation.upper() == "GT": + if metric_value > alarm.threshold: + self.queue.put((alarm, AlarmStatus.ALARM)) + else: + self.queue.put((alarm, AlarmStatus.OK)) + elif alarm.operation.upper() == "LT": + if metric_value < alarm.threshold: + self.queue.put((alarm, AlarmStatus.ALARM)) + else: + self.queue.put((alarm, AlarmStatus.OK)) + + def update_alarm_status(self, alarm_state, uuid): + alarm_data = self.common_db.get_alarm_by_uuid(uuid) + if alarm_data.get("alarm_status").upper() != AlarmStatus.DISABLED.value.upper(): + self.common_db.update_alarm_status(alarm_state, uuid) + return def evaluate_alarms(self) -> List[Tuple[Alarm, AlarmStatus]]: log.debug("evaluate_alarms") diff --git a/osm_mon/server/server.py b/osm_mon/server/server.py index 07fdd22..62721ff 100755 --- a/osm_mon/server/server.py +++ b/osm_mon/server/server.py @@ -74,6 +74,7 @@ class Server: alarm_details["severity"].lower(), alarm_details["statistic"].lower(), alarm_details["metric_name"], + alarm_details["action"], alarm_details["tags"], ) response = response_builder.generate_response( diff --git a/osm_mon/server/service.py b/osm_mon/server/service.py index 94bdf4e..584a42f 100755 --- a/osm_mon/server/service.py +++ b/osm_mon/server/service.py @@ -41,11 +41,12 @@ class ServerService: severity: str, statistic: str, metric_name: str, + action: str, tags: dict, ) -> Alarm: log.debug("create_alarm") alarm = Alarm( - name, severity, threshold, operation, statistic, metric_name, tags + name, severity, threshold, operation, statistic, metric_name, action, tags ) self.common_db.create_alarm(alarm) log.info("Alarm %s created", alarm.name) diff --git a/osm_mon/tests/unit/core/test_common_db_client.py b/osm_mon/tests/unit/core/test_common_db_client.py index b639b7a..8ffc3d8 100644 --- a/osm_mon/tests/unit/core/test_common_db_client.py +++ b/osm_mon/tests/unit/core/test_common_db_client.py @@ -185,7 +185,7 @@ class CommonDbClientTest(unittest.TestCase): @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock()) @mock.patch.object(dbmongo.DbMongo, "create") def test_create_alarm(self, create): - alarm = Alarm("name", "severity", 50.0, "operation", "statistic", "metric", {}) + alarm = Alarm("name", "severity", 50.0, "operation", "statistic", "metric", "scale_out", {}, "ok") alarm.uuid = "1" common_db_client = CommonDbClient(self.config) common_db_client.create_alarm(alarm) @@ -200,6 +200,7 @@ class CommonDbClientTest(unittest.TestCase): "name": "name", "operation": "operation", "uuid": "1", + "alarm_status": "ok", }, )