blob: 8d4c2767a91281acad96c2cacb3c060c91a50d78 [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
25import multiprocessing
26import 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')
Benjamin Diaza97bdb32019-04-10 15:22:22 -030058 alarms_tuples = self.service.evaluate_alarms()
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030059 processes = []
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030060 for alarm, status in alarms_tuples:
Benjamin Diaz51f44862018-11-15 10:27:12 -030061 p = multiprocessing.Process(target=self.notify_alarm,
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030062 args=(alarm, status))
Benjamin Diaz51f44862018-11-15 10:27:12 -030063 p.start()
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030064 processes.append(p)
65 for process in processes:
66 process.join(timeout=10)
Benjamin Diaz51f44862018-11-15 10:27:12 -030067
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030068 def notify_alarm(self, alarm: Alarm, status: AlarmStatus):
Benjamin Diaz058d51d2018-11-20 14:01:43 -030069 log.debug("notify_alarm")
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030070 resp_message = self._build_alarm_response(alarm, status)
71 log.info("Sent alarm notification: %s", resp_message)
72 self.loop.run_until_complete(self.msg_bus.aiowrite('alarm_response', 'notify_alarm', resp_message))
73
74 def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus):
Benjamin Diaz51f44862018-11-15 10:27:12 -030075 response = ResponseBuilder()
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030076 tags = {}
Gianpietro Lavado1d71df52019-12-02 17:41:20 +000077 for name, value in alarm.tags.items():
78 tags[name] = value
Benjamin Diaz51f44862018-11-15 10:27:12 -030079 now = time.strftime("%d-%m-%Y") + " " + time.strftime("%X")
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030080 return response.generate_response(
Benjamin Diaz51f44862018-11-15 10:27:12 -030081 'notify_alarm',
Benjamin Diazde3d5702018-11-22 17:27:35 -030082 alarm_id=alarm.uuid,
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030083 metric_name=alarm.metric,
Benjamin Diaz51f44862018-11-15 10:27:12 -030084 operation=alarm.operation,
85 threshold_value=alarm.threshold,
86 sev=alarm.severity,
Benjamin Diaz2bdf4022019-03-06 15:53:56 -030087 status=status.value,
Benjamin Diazd5ac6e12019-09-19 11:59:06 -030088 date=now,
89 tags=tags)