blob: 61b788a0dfb2c5e2954bac01122bcca1c4035254 [file] [log] [blame]
Benjamin Diaz51f44862018-11-15 10:27:12 -03001# -*- 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 Diaz5ac7c082019-02-06 11:58:00 -030023import asyncio
Benjamin Diaz51f44862018-11-15 10:27:12 -030024import logging
Atul Agarwalcb6abac2021-03-26 11:14:25 +000025import multiprocessing
Benjamin Diaz51f44862018-11-15 10:27:12 -030026import time
27
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030028from osm_mon.core.config import Config
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030029from osm_mon.core.message_bus_client import MessageBusClient
almagia1b7145f2019-11-30 03:56:04 +010030from osm_mon.core.models import Alarm
Benjamin Diaz51f44862018-11-15 10:27:12 -030031from osm_mon.core.response import ResponseBuilder
Benjamin Diaza97bdb32019-04-10 15:22:22 -030032from osm_mon.evaluator.service import EvaluatorService, AlarmStatus
Benjamin Diaz51f44862018-11-15 10:27:12 -030033
34log = logging.getLogger(__name__)
35
36
37class Evaluator:
Dario Faccin23d9af72023-05-24 16:27:54 +020038 def __init__(self, config: Config):
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030039 self.conf = config
Benjamin Diaza97bdb32019-04-10 15:22:22 -030040 self.service = EvaluatorService(config)
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030041 self.msg_bus = MessageBusClient(config)
Benjamin Diaz51f44862018-11-15 10:27:12 -030042
Benjamin Diaz51f44862018-11-15 10:27:12 -030043 def evaluate_forever(self):
garciadeblas8e4179f2021-05-14 16:47:03 +020044 log.debug("evaluate_forever")
Benjamin Diaz51f44862018-11-15 10:27:12 -030045 while True:
46 try:
47 self.evaluate()
garciadeblas8e4179f2021-05-14 16:47:03 +020048 time.sleep(int(self.conf.get("evaluator", "interval")))
Benjamin Diaz51f44862018-11-15 10:27:12 -030049 except Exception:
50 log.exception("Error evaluating alarms")
51
52 def evaluate(self):
garciadeblas8e4179f2021-05-14 16:47:03 +020053 log.debug("evaluate")
54 log.info("Starting alarm evaluation")
Benjamin Diaza97bdb32019-04-10 15:22:22 -030055 alarms_tuples = self.service.evaluate_alarms()
Atul Agarwalcb6abac2021-03-26 11:14:25 +000056 processes = []
57 for alarm, status in alarms_tuples:
garciadeblas8e4179f2021-05-14 16:47:03 +020058 p = multiprocessing.Process(target=self.notify_alarm, args=(alarm, status))
Atul Agarwalcb6abac2021-03-26 11:14:25 +000059 p.start()
60 processes.append(p)
61 for process in processes:
62 process.join(timeout=10)
garciadeblas8e4179f2021-05-14 16:47:03 +020063 log.info("Alarm evaluation is complete")
Benjamin Diaz51f44862018-11-15 10:27:12 -030064
Atul Agarwalcb6abac2021-03-26 11:14:25 +000065 def notify_alarm(self, alarm: Alarm, status: AlarmStatus):
palsusc811d682021-02-09 17:03:49 +000066 log.debug("_notify_alarm")
Atul Agarwalcb6abac2021-03-26 11:14:25 +000067 resp_message = self._build_alarm_response(alarm, status)
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030068 log.info("Sent alarm notification: %s", resp_message)
Dario Faccin23d9af72023-05-24 16:27:54 +020069 asyncio.run(
garciadeblas8e4179f2021-05-14 16:47:03 +020070 self.msg_bus.aiowrite("alarm_response", "notify_alarm", resp_message)
71 )
Atul Agarwal927a5842021-03-18 07:54:40 +000072 evaluator_service = EvaluatorService(self.conf)
73 evaluator_service.update_alarm_status(status.value, alarm.uuid)
74 return
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030075
Atul Agarwalcb6abac2021-03-26 11:14:25 +000076 def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus):
palsusc811d682021-02-09 17:03:49 +000077 log.debug("_build_alarm_response")
Benjamin Diaz51f44862018-11-15 10:27:12 -030078 response = ResponseBuilder()
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030079 tags = {}
Gianpietro Lavado1d71df52019-12-02 17:41:20 +000080 for name, value in alarm.tags.items():
81 tags[name] = value
Benjamin Diaz51f44862018-11-15 10:27:12 -030082 now = time.strftime("%d-%m-%Y") + " " + time.strftime("%X")
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030083 return response.generate_response(
garciadeblas8e4179f2021-05-14 16:47:03 +020084 "notify_alarm",
Benjamin Diazde3d5702018-11-22 17:27:35 -030085 alarm_id=alarm.uuid,
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030086 metric_name=alarm.metric,
Benjamin Diaz51f44862018-11-15 10:27:12 -030087 operation=alarm.operation,
88 threshold_value=alarm.threshold,
89 sev=alarm.severity,
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030090 status=status.value,
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030091 date=now,
garciadeblas8e4179f2021-05-14 16:47:03 +020092 tags=tags,
93 )