| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -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 | ## |
| 23 | import logging |
| 24 | import multiprocessing |
| 25 | from enum import Enum |
| 26 | from typing import Tuple, List |
| 27 | |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 28 | from osm_mon.core.common_db import CommonDbClient |
| 29 | from osm_mon.core.config import Config |
| almagia | 1b7145f | 2019-11-30 03:56:04 +0100 | [diff] [blame] | 30 | from osm_mon.core.models import Alarm |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 31 | from osm_mon.evaluator.backends.prometheus import PrometheusBackend |
| 32 | |
| 33 | log = logging.getLogger(__name__) |
| 34 | |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 35 | BACKENDS = {"prometheus": PrometheusBackend} |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 36 | |
| 37 | |
| 38 | class AlarmStatus(Enum): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 39 | ALARM = "alarm" |
| 40 | OK = "ok" |
| 41 | INSUFFICIENT = "insufficient-data" |
| Atul Agarwal | 927a584 | 2021-03-18 07:54:40 +0000 | [diff] [blame] | 42 | DISABLED = "disabled" |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 43 | |
| 44 | |
| 45 | class EvaluatorService: |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 46 | def __init__(self, config: Config): |
| 47 | self.conf = config |
| 48 | self.common_db = CommonDbClient(self.conf) |
| 49 | self.queue = multiprocessing.Queue() |
| 50 | |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 51 | def _get_metric_value(self, metric_name: str, tags: dict): |
| 52 | return BACKENDS[self.conf.get("evaluator", "backend")]( |
| 53 | self.conf |
| 54 | ).get_metric_value(metric_name, tags) |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 55 | |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 56 | def _evaluate_metric(self, alarm: Alarm): |
| Pedro Escaleira | 9ff497c | 2022-06-14 13:27:32 +0100 | [diff] [blame] | 57 | """Method to evaluate a metric value comparing it against an alarm threshold. |
| 58 | |
| 59 | Args: |
| 60 | alarm (Alarm): the alarm with the threshold to compare the metric against |
| 61 | """ |
| 62 | |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 63 | log.debug("_evaluate_metric") |
| almagia | 1b7145f | 2019-11-30 03:56:04 +0100 | [diff] [blame] | 64 | metric_value = self._get_metric_value(alarm.metric, alarm.tags) |
| Atul Agarwal | 927a584 | 2021-03-18 07:54:40 +0000 | [diff] [blame] | 65 | if alarm.alarm_status.upper() != AlarmStatus.DISABLED.value.upper(): |
| 66 | if metric_value is None: |
| 67 | log.warning("No metric result for alarm %s", alarm.uuid) |
| 68 | self.queue.put((alarm, AlarmStatus.INSUFFICIENT)) |
| 69 | else: |
| Pedro Escaleira | 9ff497c | 2022-06-14 13:27:32 +0100 | [diff] [blame] | 70 | if ( |
| 71 | (alarm.operation.upper() == "GT" and metric_value > alarm.threshold) |
| 72 | or ( |
| 73 | alarm.operation.upper() == "LT" |
| 74 | and metric_value < alarm.threshold |
| 75 | ) |
| 76 | or ( |
| 77 | alarm.operation.upper() == "GE" |
| 78 | and metric_value >= alarm.threshold |
| 79 | ) |
| 80 | or ( |
| 81 | alarm.operation.upper() == "LE" |
| 82 | and metric_value <= alarm.threshold |
| 83 | ) |
| 84 | or ( |
| 85 | alarm.operation.upper() == "EQ" |
| 86 | and metric_value == alarm.threshold |
| 87 | ) |
| 88 | or ( |
| 89 | alarm.operation.upper() == "NE" |
| 90 | and metric_value != alarm.threshold |
| 91 | ) |
| 92 | ): |
| 93 | self.queue.put((alarm, AlarmStatus.ALARM)) |
| 94 | elif alarm.operation.upper() in ("GT", "LT", "GE", "LE", "EQ", "NE"): |
| 95 | self.queue.put((alarm, AlarmStatus.OK)) |
| Atul Agarwal | 927a584 | 2021-03-18 07:54:40 +0000 | [diff] [blame] | 96 | |
| 97 | def update_alarm_status(self, alarm_state, uuid): |
| 98 | alarm_data = self.common_db.get_alarm_by_uuid(uuid) |
| 99 | if alarm_data.get("alarm_status").upper() != AlarmStatus.DISABLED.value.upper(): |
| 100 | self.common_db.update_alarm_status(alarm_state, uuid) |
| 101 | return |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 102 | |
| 103 | def evaluate_alarms(self) -> List[Tuple[Alarm, AlarmStatus]]: |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 104 | log.debug("evaluate_alarms") |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 105 | processes = [] |
| almagia | 1b7145f | 2019-11-30 03:56:04 +0100 | [diff] [blame] | 106 | for alarm in self.common_db.get_alarms(): |
| garciadeblas | 8e4179f | 2021-05-14 16:47:03 +0200 | [diff] [blame] | 107 | p = multiprocessing.Process(target=self._evaluate_metric, args=(alarm,)) |
| almagia | 1b7145f | 2019-11-30 03:56:04 +0100 | [diff] [blame] | 108 | processes.append(p) |
| 109 | p.start() |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 110 | |
| almagia | 1b7145f | 2019-11-30 03:56:04 +0100 | [diff] [blame] | 111 | for process in processes: |
| 112 | process.join(timeout=10) |
| 113 | alarms_tuples = [] |
| 114 | while not self.queue.empty(): |
| 115 | alarms_tuples.append(self.queue.get()) |
| 116 | return alarms_tuples |