blob: 76881b95fd3b2d56e3a3a205c5a1898ea1915736 [file] [log] [blame]
Benjamin Diaz51f44862018-11-15 10:27:12 -03001# -*- coding: utf-8 -*-
2
3# Copyright 2018 Whitestack, LLC
4# *************************************************************
5
6# This file is part of OSM Monitoring module
7# All Rights Reserved to Whitestack, LLC
8
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
12
13# http://www.apache.org/licenses/LICENSE-2.0
14
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
19# under the License.
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact: bdiaz@whitestack.com or glavado@whitestack.com
22##
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030023import asyncio
Benjamin Diaz51f44862018-11-15 10:27:12 -030024import logging
25import multiprocessing
26import time
27
Benjamin Diaz83038622019-01-28 19:03:39 -030028import peewee
Benjamin Diaz058d51d2018-11-20 14:01:43 -030029import requests
Benjamin Diaz51f44862018-11-15 10:27:12 -030030from osm_common.dbbase import DbException
31
Benjamin Diaz058d51d2018-11-20 14:01:43 -030032from osm_mon.collector.backends.prometheus import OSM_METRIC_PREFIX
Benjamin Diaz51f44862018-11-15 10:27:12 -030033from osm_mon.core.common_db import CommonDbClient
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030034from osm_mon.core.config import Config
Benjamin Diaz51f44862018-11-15 10:27:12 -030035from osm_mon.core.database import DatabaseManager, Alarm
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030036from osm_mon.core.message_bus_client import MessageBusClient
Benjamin Diaz51f44862018-11-15 10:27:12 -030037from osm_mon.core.response import ResponseBuilder
Benjamin Diaz51f44862018-11-15 10:27:12 -030038
39log = logging.getLogger(__name__)
40
41
42class Evaluator:
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030043 def __init__(self, config: Config, loop=None):
44 self.conf = config
45 if not loop:
46 loop = asyncio.get_event_loop()
47 self.loop = loop
48 self.common_db = CommonDbClient(self.conf)
Benjamin Diaz51f44862018-11-15 10:27:12 -030049 self.plugins = []
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030050 self.database_manager = DatabaseManager(self.conf)
Benjamin Diaz51f44862018-11-15 10:27:12 -030051 self.database_manager.create_tables()
52 self.queue = multiprocessing.Queue()
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030053 self.msg_bus = MessageBusClient(config)
Benjamin Diaz51f44862018-11-15 10:27:12 -030054
Benjamin Diaz058d51d2018-11-20 14:01:43 -030055 def _evaluate_metric(self,
56 nsr_id: str,
57 vnf_member_index: int,
58 vdur_name: str,
59 metric_name: str,
60 alarm: Alarm):
61 log.debug("_evaluate_metric")
62 # TODO: Refactor to fit backend plugin model
Benjamin Diaz058d51d2018-11-20 14:01:43 -030063 query_section = "query={0}{{ns_id=\"{1}\",vdu_name=\"{2}\",vnf_member_index=\"{3}\"}}".format(
64 OSM_METRIC_PREFIX + metric_name, nsr_id, vdur_name, vnf_member_index)
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030065 request_url = self.conf.get('prometheus', 'url') + "/api/v1/query?" + query_section
Benjamin Diaz058d51d2018-11-20 14:01:43 -030066 log.info("Querying Prometheus: %s", request_url)
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030067 r = requests.get(request_url, timeout=int(self.conf.get('global', 'request_timeout')))
Benjamin Diaz058d51d2018-11-20 14:01:43 -030068 if r.status_code == 200:
69 json_response = r.json()
70 if json_response['status'] == 'success':
71 result = json_response['data']['result']
72 if len(result):
73 metric_value = float(result[0]['value'][1])
74 log.info("Metric value: %s", metric_value)
75 if alarm.operation.upper() == 'GT':
76 if metric_value > alarm.threshold:
77 self.queue.put(alarm)
78 elif alarm.operation.upper() == 'LT':
79 if metric_value < alarm.threshold:
80 self.queue.put(alarm)
81 else:
82 log.warning("No metric result for alarm %s", alarm.id)
83 else:
84 log.warning("Prometheus response is not success. Got status %s", json_response['status'])
Benjamin Diaz51f44862018-11-15 10:27:12 -030085 else:
Benjamin Diaz058d51d2018-11-20 14:01:43 -030086 log.warning("Error contacting Prometheus. Got status code %s: %s", r.status_code, r.text)
Benjamin Diaz51f44862018-11-15 10:27:12 -030087
88 def evaluate_forever(self):
Benjamin Diaz058d51d2018-11-20 14:01:43 -030089 log.debug('evaluate_forever')
Benjamin Diaz51f44862018-11-15 10:27:12 -030090 while True:
91 try:
92 self.evaluate()
Benjamin Diaz5ac7c082019-02-06 11:58:00 -030093 time.sleep(int(self.conf.get('evaluator', 'interval')))
Benjamin Diaz83038622019-01-28 19:03:39 -030094 except peewee.PeeweeException:
95 log.exception("Database error evaluating alarms: ")
96 raise
Benjamin Diaz51f44862018-11-15 10:27:12 -030097 except Exception:
98 log.exception("Error evaluating alarms")
99
100 def evaluate(self):
Benjamin Diaz058d51d2018-11-20 14:01:43 -0300101 log.debug('evaluate')
Benjamin Diaz51f44862018-11-15 10:27:12 -0300102 processes = []
103 for alarm in Alarm.select():
104 try:
105 vnfr = self.common_db.get_vnfr(alarm.nsr_id, alarm.vnf_member_index)
106 except DbException:
107 log.exception("Error getting vnfr: ")
108 continue
109 vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
110 try:
111 vdur = next(filter(lambda vdur: vdur['name'] == alarm.vdur_name, vnfr['vdur']))
112 except StopIteration:
113 log.warning("No vdur found with name %s for alarm %s", alarm.vdur_name, alarm.id)
114 continue
115 vdu = next(filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu']))
116 vnf_monitoring_param = next(
117 filter(lambda param: param['id'] == alarm.monitoring_param, vnfd['monitoring-param']))
118 nsr_id = vnfr['nsr-id-ref']
119 vnf_member_index = vnfr['member-vnf-index-ref']
120 vdur_name = vdur['name']
121 if 'vdu-monitoring-param' in vnf_monitoring_param:
122 vdu_monitoring_param = next(filter(
123 lambda param: param['id'] == vnf_monitoring_param['vdu-monitoring-param'][
124 'vdu-monitoring-param-ref'], vdu['monitoring-param']))
125 nfvi_metric = vdu_monitoring_param['nfvi-metric']
126
Benjamin Diaz058d51d2018-11-20 14:01:43 -0300127 p = multiprocessing.Process(target=self._evaluate_metric,
Benjamin Diaz51f44862018-11-15 10:27:12 -0300128 args=(nsr_id,
129 vnf_member_index,
130 vdur_name,
131 nfvi_metric,
Benjamin Diaz51f44862018-11-15 10:27:12 -0300132 alarm))
133 processes.append(p)
134 p.start()
135 if 'vdu-metric' in vnf_monitoring_param:
136 vnf_metric_name = vnf_monitoring_param['vdu-metric']['vdu-metric-name-ref']
Benjamin Diaz058d51d2018-11-20 14:01:43 -0300137 p = multiprocessing.Process(target=self._evaluate_metric,
Benjamin Diaz51f44862018-11-15 10:27:12 -0300138 args=(nsr_id,
139 vnf_member_index,
140 vdur_name,
141 vnf_metric_name,
142 alarm))
143 processes.append(p)
144 p.start()
145 if 'vnf-metric' in vnf_monitoring_param:
Benjamin Diaz44ebeeb2018-11-24 00:05:11 -0300146 vnf_metric_name = vnf_monitoring_param['vnf-metric']['vnf-metric-name-ref']
147 p = multiprocessing.Process(target=self._evaluate_metric,
148 args=(nsr_id,
149 vnf_member_index,
150 '',
151 vnf_metric_name,
152 alarm))
153 processes.append(p)
154 p.start()
Benjamin Diaz51f44862018-11-15 10:27:12 -0300155
156 for process in processes:
Benjamin Diaz5ac7c082019-02-06 11:58:00 -0300157 process.join(timeout=10)
Benjamin Diaz51f44862018-11-15 10:27:12 -0300158 triggered_alarms = []
159 while not self.queue.empty():
160 triggered_alarms.append(self.queue.get())
161 for alarm in triggered_alarms:
Benjamin Diaz51f44862018-11-15 10:27:12 -0300162 p = multiprocessing.Process(target=self.notify_alarm,
163 args=(alarm,))
164 p.start()
165
166 def notify_alarm(self, alarm: Alarm):
Benjamin Diaz058d51d2018-11-20 14:01:43 -0300167 log.debug("notify_alarm")
Benjamin Diaz51f44862018-11-15 10:27:12 -0300168 response = ResponseBuilder()
169 now = time.strftime("%d-%m-%Y") + " " + time.strftime("%X")
170 # Generate and send response
171 resp_message = response.generate_response(
172 'notify_alarm',
Benjamin Diazde3d5702018-11-22 17:27:35 -0300173 alarm_id=alarm.uuid,
Benjamin Diaz51f44862018-11-15 10:27:12 -0300174 vdu_name=alarm.vdur_name,
175 vnf_member_index=alarm.vnf_member_index,
176 ns_id=alarm.nsr_id,
177 metric_name=alarm.monitoring_param,
178 operation=alarm.operation,
179 threshold_value=alarm.threshold,
180 sev=alarm.severity,
181 status='alarm',
182 date=now)
Benjamin Diaz5ac7c082019-02-06 11:58:00 -0300183 self.loop.run_until_complete(self.msg_bus.aiowrite('alarm_response', 'notify_alarm', resp_message))
Benjamin Diaz51f44862018-11-15 10:27:12 -0300184 log.info("Sent alarm notification: %s", resp_message)