1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
26 import concurrent
.futures
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
34 log
= logging
.getLogger(__name__
)
39 def __init__(self
, config
: Config
, loop
=None):
42 loop
= asyncio
.get_event_loop()
44 self
.service
= EvaluatorService(config
)
45 self
.msg_bus
= MessageBusClient(config
)
47 def evaluate_forever(self
):
48 log
.debug('evaluate_forever')
52 time
.sleep(int(self
.conf
.get('evaluator', 'interval')))
54 log
.exception("Error evaluating alarms")
58 alarms_tuples
= self
.service
.evaluate_alarms()
59 # Starting evaluate executor pool with pool size process_pool_size. Default process_pool_size is 20
60 with concurrent
.futures
.ProcessPoolExecutor(self
.conf
.get('evaluator', 'process_pool_size')) as executor
:
61 log
.info('Started evaluate process pool with pool size %s' % (self
.conf
.get('evaluator',
62 'process_pool_size')))
64 for alarm
, status
in alarms_tuples
:
65 evaluate_futures
.append(executor
.submit(Evaluator
._notify
_alarm
, self
.conf
, alarm
, status
))
68 # Wait for future calls to complete till process_timeout. Default is 50 seconds
69 for evaluate_future
in concurrent
.futures
.as_completed(evaluate_futures
,
70 self
.conf
.get('evaluator', 'process_timeout')):
71 result
= evaluate_future
.result(timeout
=int(self
.conf
.get('evaluator',
73 log
.debug('result = %s' % (result
))
74 except concurrent
.futures
.TimeoutError
as e
:
75 # Some processes have not completed due to timeout error
76 log
.info('Some processes have not finished due to TimeoutError exception')
77 log
.debug('concurrent.futures.TimeoutError exception %s' % (e
))
78 Evaluator
._stop
_process
_pool
(executor
)
81 def _stop_process_pool(executor
):
82 log
.debug("_stop_process_pool")
83 log
.info('Stopping all processes in the process pool')
85 for pid
, process
in executor
._processes
.items():
86 if process
.is_alive():
88 except Exception as e
:
89 log
.info("Exception during process termination")
90 log
.debug("Exception %s" % (e
))
95 def _notify_alarm(conf
: Config
, alarm
: Alarm
, status
: AlarmStatus
):
96 log
.debug("_notify_alarm")
97 resp_message
= Evaluator
._build
_alarm
_response
(alarm
, status
)
98 msg_bus
= MessageBusClient(conf
)
99 loop
= asyncio
.get_event_loop()
100 log
.info("Sent alarm notification: %s", resp_message
)
101 loop
.run_until_complete(msg_bus
.aiowrite('alarm_response', 'notify_alarm', resp_message
))
105 def _build_alarm_response(alarm
: Alarm
, status
: AlarmStatus
):
106 log
.debug("_build_alarm_response")
107 response
= ResponseBuilder()
109 for name
, value
in alarm
.tags
.items():
111 now
= time
.strftime("%d-%m-%Y") + " " + time
.strftime("%X")
112 return response
.generate_response(
115 metric_name
=alarm
.metric
,
116 operation
=alarm
.operation
,
117 threshold_value
=alarm
.threshold
,