| 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 |
| 25 | import multiprocessing |
| 26 | import time |
| 27 | |
| Benjamin Diaz | 8303862 | 2019-01-28 19:03:39 -0300 | [diff] [blame] | 28 | import peewee |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 29 | |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 30 | from osm_mon.core.config import Config |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 31 | from osm_mon.core.database import Alarm |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 32 | from osm_mon.core.message_bus_client import MessageBusClient |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 33 | from osm_mon.core.response import ResponseBuilder |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 34 | from osm_mon.evaluator.service import EvaluatorService, AlarmStatus |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 35 | |
| 36 | log = logging.getLogger(__name__) |
| 37 | |
| 38 | |
| 39 | class Evaluator: |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 40 | |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 41 | def __init__(self, config: Config, loop=None): |
| 42 | self.conf = config |
| 43 | if not loop: |
| 44 | loop = asyncio.get_event_loop() |
| 45 | self.loop = loop |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 46 | self.service = EvaluatorService(config) |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 47 | self.msg_bus = MessageBusClient(config) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 48 | |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 49 | def evaluate_forever(self): |
| Benjamin Diaz | 058d51d | 2018-11-20 14:01:43 -0300 | [diff] [blame] | 50 | log.debug('evaluate_forever') |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 51 | while True: |
| 52 | try: |
| 53 | self.evaluate() |
| Benjamin Diaz | 5ac7c08 | 2019-02-06 11:58:00 -0300 | [diff] [blame] | 54 | time.sleep(int(self.conf.get('evaluator', 'interval'))) |
| Benjamin Diaz | 8303862 | 2019-01-28 19:03:39 -0300 | [diff] [blame] | 55 | except peewee.PeeweeException: |
| 56 | log.exception("Database error evaluating alarms: ") |
| 57 | raise |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 58 | except Exception: |
| 59 | log.exception("Error evaluating alarms") |
| 60 | |
| 61 | def evaluate(self): |
| Benjamin Diaz | 058d51d | 2018-11-20 14:01:43 -0300 | [diff] [blame] | 62 | log.debug('evaluate') |
| Benjamin Diaz | a97bdb3 | 2019-04-10 15:22:22 -0300 | [diff] [blame] | 63 | alarms_tuples = self.service.evaluate_alarms() |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 64 | for alarm, status in alarms_tuples: |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 65 | p = multiprocessing.Process(target=self.notify_alarm, |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 66 | args=(alarm, status)) |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 67 | p.start() |
| 68 | |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 69 | def notify_alarm(self, alarm: Alarm, status: AlarmStatus): |
| Benjamin Diaz | 058d51d | 2018-11-20 14:01:43 -0300 | [diff] [blame] | 70 | log.debug("notify_alarm") |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 71 | resp_message = self._build_alarm_response(alarm, status) |
| 72 | log.info("Sent alarm notification: %s", resp_message) |
| 73 | self.loop.run_until_complete(self.msg_bus.aiowrite('alarm_response', 'notify_alarm', resp_message)) |
| 74 | |
| 75 | def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus): |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 76 | response = ResponseBuilder() |
| 77 | now = time.strftime("%d-%m-%Y") + " " + time.strftime("%X") |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 78 | return response.generate_response( |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 79 | 'notify_alarm', |
| Benjamin Diaz | de3d570 | 2018-11-22 17:27:35 -0300 | [diff] [blame] | 80 | alarm_id=alarm.uuid, |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 81 | vdu_name=alarm.vdur_name, |
| 82 | vnf_member_index=alarm.vnf_member_index, |
| 83 | ns_id=alarm.nsr_id, |
| 84 | metric_name=alarm.monitoring_param, |
| 85 | operation=alarm.operation, |
| 86 | threshold_value=alarm.threshold, |
| 87 | sev=alarm.severity, |
| Benjamin Diaz | 2bdf402 | 2019-03-06 15:53:56 -0300 | [diff] [blame] | 88 | status=status.value, |
| Benjamin Diaz | 51f4486 | 2018-11-15 10:27:12 -0300 | [diff] [blame] | 89 | date=now) |