44b0af6d3b147489b6c4d6a94c6f26a2ed45efaa
1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 import multiprocessing
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
32 log
= logging
.getLogger(__name__
)
36 def __init__(self
, config
: Config
):
38 self
.service
= EvaluatorService(config
)
39 self
.msg_bus
= MessageBusClient(config
)
41 def evaluate_forever(self
):
42 log
.debug("evaluate_forever")
46 time
.sleep(int(self
.conf
.get("evaluator", "interval")))
48 log
.exception("Error evaluating alarms")
52 log
.info("Starting alarm evaluation")
53 alarms_tuples
= self
.service
.evaluate_alarms()
55 for alarm
, status
in alarms_tuples
:
56 p
= multiprocessing
.Process(target
=self
.notify_alarm
, args
=(alarm
, status
))
59 for process
in processes
:
60 process
.join(timeout
=10)
61 log
.info("Alarm evaluation is complete")
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
)
68 self
.msg_bus
.aiowrite("alarm_response", "notify_alarm", resp_message
)
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
)
75 def _build_alarm_response(self
, alarm
: Alarm
, status
: AlarmStatus
):
76 log
.debug("_build_alarm_response")
77 response
= ResponseBuilder()
79 for name
, value
in alarm
.tags
.items():
81 now
= time
.strftime("%d-%m-%Y") + " " + time
.strftime("%X")
82 return response
.generate_response(
85 metric_name
=alarm
.metric
,
86 operation
=alarm
.operation
,
87 threshold_value
=alarm
.threshold
,
92 extra_tabels
=alarm
.extra_labels
,