1 |
|
# -*- coding: utf-8 -*- |
2 |
|
# pylint: disable=no-member |
3 |
|
|
4 |
|
# Copyright 2018 Whitestack, LLC |
5 |
|
# ************************************************************* |
6 |
|
|
7 |
|
# This file is part of OSM Monitoring module |
8 |
|
# All Rights Reserved to Whitestack, LLC |
9 |
|
|
10 |
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
11 |
|
# not use this file except in compliance with the License. You may obtain |
12 |
|
# a copy of the License at |
13 |
|
|
14 |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
15 |
|
|
16 |
|
# Unless required by applicable law or agreed to in writing, software |
17 |
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
18 |
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
19 |
|
# License for the specific language governing permissions and limitations |
20 |
|
# under the License. |
21 |
|
|
22 |
|
# For those usages not covered by the Apache License, Version 2.0 please |
23 |
|
# contact: bdiaz@whitestack.com or glavado@whitestack.com |
24 |
|
## |
25 |
1 |
import asyncio |
26 |
1 |
import json |
27 |
1 |
import logging |
28 |
|
|
29 |
1 |
import requests |
30 |
1 |
from requests.exceptions import ConnectionError, RequestException |
31 |
|
|
32 |
1 |
from osm_policy_module.common.common_db_client import CommonDbClient |
33 |
1 |
from osm_policy_module.common.lcm_client import LcmClient |
34 |
1 |
from osm_policy_module.common.mon_client import MonClient |
35 |
1 |
from osm_policy_module.core import database |
36 |
1 |
from osm_policy_module.core.config import Config |
37 |
1 |
from osm_policy_module.core.database import VnfAlarm, VnfAlarmRepository, AlarmActionRepository |
38 |
1 |
from osm_policy_module.core.exceptions import VdurNotFound |
39 |
|
|
40 |
1 |
log = logging.getLogger(__name__) |
41 |
|
|
42 |
|
|
43 |
1 |
class AlarmingService: |
44 |
|
|
45 |
1 |
def __init__(self, config: Config, loop=None): |
46 |
1 |
self.conf = config |
47 |
1 |
if not loop: |
48 |
1 |
loop = asyncio.get_event_loop() |
49 |
1 |
self.loop = loop |
50 |
1 |
self.db_client = CommonDbClient(config) |
51 |
1 |
self.mon_client = MonClient(config, loop=self.loop) |
52 |
1 |
self.lcm_client = LcmClient(config, loop=self.loop) |
53 |
|
|
54 |
1 |
async def configure_vnf_alarms(self, nsr_id: str): |
55 |
1 |
log.info("Configuring vnf alarms for network service %s", nsr_id) |
56 |
1 |
alarms_created = [] |
57 |
1 |
database.db.connect() |
58 |
1 |
try: |
59 |
1 |
with database.db.atomic(): |
60 |
1 |
vnfrs = self.db_client.get_vnfrs(nsr_id) |
61 |
1 |
for vnfr in vnfrs: |
62 |
1 |
log.debug("Processing vnfr: %s", vnfr) |
63 |
1 |
vnfd = self.db_client.get_vnfd(vnfr['vnfd-id']) |
64 |
1 |
for vdur in vnfr['vdur']: |
65 |
1 |
vdu = next( |
66 |
|
filter( |
67 |
|
lambda vdu: vdu['id'] == vdur['vdu-id-ref'], |
68 |
|
vnfd['vdu'] |
69 |
|
) |
70 |
|
) |
71 |
1 |
if 'alarm' in vdu: |
72 |
1 |
alarm_descriptors = vdu['alarm'] |
73 |
1 |
for alarm_descriptor in alarm_descriptors: |
74 |
1 |
try: |
75 |
1 |
VnfAlarmRepository.get( |
76 |
|
VnfAlarm.alarm_id == alarm_descriptor['alarm-id'], |
77 |
|
VnfAlarm.vnf_member_index == vnfr['member-vnf-index-ref'], |
78 |
|
VnfAlarm.vdu_name == vdur['name'], |
79 |
|
VnfAlarm.nsr_id == nsr_id |
80 |
|
) |
81 |
0 |
log.debug("vdu %s already has an alarm configured with same id %s", vdur['name'], |
82 |
|
alarm_descriptor['alarm-id']) |
83 |
0 |
continue |
84 |
1 |
except VnfAlarm.DoesNotExist: |
85 |
1 |
pass |
86 |
1 |
vnf_monitoring_param = next( |
87 |
|
filter( |
88 |
|
lambda param: param['id'] == alarm_descriptor['vnf-monitoring-param-ref'], |
89 |
|
vdu.get('monitoring-parameter', []) |
90 |
|
), |
91 |
|
{} |
92 |
|
) |
93 |
1 |
metric_name = self._get_metric_name(vnf_monitoring_param) |
94 |
1 |
alarm_uuid = await self.mon_client.create_alarm( |
95 |
|
metric_name=metric_name, |
96 |
|
ns_id=nsr_id, |
97 |
|
vdu_name=vdur['name'], |
98 |
|
vnf_member_index=vnfr['member-vnf-index-ref'], |
99 |
|
threshold=alarm_descriptor['value'], |
100 |
|
operation=alarm_descriptor['operation'] |
101 |
|
) |
102 |
1 |
alarm = VnfAlarmRepository.create( |
103 |
|
alarm_id=alarm_descriptor['alarm-id'], |
104 |
|
alarm_uuid=alarm_uuid, |
105 |
|
nsr_id=nsr_id, |
106 |
|
vnf_member_index=vnfr['member-vnf-index-ref'], |
107 |
|
vdu_name=vdur['name'] |
108 |
|
) |
109 |
1 |
for action_type in ['ok', 'insufficient-data', 'alarm']: |
110 |
1 |
if 'actions' in alarm_descriptor and action_type in alarm_descriptor['actions']: |
111 |
1 |
for url in alarm_descriptor['actions'][action_type]: |
112 |
1 |
AlarmActionRepository.create( |
113 |
|
type=action_type, |
114 |
|
url=url['url'], |
115 |
|
alarm=alarm |
116 |
|
) |
117 |
1 |
alarms_created.append(alarm) |
118 |
|
|
119 |
0 |
except Exception as e: |
120 |
0 |
log.exception("Error configuring VNF alarms:") |
121 |
0 |
if len(alarms_created) > 0: |
122 |
0 |
log.debug("Cleaning alarm resources in MON") |
123 |
0 |
for alarm in alarms_created: |
124 |
0 |
try: |
125 |
0 |
await self.mon_client.delete_alarm(alarm.nsr_id, |
126 |
|
alarm.vnf_member_index, |
127 |
|
alarm.vdu_name, |
128 |
|
alarm.alarm_uuid) |
129 |
0 |
except ValueError: |
130 |
0 |
log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) |
131 |
0 |
raise e |
132 |
|
finally: |
133 |
1 |
database.db.close() |
134 |
|
|
135 |
1 |
async def delete_orphaned_alarms(self, nsr_id): |
136 |
|
# TODO: Review as it seems this code is never called |
137 |
0 |
log.info("Deleting orphaned vnf alarms for network service %s", nsr_id) |
138 |
0 |
database.db.connect() |
139 |
0 |
try: |
140 |
0 |
with database.db.atomic(): |
141 |
0 |
for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id): |
142 |
0 |
try: |
143 |
0 |
self.db_client.get_vdur(nsr_id, alarm.vnf_member_index, alarm.vdu_name) |
144 |
0 |
except VdurNotFound: |
145 |
0 |
log.debug("Deleting orphaned alarm %s", alarm.alarm_uuid) |
146 |
0 |
try: |
147 |
0 |
await self.mon_client.delete_alarm( |
148 |
|
alarm.nsr_id, |
149 |
|
alarm.vnf_member_index, |
150 |
|
alarm.vdu_name, |
151 |
|
alarm.alarm_uuid) |
152 |
0 |
except ValueError: |
153 |
0 |
log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) |
154 |
0 |
alarm.delete_instance() |
155 |
0 |
except Exception as e: |
156 |
0 |
log.exception("Error deleting orphaned alarms:") |
157 |
0 |
raise e |
158 |
|
finally: |
159 |
0 |
database.db.close() |
160 |
|
|
161 |
1 |
async def delete_vnf_alarms(self, nsr_id): |
162 |
0 |
log.info("Deleting vnf alarms for network service %s", nsr_id) |
163 |
0 |
database.db.connect() |
164 |
0 |
try: |
165 |
0 |
with database.db.atomic(): |
166 |
0 |
for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id): |
167 |
0 |
log.debug("Deleting vnf alarm %s", alarm.alarm_uuid) |
168 |
0 |
try: |
169 |
0 |
await self.mon_client.delete_alarm( |
170 |
|
alarm.nsr_id, |
171 |
|
alarm.vnf_member_index, |
172 |
|
alarm.vdu_name, |
173 |
|
alarm.alarm_uuid) |
174 |
0 |
except ValueError: |
175 |
0 |
log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) |
176 |
0 |
alarm.delete_instance() |
177 |
|
|
178 |
0 |
except Exception as e: |
179 |
0 |
log.exception("Error deleting vnf alarms:") |
180 |
0 |
raise e |
181 |
|
finally: |
182 |
0 |
database.db.close() |
183 |
|
|
184 |
1 |
async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict): |
185 |
1 |
database.db.connect() |
186 |
1 |
try: |
187 |
1 |
with database.db.atomic(): |
188 |
1 |
alarm = VnfAlarmRepository.get(VnfAlarm.alarm_uuid == alarm_uuid) |
189 |
1 |
log.debug("Handling vnf alarm %s with status %s", alarm.alarm_id, status) |
190 |
1 |
for action in alarm.actions: |
191 |
1 |
if action.type == status: |
192 |
1 |
log.info("Executing request to url %s for vnf alarm %s with status %s", action.url, |
193 |
|
alarm.alarm_id, status) |
194 |
1 |
try: |
195 |
1 |
requests.post(url=action.url, json=json.dumps(payload), timeout=10) |
196 |
0 |
except RequestException as e: |
197 |
0 |
log.info("Error connecting to url %s", action.url) |
198 |
0 |
log.debug("RequestException %s", e) |
199 |
0 |
except ConnectionError: |
200 |
0 |
log.exception("Error connecting to url %s", action.url) |
201 |
|
|
202 |
0 |
except VnfAlarm.DoesNotExist: |
203 |
0 |
log.debug("There is no alarming action configured for alarm %s.", alarm_uuid) |
204 |
|
finally: |
205 |
1 |
database.db.close() |
206 |
|
|
207 |
1 |
def _get_metric_name(self, vnf_monitoring_param: dict): |
208 |
1 |
if 'performance-metric' in vnf_monitoring_param: |
209 |
1 |
return vnf_monitoring_param['performance-metric'] |
210 |
0 |
raise ValueError('No metric name found for vnf_monitoring_param %s' % vnf_monitoring_param['id']) |