1 |
|
# -*- 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 |
|
|
21 |
|
# For those usages not covered by the Apache License, Version 2.0 please |
22 |
|
# contact: bdiaz@whitestack.com or glavado@whitestack.com |
23 |
|
## |
24 |
1 |
import asyncio |
25 |
1 |
import json |
26 |
1 |
import logging |
27 |
|
|
28 |
1 |
import requests |
29 |
|
|
30 |
1 |
from osm_policy_module.common.common_db_client import CommonDbClient |
31 |
1 |
from osm_policy_module.common.lcm_client import LcmClient |
32 |
1 |
from osm_policy_module.common.mon_client import MonClient |
33 |
1 |
from osm_policy_module.core import database |
34 |
1 |
from osm_policy_module.core.config import Config |
35 |
1 |
from osm_policy_module.core.database import VnfAlarm, VnfAlarmRepository, AlarmActionRepository |
36 |
1 |
from osm_policy_module.core.exceptions import VdurNotFound |
37 |
|
|
38 |
1 |
log = logging.getLogger(__name__) |
39 |
|
|
40 |
|
|
41 |
1 |
class AlarmingService: |
42 |
|
|
43 |
1 |
def __init__(self, config: Config, loop=None): |
44 |
1 |
self.conf = config |
45 |
1 |
if not loop: |
46 |
1 |
loop = asyncio.get_event_loop() |
47 |
1 |
self.loop = loop |
48 |
1 |
self.db_client = CommonDbClient(config) |
49 |
1 |
self.mon_client = MonClient(config, loop=self.loop) |
50 |
1 |
self.lcm_client = LcmClient(config, loop=self.loop) |
51 |
|
|
52 |
1 |
async def configure_vnf_alarms(self, nsr_id: str): |
53 |
1 |
log.info("Configuring vnf alarms for network service %s", nsr_id) |
54 |
1 |
alarms_created = [] |
55 |
1 |
database.db.connect() |
56 |
1 |
try: |
57 |
1 |
with database.db.atomic(): |
58 |
1 |
vnfrs = self.db_client.get_vnfrs(nsr_id) |
59 |
1 |
for vnfr in vnfrs: |
60 |
1 |
log.debug("Processing vnfr: %s", vnfr) |
61 |
1 |
vnfd = self.db_client.get_vnfd(vnfr['vnfd-id']) |
62 |
1 |
for vdur in vnfr['vdur']: |
63 |
1 |
vdu = next( |
64 |
|
filter( |
65 |
|
lambda vdu: vdu['id'] == vdur['vdu-id-ref'], |
66 |
|
vnfd['vdu'] |
67 |
|
) |
68 |
|
) |
69 |
1 |
if 'alarm' in vdu: |
70 |
1 |
alarm_descriptors = vdu['alarm'] |
71 |
1 |
for alarm_descriptor in alarm_descriptors: |
72 |
1 |
try: |
73 |
1 |
VnfAlarmRepository.get( |
74 |
|
VnfAlarm.alarm_id == alarm_descriptor['alarm-id'], |
75 |
|
VnfAlarm.vnf_member_index == vnfr['member-vnf-index-ref'], |
76 |
|
VnfAlarm.vdu_name == vdur['name'], |
77 |
|
VnfAlarm.nsr_id == nsr_id |
78 |
|
) |
79 |
0 |
log.debug("vdu %s already has an alarm configured with same id %s", vdur['name'], |
80 |
|
alarm_descriptor['alarm-id']) |
81 |
0 |
continue |
82 |
1 |
except VnfAlarm.DoesNotExist: |
83 |
1 |
pass |
84 |
1 |
vnf_monitoring_param = next( |
85 |
|
filter( |
86 |
|
lambda param: param['id'] == alarm_descriptor['vnf-monitoring-param-ref'], |
87 |
|
vnfd['monitoring-param']) |
88 |
|
) |
89 |
1 |
metric_name = self._get_metric_name(vnf_monitoring_param, vdur, vnfd) |
90 |
1 |
alarm_uuid = await self.mon_client.create_alarm( |
91 |
|
metric_name=metric_name, |
92 |
|
ns_id=nsr_id, |
93 |
|
vdu_name=vdur['name'], |
94 |
|
vnf_member_index=vnfr['member-vnf-index-ref'], |
95 |
|
threshold=alarm_descriptor['value'], |
96 |
|
operation=alarm_descriptor['operation'], |
97 |
|
statistic=vnf_monitoring_param['aggregation-type'] |
98 |
|
) |
99 |
1 |
alarm = VnfAlarmRepository.create( |
100 |
|
alarm_id=alarm_descriptor['alarm-id'], |
101 |
|
alarm_uuid=alarm_uuid, |
102 |
|
nsr_id=nsr_id, |
103 |
|
vnf_member_index=vnfr['member-vnf-index-ref'], |
104 |
|
vdu_name=vdur['name'] |
105 |
|
) |
106 |
1 |
for action_type in ['ok', 'insufficient-data', 'alarm']: |
107 |
1 |
if 'actions' in alarm_descriptor and action_type in alarm_descriptor['actions']: |
108 |
1 |
for url in alarm_descriptor['actions'][action_type]: |
109 |
1 |
AlarmActionRepository.create( |
110 |
|
type=action_type, |
111 |
|
url=url['url'], |
112 |
|
alarm=alarm |
113 |
|
) |
114 |
1 |
alarms_created.append(alarm) |
115 |
|
|
116 |
0 |
except Exception as e: |
117 |
0 |
log.exception("Error configuring VNF alarms:") |
118 |
0 |
if len(alarms_created) > 0: |
119 |
0 |
log.debug("Cleaning alarm resources in MON") |
120 |
0 |
for alarm in alarms_created: |
121 |
0 |
try: |
122 |
0 |
await self.mon_client.delete_alarm(alarm.nsr_id, |
123 |
|
alarm.vnf_member_index, |
124 |
|
alarm.vdu_name, |
125 |
|
alarm.alarm_uuid) |
126 |
0 |
except ValueError: |
127 |
0 |
log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) |
128 |
0 |
raise e |
129 |
|
finally: |
130 |
1 |
database.db.close() |
131 |
|
|
132 |
1 |
async def delete_orphaned_alarms(self, nsr_id): |
133 |
0 |
log.info("Deleting orphaned vnf alarms for network service %s", nsr_id) |
134 |
0 |
database.db.connect() |
135 |
0 |
try: |
136 |
0 |
with database.db.atomic(): |
137 |
0 |
for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id): |
138 |
0 |
try: |
139 |
0 |
self.db_client.get_vdur(nsr_id, alarm.vnf_member_index, alarm.vdu_name) |
140 |
0 |
except VdurNotFound: |
141 |
0 |
log.debug("Deleting orphaned alarm %s", alarm.alarm_uuid) |
142 |
0 |
try: |
143 |
0 |
await self.mon_client.delete_alarm( |
144 |
|
alarm.nsr_id, |
145 |
|
alarm.vnf_member_index, |
146 |
|
alarm.vdu_name, |
147 |
|
alarm.alarm_uuid) |
148 |
0 |
except ValueError: |
149 |
0 |
log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) |
150 |
0 |
alarm.delete_instance() |
151 |
0 |
except Exception as e: |
152 |
0 |
log.exception("Error deleting orphaned alarms:") |
153 |
0 |
raise e |
154 |
|
finally: |
155 |
0 |
database.db.close() |
156 |
|
|
157 |
1 |
async def delete_vnf_alarms(self, nsr_id): |
158 |
0 |
log.info("Deleting vnf alarms for network service %s", nsr_id) |
159 |
0 |
database.db.connect() |
160 |
0 |
try: |
161 |
0 |
with database.db.atomic(): |
162 |
0 |
for alarm in VnfAlarmRepository.list(VnfAlarm.nsr_id == nsr_id): |
163 |
0 |
log.debug("Deleting vnf alarm %s", alarm.alarm_uuid) |
164 |
0 |
try: |
165 |
0 |
await self.mon_client.delete_alarm( |
166 |
|
alarm.nsr_id, |
167 |
|
alarm.vnf_member_index, |
168 |
|
alarm.vdu_name, |
169 |
|
alarm.alarm_uuid) |
170 |
0 |
except ValueError: |
171 |
0 |
log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid) |
172 |
0 |
alarm.delete_instance() |
173 |
|
|
174 |
0 |
except Exception as e: |
175 |
0 |
log.exception("Error deleting vnf alarms:") |
176 |
0 |
raise e |
177 |
|
finally: |
178 |
0 |
database.db.close() |
179 |
|
|
180 |
1 |
async def handle_alarm(self, alarm_uuid: str, status: str, payload: dict): |
181 |
1 |
database.db.connect() |
182 |
1 |
try: |
183 |
1 |
with database.db.atomic(): |
184 |
1 |
alarm = VnfAlarmRepository.get(VnfAlarm.alarm_uuid == alarm_uuid) |
185 |
1 |
log.debug("Handling vnf alarm %s with status %s", alarm.alarm_id, status) |
186 |
1 |
for action in alarm.actions: |
187 |
1 |
if action.type == status: |
188 |
1 |
log.info("Executing request to url %s for vnf alarm %s with status %s", action.url, |
189 |
|
alarm.alarm_id, status) |
190 |
1 |
requests.post(url=action.url, json=json.dumps(payload)) |
191 |
0 |
except VnfAlarm.DoesNotExist: |
192 |
0 |
log.debug("There is no alarming action configured for alarm %s.", alarm_uuid) |
193 |
|
finally: |
194 |
1 |
database.db.close() |
195 |
|
|
196 |
1 |
def _get_metric_name(self, vnf_monitoring_param: dict, vdur: dict, vnfd: dict): |
197 |
1 |
vdu = next( |
198 |
|
filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu']) |
199 |
|
) |
200 |
1 |
if 'vdu-monitoring-param' in vnf_monitoring_param: |
201 |
1 |
vdu_monitoring_param = next(filter( |
202 |
|
lambda param: param['id'] == vnf_monitoring_param['vdu-monitoring-param'][ |
203 |
|
'vdu-monitoring-param-ref'], vdu['monitoring-param'])) |
204 |
1 |
nfvi_metric = vdu_monitoring_param['nfvi-metric'] |
205 |
1 |
return nfvi_metric |
206 |
0 |
if 'vdu-metric' in vnf_monitoring_param: |
207 |
0 |
vnf_metric_name = vnf_monitoring_param['vdu-metric']['vdu-metric-name-ref'] |
208 |
0 |
return vnf_metric_name |
209 |
0 |
if 'vnf-metric' in vnf_monitoring_param: |
210 |
0 |
vnf_metric_name = vnf_monitoring_param['vnf-metric']['vnf-metric-name-ref'] |
211 |
0 |
return vnf_metric_name |
212 |
0 |
raise ValueError('No metric name found for vnf_monitoring_param %s' % vnf_monitoring_param['id']) |