Coverage for osm_mon/evaluator/evaluator.py: 58%

52 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-06 19:04 +0000

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## 

21import asyncio 

22import logging 

23import multiprocessing 

24import time 

25 

26from osm_mon.core.config import Config 

27from osm_mon.core.message_bus_client import MessageBusClient 

28from osm_mon.core.models import Alarm 

29from osm_mon.core.response import ResponseBuilder 

30from osm_mon.evaluator.service import EvaluatorService, AlarmStatus 

31 

32log = logging.getLogger(__name__) 

33 

34 

35class 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 )