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
import database
29 from osm_mon
.core
.common_db
import CommonDbClient
30 from osm_mon
.core
.config
import Config
31 from osm_mon
.core
.database
import Alarm
, AlarmRepository
32 from osm_mon
.evaluator
.backends
.prometheus
import PrometheusBackend
34 log
= logging
.getLogger(__name__
)
37 'prometheus': PrometheusBackend
41 class AlarmStatus(Enum
):
44 INSUFFICIENT
= 'insufficient-data'
47 class EvaluatorService
:
49 def __init__(self
, config
: Config
):
51 self
.common_db
= CommonDbClient(self
.conf
)
52 self
.queue
= multiprocessing
.Queue()
54 def _get_metric_value(self
,
57 return BACKENDS
[self
.conf
.get('evaluator', 'backend')](self
.conf
).get_metric_value(metric_name
, tags
)
59 def _evaluate_metric(self
,
60 alarm
: Alarm
, tags
: dict):
61 log
.debug("_evaluate_metric")
62 metric_value
= self
._get
_metric
_value
(alarm
.metric
, tags
)
63 if metric_value
is None:
64 log
.warning("No metric result for alarm %s", alarm
.id)
65 self
.queue
.put((alarm
, AlarmStatus
.INSUFFICIENT
))
67 if alarm
.operation
.upper() == 'GT':
68 if metric_value
> alarm
.threshold
:
69 self
.queue
.put((alarm
, AlarmStatus
.ALARM
))
71 self
.queue
.put((alarm
, AlarmStatus
.OK
))
72 elif alarm
.operation
.upper() == 'LT':
73 if metric_value
< alarm
.threshold
:
74 self
.queue
.put((alarm
, AlarmStatus
.ALARM
))
76 self
.queue
.put((alarm
, AlarmStatus
.OK
))
78 def evaluate_alarms(self
) -> List
[Tuple
[Alarm
, AlarmStatus
]]:
79 log
.debug('evaluate_alarms')
83 with database
.db
.atomic():
84 for alarm
in AlarmRepository
.list():
85 # Tags need to be passed inside a dict to avoid database locking issues related to process forking
87 for tag
in alarm
.tags
:
88 tags
[tag
.name
] = tag
.value
89 p
= multiprocessing
.Process(target
=self
._evaluate
_metric
,
94 for process
in processes
:
95 process
.join(timeout
=10)
97 log
.info("Appending alarms to queue")
98 while not self
.queue
.empty():
99 alarms_tuples
.append(self
.queue
.get())