Feature 10339 - Enhanced Alarm Mgmt. (SOL005 FM Interface)
[osm/MON.git] / osm_mon / evaluator / evaluator.py
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 ##
23 import asyncio
24 import logging
25 import multiprocessing
26 import time
27
28 from osm_mon.core.config import Config
29 from osm_mon.core.message_bus_client import MessageBusClient
30 from osm_mon.core.models import Alarm
31 from osm_mon.core.response import ResponseBuilder
32 from osm_mon.evaluator.service import EvaluatorService, AlarmStatus
33
34 log = logging.getLogger(__name__)
35
36
37 class Evaluator:
38 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
43 self.service = EvaluatorService(config)
44 self.msg_bus = MessageBusClient(config)
45
46 def evaluate_forever(self):
47 log.debug("evaluate_forever")
48 while True:
49 try:
50 self.evaluate()
51 time.sleep(int(self.conf.get("evaluator", "interval")))
52 except Exception:
53 log.exception("Error evaluating alarms")
54
55 def evaluate(self):
56 log.debug("evaluate")
57 log.info("Starting alarm evaluation")
58 alarms_tuples = self.service.evaluate_alarms()
59 processes = []
60 for alarm, status in alarms_tuples:
61 p = multiprocessing.Process(target=self.notify_alarm, args=(alarm, status))
62 p.start()
63 processes.append(p)
64 for process in processes:
65 process.join(timeout=10)
66 log.info("Alarm evaluation is complete")
67
68 def notify_alarm(self, alarm: Alarm, status: AlarmStatus):
69 log.debug("_notify_alarm")
70 resp_message = self._build_alarm_response(alarm, status)
71 log.info("Sent alarm notification: %s", resp_message)
72 self.loop.run_until_complete(
73 self.msg_bus.aiowrite("alarm_response", "notify_alarm", resp_message)
74 )
75 evaluator_service = EvaluatorService(self.conf)
76 evaluator_service.update_alarm_status(status.value, alarm.uuid)
77 return
78
79 def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus):
80 log.debug("_build_alarm_response")
81 response = ResponseBuilder()
82 tags = {}
83 for name, value in alarm.tags.items():
84 tags[name] = value
85 now = time.strftime("%d-%m-%Y") + " " + time.strftime("%X")
86 return response.generate_response(
87 "notify_alarm",
88 alarm_id=alarm.uuid,
89 metric_name=alarm.metric,
90 operation=alarm.operation,
91 threshold_value=alarm.threshold,
92 sev=alarm.severity,
93 status=status.value,
94 date=now,
95 tags=tags,
96 )