blob: 732f8ac7699df08e2921f48fd5916ece8a87b93d [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:
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030038 def __init__(self, config: Config, loop=None):
39 self.conf = config
40 if not loop:
41 loop = asyncio.get_event_loop()
42 self.loop = loop
Benjamin Diaza97bdb32019-04-10 15:22:22 -030043 self.service = EvaluatorService(config)
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030044 self.msg_bus = MessageBusClient(config)
Benjamin Diaz51f44862018-11-15 10:27:12 -030045
Benjamin Diaz51f44862018-11-15 10:27:12 -030046 def evaluate_forever(self):
garciadeblas8e4179f2021-05-14 16:47:03 +020047 log.debug("evaluate_forever")
Benjamin Diaz51f44862018-11-15 10:27:12 -030048 while True:
49 try:
50 self.evaluate()
garciadeblas8e4179f2021-05-14 16:47:03 +020051 time.sleep(int(self.conf.get("evaluator", "interval")))
Benjamin Diaz51f44862018-11-15 10:27:12 -030052 except Exception:
53 log.exception("Error evaluating alarms")
54
55 def evaluate(self):
garciadeblas8e4179f2021-05-14 16:47:03 +020056 log.debug("evaluate")
57 log.info("Starting alarm evaluation")
Benjamin Diaza97bdb32019-04-10 15:22:22 -030058 alarms_tuples = self.service.evaluate_alarms()
Atul Agarwalcb6abac2021-03-26 11:14:25 +000059 processes = []
60 for alarm, status in alarms_tuples:
garciadeblas8e4179f2021-05-14 16:47:03 +020061 p = multiprocessing.Process(target=self.notify_alarm, args=(alarm, status))
Atul Agarwalcb6abac2021-03-26 11:14:25 +000062 p.start()
63 processes.append(p)
64 for process in processes:
65 process.join(timeout=10)
garciadeblas8e4179f2021-05-14 16:47:03 +020066 log.info("Alarm evaluation is complete")
Benjamin Diaz51f44862018-11-15 10:27:12 -030067
Atul Agarwalcb6abac2021-03-26 11:14:25 +000068 def notify_alarm(self, alarm: Alarm, status: AlarmStatus):
palsusc811d682021-02-09 17:03:49 +000069 log.debug("_notify_alarm")
Atul Agarwalcb6abac2021-03-26 11:14:25 +000070 resp_message = self._build_alarm_response(alarm, status)
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030071 log.info("Sent alarm notification: %s", resp_message)
garciadeblas8e4179f2021-05-14 16:47:03 +020072 self.loop.run_until_complete(
73 self.msg_bus.aiowrite("alarm_response", "notify_alarm", resp_message)
74 )
Atul Agarwal927a5842021-03-18 07:54:40 +000075 evaluator_service = EvaluatorService(self.conf)
76 evaluator_service.update_alarm_status(status.value, alarm.uuid)
77 return
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030078
Atul Agarwalcb6abac2021-03-26 11:14:25 +000079 def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus):
palsusc811d682021-02-09 17:03:49 +000080 log.debug("_build_alarm_response")
Benjamin Diaz51f44862018-11-15 10:27:12 -030081 response = ResponseBuilder()
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030082 tags = {}
Gianpietro Lavado1d71df52019-12-02 17:41:20 +000083 for name, value in alarm.tags.items():
84 tags[name] = value
Benjamin Diaz51f44862018-11-15 10:27:12 -030085 now = time.strftime("%d-%m-%Y") + " " + time.strftime("%X")
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030086 return response.generate_response(
garciadeblas8e4179f2021-05-14 16:47:03 +020087 "notify_alarm",
Benjamin Diazde3d5702018-11-22 17:27:35 -030088 alarm_id=alarm.uuid,
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030089 metric_name=alarm.metric,
Benjamin Diaz51f44862018-11-15 10:27:12 -030090 operation=alarm.operation,
91 threshold_value=alarm.threshold,
92 sev=alarm.severity,
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030093 status=status.value,
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030094 date=now,
garciadeblas8e4179f2021-05-14 16:47:03 +020095 tags=tags,
96 )