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