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_common
.dbbase
import DbException
30 from osm_mon
.core
import database
31 from osm_mon
.core
.common_db
import CommonDbClient
32 from osm_mon
.core
.config
import Config
33 from osm_mon
.core
.database
import Alarm
, AlarmRepository
34 from osm_mon
.evaluator
.backends
.prometheus
import PrometheusBackend
36 log
= logging
.getLogger(__name__
)
39 'prometheus': PrometheusBackend
43 class AlarmStatus(Enum
):
46 INSUFFICIENT
= 'insufficient-data'
49 class EvaluatorService
:
51 def __init__(self
, config
: Config
):
53 self
.common_db
= CommonDbClient(self
.conf
)
54 self
.queue
= multiprocessing
.Queue()
56 def _get_metric_value(self
,
58 vnf_member_index
: int,
61 return BACKENDS
[self
.conf
.get('evaluator', 'backend')]().get_metric_value(metric_name
, nsr_id
, vdur_name
,
64 def _evaluate_metric(self
,
66 vnf_member_index
: int,
70 log
.debug("_evaluate_metric")
71 metric_value
= self
._get
_metric
_value
(nsr_id
, vnf_member_index
, vdur_name
, metric_name
)
73 log
.warning("No metric result for alarm %s", alarm
.id)
74 self
.queue
.put((alarm
, AlarmStatus
.INSUFFICIENT
))
76 if alarm
.operation
.upper() == 'GT':
77 if metric_value
> alarm
.threshold
:
78 self
.queue
.put((alarm
, AlarmStatus
.ALARM
))
80 self
.queue
.put((alarm
, AlarmStatus
.OK
))
81 elif alarm
.operation
.upper() == 'LT':
82 if metric_value
< alarm
.threshold
:
83 self
.queue
.put((alarm
, AlarmStatus
.ALARM
))
85 self
.queue
.put((alarm
, AlarmStatus
.OK
))
87 def evaluate_alarms(self
) -> List
[Tuple
[Alarm
, AlarmStatus
]]:
88 log
.debug('evaluate_alarms')
92 with database
.db
.atomic():
93 for alarm
in AlarmRepository
.list():
95 vnfr
= self
.common_db
.get_vnfr(alarm
.nsr_id
, alarm
.vnf_member_index
)
97 log
.exception("Error getting vnfr: ")
99 vnfd
= self
.common_db
.get_vnfd(vnfr
['vnfd-id'])
101 vdur
= next(filter(lambda vdur
: vdur
['name'] == alarm
.vdur_name
, vnfr
['vdur']))
102 except StopIteration:
103 log
.warning("No vdur found with name %s for alarm %s", alarm
.vdur_name
, alarm
.id)
105 vdu
= next(filter(lambda vdu
: vdu
['id'] == vdur
['vdu-id-ref'], vnfd
['vdu']))
106 vnf_monitoring_param
= next(
107 filter(lambda param
: param
['id'] == alarm
.monitoring_param
, vnfd
['monitoring-param']))
108 nsr_id
= vnfr
['nsr-id-ref']
109 vnf_member_index
= vnfr
['member-vnf-index-ref']
110 vdur_name
= vdur
['name']
111 if 'vdu-monitoring-param' in vnf_monitoring_param
:
112 vdu_monitoring_param
= next(filter(
113 lambda param
: param
['id'] == vnf_monitoring_param
['vdu-monitoring-param'][
114 'vdu-monitoring-param-ref'], vdu
['monitoring-param']))
115 nfvi_metric
= vdu_monitoring_param
['nfvi-metric']
117 p
= multiprocessing
.Process(target
=self
._evaluate
_metric
,
125 if 'vdu-metric' in vnf_monitoring_param
:
126 vnf_metric_name
= vnf_monitoring_param
['vdu-metric']['vdu-metric-name-ref']
127 p
= multiprocessing
.Process(target
=self
._evaluate
_metric
,
135 if 'vnf-metric' in vnf_monitoring_param
:
136 vnf_metric_name
= vnf_monitoring_param
['vnf-metric']['vnf-metric-name-ref']
137 p
= multiprocessing
.Process(target
=self
._evaluate
_metric
,
146 for process
in processes
:
147 process
.join(timeout
=10)
149 while not self
.queue
.empty():
150 alarms_tuples
.append(self
.queue
.get())