44b0af6d3b147489b6c4d6a94c6f26a2ed45efaa
[osm/MON.git] / osm_mon / evaluator / evaluator.py
1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact: bdiaz@whitestack.com or glavado@whitestack.com
20 ##
21 import asyncio
22 import logging
23 import multiprocessing
24 import time
25
26 from osm_mon.core.config import Config
27 from osm_mon.core.message_bus_client import MessageBusClient
28 from osm_mon.core.models import Alarm
29 from osm_mon.core.response import ResponseBuilder
30 from osm_mon.evaluator.service import EvaluatorService, AlarmStatus
31
32 log = logging.getLogger(__name__)
33
34
35 class Evaluator:
36 def __init__(self, config: Config):
37 self.conf = config
38 self.service = EvaluatorService(config)
39 self.msg_bus = MessageBusClient(config)
40
41 def evaluate_forever(self):
42 log.debug("evaluate_forever")
43 while True:
44 try:
45 self.evaluate()
46 time.sleep(int(self.conf.get("evaluator", "interval")))
47 except Exception:
48 log.exception("Error evaluating alarms")
49
50 def evaluate(self):
51 log.debug("evaluate")
52 log.info("Starting alarm evaluation")
53 alarms_tuples = self.service.evaluate_alarms()
54 processes = []
55 for alarm, status in alarms_tuples:
56 p = multiprocessing.Process(target=self.notify_alarm, args=(alarm, status))
57 p.start()
58 processes.append(p)
59 for process in processes:
60 process.join(timeout=10)
61 log.info("Alarm evaluation is complete")
62
63 def notify_alarm(self, alarm: Alarm, status: AlarmStatus):
64 log.debug("_notify_alarm")
65 resp_message = self._build_alarm_response(alarm, status)
66 log.info("Sent alarm notification: %s", resp_message)
67 asyncio.run(
68 self.msg_bus.aiowrite("alarm_response", "notify_alarm", resp_message)
69 )
70 evaluator_service = EvaluatorService(self.conf)
71 evaluator_service.update_alarm_status(status.value, alarm.uuid)
72 evaluator_service.update_alarm_extra_labels(alarm.extra_labels, alarm.uuid)
73 return
74
75 def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus):
76 log.debug("_build_alarm_response")
77 response = ResponseBuilder()
78 tags = {}
79 for name, value in alarm.tags.items():
80 tags[name] = value
81 now = time.strftime("%d-%m-%Y") + " " + time.strftime("%X")
82 return response.generate_response(
83 "notify_alarm",
84 alarm_id=alarm.uuid,
85 metric_name=alarm.metric,
86 operation=alarm.operation,
87 threshold_value=alarm.threshold,
88 sev=alarm.severity,
89 status=status.value,
90 date=now,
91 tags=tags,
92 extra_tabels=alarm.extra_labels,
93 )