| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 1 | # Copyright 2018 Whitestack, LLC |
| 2 | # ************************************************************* |
| 3 | |
| 4 | # This file is part of OSM Monitoring module |
| 5 | # All Rights Reserved to Whitestack, LLC |
| 6 | |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | # not use this file except in compliance with the License. You may obtain |
| 9 | # a copy of the License at |
| 10 | |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | # License for the specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # For those usages not covered by the Apache License, Version 2.0 please |
| 19 | # contact: bdiaz@whitestack.com or glavado@whitestack.com |
| 20 | ## |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 21 | import asyncio |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 22 | import logging |
| Atul Agarwal | cb6abac | 2021-03-26 11:14:25 +0000 | [diff] [blame] | 23 | import multiprocessing |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 24 | import time |
| 25 | |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 26 | from osm_mon.core.config import Config |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 27 | from osm_mon.core.message_bus_client import MessageBusClient |
| almagia | 1b7145f | 2019-11-30 03:56:04 +0100 | [diff] [blame] | 28 | from osm_mon.core.models import Alarm |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 29 | from osm_mon.core.response import ResponseBuilder |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 30 | from osm_mon.evaluator.service import EvaluatorService, AlarmStatus |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 31 | |
| 32 | log = logging.getLogger(__name__) |
| 33 | |
| 34 | |
| 35 | class Evaluator: |
| Mark Beierl | 072b0bd | 2023-05-10 15:43:03 -0400 | [diff] [blame] | 36 | def __init__(self, config: Config): |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 37 | self.conf = config |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 38 | self.service = EvaluatorService(config) |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 39 | self.msg_bus = MessageBusClient(config) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 40 | |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 41 | def evaluate_forever(self): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 42 | log.debug("evaluate_forever") |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 43 | while True: |
| 44 | try: |
| 45 | self.evaluate() |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 46 | time.sleep(int(self.conf.get("evaluator", "interval"))) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 47 | except Exception: |
| 48 | log.exception("Error evaluating alarms") |
| 49 | |
| 50 | def evaluate(self): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 51 | log.debug("evaluate") |
| 52 | log.info("Starting alarm evaluation") |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 53 | alarms_tuples = self.service.evaluate_alarms() |
| Atul Agarwal | cb6abac | 2021-03-26 11:14:25 +0000 | [diff] [blame] | 54 | processes = [] |
| 55 | for alarm, status in alarms_tuples: |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 56 | p = multiprocessing.Process(target=self.notify_alarm, args=(alarm, status)) |
| Atul Agarwal | cb6abac | 2021-03-26 11:14:25 +0000 | [diff] [blame] | 57 | p.start() |
| 58 | processes.append(p) |
| 59 | for process in processes: |
| 60 | process.join(timeout=10) |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 61 | log.info("Alarm evaluation is complete") |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 62 | |
| Atul Agarwal | cb6abac | 2021-03-26 11:14:25 +0000 | [diff] [blame] | 63 | def notify_alarm(self, alarm: Alarm, status: AlarmStatus): |
| palsus | c811d68 | 2021-02-09 17:03:49 +0000 | [diff] [blame] | 64 | log.debug("_notify_alarm") |
| Atul Agarwal | cb6abac | 2021-03-26 11:14:25 +0000 | [diff] [blame] | 65 | resp_message = self._build_alarm_response(alarm, status) |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 66 | log.info("Sent alarm notification: %s", resp_message) |
| Mark Beierl | 072b0bd | 2023-05-10 15:43:03 -0400 | [diff] [blame] | 67 | asyncio.run( |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 68 | self.msg_bus.aiowrite("alarm_response", "notify_alarm", resp_message) |
| 69 | ) |
| Atul Agarwal | 927a584 | 2021-03-18 07:54:40 +0000 | [diff] [blame] | 70 | evaluator_service = EvaluatorService(self.conf) |
| 71 | evaluator_service.update_alarm_status(status.value, alarm.uuid) |
| Daniel Gomes | b317322 | 2023-02-10 17:39:56 +0000 | [diff] [blame^] | 72 | evaluator_service.update_alarm_extra_labels(alarm.extra_labels, alarm.uuid) |
| Atul Agarwal | 927a584 | 2021-03-18 07:54:40 +0000 | [diff] [blame] | 73 | return |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 74 | |
| Atul Agarwal | cb6abac | 2021-03-26 11:14:25 +0000 | [diff] [blame] | 75 | def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus): |
| palsus | c811d68 | 2021-02-09 17:03:49 +0000 | [diff] [blame] | 76 | log.debug("_build_alarm_response") |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 77 | response = ResponseBuilder() |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 78 | tags = {} |
| Gianpietro Lavado | 1d71df5 | 2019-12-02 17:41:20 +0000 | [diff] [blame] | 79 | for name, value in alarm.tags.items(): |
| 80 | tags[name] = value |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 81 | now = time.strftime("%d-%m-%Y") + " " + time.strftime("%X") |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 82 | return response.generate_response( |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 83 | "notify_alarm", |
| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 84 | alarm_id=alarm.uuid, |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 85 | metric_name=alarm.metric, |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 86 | operation=alarm.operation, |
| 87 | threshold_value=alarm.threshold, |
| 88 | sev=alarm.severity, |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 89 | status=status.value, |
| Benjamin Diaz | d5ac6e1 | 2019-09-19 11:59:06 -0300 | [diff] [blame] | 90 | date=now, |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 91 | tags=tags, |
| Daniel Gomes | b317322 | 2023-02-10 17:39:56 +0000 | [diff] [blame^] | 92 | extra_tabels=alarm.extra_labels, |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 93 | ) |