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
24 import multiprocessing
26 from typing
import Tuple
, List
28 from osm_mon
.core
.common_db
import CommonDbClient
29 from osm_mon
.core
.config
import Config
30 from osm_mon
.core
.models
import Alarm
31 from osm_mon
.evaluator
.backends
.prometheus
import PrometheusBackend
33 log
= logging
.getLogger(__name__
)
35 BACKENDS
= {"prometheus": PrometheusBackend
}
38 class AlarmStatus(Enum
):
41 INSUFFICIENT
= "insufficient-data"
45 class EvaluatorService
:
46 def __init__(self
, config
: Config
):
48 self
.common_db
= CommonDbClient(self
.conf
)
49 self
.queue
= multiprocessing
.Queue()
51 def _get_metric_value(self
, metric_name
: str, tags
: dict):
52 return BACKENDS
[self
.conf
.get("evaluator", "backend")](
54 ).get_metric_value(metric_name
, tags
)
56 def _evaluate_metric(self
, alarm
: Alarm
):
57 log
.debug("_evaluate_metric")
58 metric_value
= self
._get
_metric
_value
(alarm
.metric
, alarm
.tags
)
59 if alarm
.alarm_status
.upper() != AlarmStatus
.DISABLED
.value
.upper():
60 if metric_value
is None:
61 log
.warning("No metric result for alarm %s", alarm
.uuid
)
62 self
.queue
.put((alarm
, AlarmStatus
.INSUFFICIENT
))
64 if alarm
.operation
.upper() == "GT":
65 if metric_value
> alarm
.threshold
:
66 self
.queue
.put((alarm
, AlarmStatus
.ALARM
))
68 self
.queue
.put((alarm
, AlarmStatus
.OK
))
69 elif alarm
.operation
.upper() == "LT":
70 if metric_value
< alarm
.threshold
:
71 self
.queue
.put((alarm
, AlarmStatus
.ALARM
))
73 self
.queue
.put((alarm
, AlarmStatus
.OK
))
75 def update_alarm_status(self
, alarm_state
, uuid
):
76 alarm_data
= self
.common_db
.get_alarm_by_uuid(uuid
)
77 if alarm_data
.get("alarm_status").upper() != AlarmStatus
.DISABLED
.value
.upper():
78 self
.common_db
.update_alarm_status(alarm_state
, uuid
)
81 def evaluate_alarms(self
) -> List
[Tuple
[Alarm
, AlarmStatus
]]:
82 log
.debug("evaluate_alarms")
84 for alarm
in self
.common_db
.get_alarms():
85 p
= multiprocessing
.Process(target
=self
._evaluate
_metric
, args
=(alarm
,))
89 for process
in processes
:
90 process
.join(timeout
=10)
92 while not self
.queue
.empty():
93 alarms_tuples
.append(self
.queue
.get())