blob: 0b4efdee44ea6e956cd29f59cceb12b5704243e0 [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 Diaz2bdf4022019-03-06 15:53:56 -030038
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030039 def __init__(self, config: Config, loop=None):
40 self.conf = config
41 if not loop:
42 loop = asyncio.get_event_loop()
43 self.loop = loop
Benjamin Diaza97bdb32019-04-10 15:22:22 -030044 self.service = EvaluatorService(config)
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030045 self.msg_bus = MessageBusClient(config)
Benjamin Diaz51f44862018-11-15 10:27:12 -030046
Benjamin Diaz51f44862018-11-15 10:27:12 -030047 def evaluate_forever(self):
Benjamin Diaz058d51d2018-11-20 14:01:43 -030048 log.debug('evaluate_forever')
Benjamin Diaz51f44862018-11-15 10:27:12 -030049 while True:
50 try:
51 self.evaluate()
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030052 time.sleep(int(self.conf.get('evaluator', 'interval')))
Benjamin Diaz51f44862018-11-15 10:27:12 -030053 except Exception:
54 log.exception("Error evaluating alarms")
55
56 def evaluate(self):
Benjamin Diaz058d51d2018-11-20 14:01:43 -030057 log.debug('evaluate')
Atul Agarwalcb6abac2021-03-26 11:14:25 +000058 log.info('Starting alarm evaluation')
Benjamin Diaza97bdb32019-04-10 15:22:22 -030059 alarms_tuples = self.service.evaluate_alarms()
Atul Agarwalcb6abac2021-03-26 11:14:25 +000060 processes = []
61 for alarm, status in alarms_tuples:
62 p = multiprocessing.Process(target=self.notify_alarm,
63 args=(alarm, status))
64 p.start()
65 processes.append(p)
66 for process in processes:
67 process.join(timeout=10)
68 log.info('Alarm evaluation is complete')
Benjamin Diaz51f44862018-11-15 10:27:12 -030069
Atul Agarwalcb6abac2021-03-26 11:14:25 +000070 def notify_alarm(self, alarm: Alarm, status: AlarmStatus):
palsusc811d682021-02-09 17:03:49 +000071 log.debug("_notify_alarm")
Atul Agarwalcb6abac2021-03-26 11:14:25 +000072 resp_message = self._build_alarm_response(alarm, status)
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030073 log.info("Sent alarm notification: %s", resp_message)
Atul Agarwalcb6abac2021-03-26 11:14:25 +000074 self.loop.run_until_complete(self.msg_bus.aiowrite('alarm_response', 'notify_alarm', resp_message))
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(
Benjamin Diaz51f44862018-11-15 10:27:12 -030084 '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,
92 tags=tags)