Resolved Bug 1553 - Monitoring of certain infrastructure metrics fails in Openstack...
[osm/MON.git] / osm_mon / collector / vnf_collectors / openstack.py
1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: bdiaz@whitestack.com or glavado@whitestack.com
21 ##
22 from enum import Enum
23 import logging
24 import time
25 from typing import List
26
27 from ceilometerclient import client as ceilometer_client
28 from ceilometerclient.exc import HTTPException
29 import gnocchiclient.exceptions
30 from gnocchiclient.v1 import client as gnocchi_client
31 from keystoneauth1.exceptions.catalog import EndpointNotFound
32 from keystoneclient.v3 import client as keystone_client
33 from neutronclient.v2_0 import client as neutron_client
34
35 from osm_mon.collector.metric import Metric
36 from osm_mon.collector.utils.openstack import OpenstackUtils
37 from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector
38 from osm_mon.collector.vnf_metric import VnfMetric
39 from osm_mon.core.common_db import CommonDbClient
40 from osm_mon.core.config import Config
41
42
43 log = logging.getLogger(__name__)
44
45 METRIC_MAPPINGS = {
46 "average_memory_utilization": "memory.usage",
47 "disk_read_ops": "disk.read.requests.rate",
48 "disk_write_ops": "disk.write.requests.rate",
49 "disk_read_bytes": "disk.read.bytes.rate",
50 "disk_write_bytes": "disk.write.bytes.rate",
51 "packets_in_dropped": "network.outgoing.packets.drop",
52 "packets_out_dropped": "network.incoming.packets.drop",
53 "packets_received": "network.incoming.packets.rate",
54 "packets_sent": "network.outgoing.packets.rate",
55 "cpu_utilization": "cpu",
56 }
57
58 # Metrics which have new names in Rocky and higher releases
59 METRIC_MAPPINGS_FOR_ROCKY_AND_NEWER_RELEASES = {
60 "disk_read_ops": "disk.device.read.requests",
61 "disk_write_ops": "disk.device.write.requests",
62 "disk_read_bytes": "disk.device.read.bytes",
63 "disk_write_bytes": "disk.device.write.bytes",
64 "packets_received": "network.incoming.packets",
65 "packets_sent": "network.outgoing.packets"
66 }
67
68 METRIC_MULTIPLIERS = {
69 "cpu": 0.0000001
70 }
71
72 METRIC_AGGREGATORS = {
73 "cpu": "rate:mean"
74 }
75
76 INTERFACE_METRICS = ['packets_in_dropped', 'packets_out_dropped', 'packets_received', 'packets_sent']
77
78
79 class MetricType(Enum):
80 INSTANCE = 'instance'
81 INTERFACE_ALL = 'interface_all'
82 INTERFACE_ONE = 'interface_one'
83
84
85 class OpenstackCollector(BaseVimCollector):
86 def __init__(self, config: Config, vim_account_id: str):
87 super().__init__(config, vim_account_id)
88 self.common_db = CommonDbClient(config)
89 vim_account = self.common_db.get_vim_account(vim_account_id)
90 self.backend = self._get_backend(vim_account)
91
92 def _build_keystone_client(self, vim_account: dict) -> keystone_client.Client:
93 sess = OpenstackUtils.get_session(vim_account)
94 return keystone_client.Client(session=sess)
95
96 def _get_resource_uuid(self, nsr_id: str, vnf_member_index: str, vdur_name: str) -> str:
97 vdur = self.common_db.get_vdur(nsr_id, vnf_member_index, vdur_name)
98 return vdur['vim-id']
99
100 def collect(self, vnfr: dict) -> List[Metric]:
101 nsr_id = vnfr['nsr-id-ref']
102 vnf_member_index = vnfr['member-vnf-index-ref']
103 vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
104 # Populate extra tags for metrics
105 tags = {}
106 tags['ns_name'] = self.common_db.get_nsr(nsr_id)['name']
107 if vnfr['_admin']['projects_read']:
108 tags['project_id'] = vnfr['_admin']['projects_read'][0]
109 else:
110 tags['project_id'] = ''
111
112 metrics = []
113
114 for vdur in vnfr['vdur']:
115 # This avoids errors when vdur records have not been completely filled
116 if 'name' not in vdur:
117 continue
118 vdu = next(
119 filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu'])
120 )
121 if 'monitoring-parameter' in vdu:
122 for param in vdu['monitoring-parameter']:
123 metric_name = param['performance-metric']
124 openstack_metric_name = METRIC_MAPPINGS[metric_name]
125 metric_type = self._get_metric_type(metric_name)
126 try:
127 resource_id = self._get_resource_uuid(nsr_id, vnf_member_index, vdur['name'])
128 except ValueError:
129 log.warning(
130 "Could not find resource_uuid for vdur %s, vnf_member_index %s, nsr_id %s. "
131 "Was it recently deleted?",
132 vdur['name'], vnf_member_index, nsr_id)
133 continue
134 try:
135 log.info(
136 "Collecting metric type: %s and metric_name: %s and resource_id %s and ",
137 metric_type,
138 metric_name,
139 resource_id)
140 value = self.backend.collect_metric(
141 metric_type, openstack_metric_name, resource_id
142 )
143
144 if value is None and metric_name in METRIC_MAPPINGS_FOR_ROCKY_AND_NEWER_RELEASES:
145 # Reattempting metric collection with new metric names.
146 # Some metric names have changed in newer Openstack releases
147 log.info(
148 "Reattempting metric collection for type: %s and name: %s and resource_id %s",
149 metric_type,
150 metric_name,
151 resource_id
152 )
153 openstack_metric_name = METRIC_MAPPINGS_FOR_ROCKY_AND_NEWER_RELEASES[metric_name]
154 value = self.backend.collect_metric(
155 metric_type, openstack_metric_name, resource_id
156 )
157 if value is not None:
158 log.info("value: %s", value)
159 metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name, value, tags)
160 metrics.append(metric)
161 else:
162 log.info("metric value is empty")
163 except Exception as e:
164 log.exception("Error collecting metric %s for vdu %s" % (metric_name, vdur['name']))
165 log.info("Error in metric collection: %s" % e)
166 return metrics
167
168 def _get_backend(self, vim_account: dict):
169 try:
170 gnocchi = GnocchiBackend(vim_account)
171 gnocchi.client.metric.list(limit=1)
172 log.info("Using gnocchi backend to collect metric")
173 return gnocchi
174 except (HTTPException, EndpointNotFound):
175 ceilometer = CeilometerBackend(vim_account)
176 ceilometer.client.capabilities.get()
177 log.info("Using ceilometer backend to collect metric")
178 return ceilometer
179
180 def _get_metric_type(self, metric_name: str) -> MetricType:
181 if metric_name not in INTERFACE_METRICS:
182 return MetricType.INSTANCE
183 else:
184 return MetricType.INTERFACE_ALL
185
186
187 class OpenstackBackend:
188 def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str):
189 pass
190
191
192 class GnocchiBackend(OpenstackBackend):
193
194 def __init__(self, vim_account: dict):
195 self.client = self._build_gnocchi_client(vim_account)
196 self.neutron = self._build_neutron_client(vim_account)
197
198 def _build_gnocchi_client(self, vim_account: dict) -> gnocchi_client.Client:
199 sess = OpenstackUtils.get_session(vim_account)
200 return gnocchi_client.Client(session=sess)
201
202 def _build_neutron_client(self, vim_account: dict) -> neutron_client.Client:
203 sess = OpenstackUtils.get_session(vim_account)
204 return neutron_client.Client(session=sess)
205
206 def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str):
207 if metric_type == MetricType.INTERFACE_ALL:
208 return self._collect_interface_all_metric(metric_name, resource_id)
209
210 elif metric_type == MetricType.INSTANCE:
211 return self._collect_instance_metric(metric_name, resource_id)
212
213 else:
214 raise Exception('Unknown metric type %s' % metric_type.value)
215
216 def _collect_interface_all_metric(self, openstack_metric_name, resource_id):
217 total_measure = None
218 interfaces = self.client.resource.search(resource_type='instance_network_interface',
219 query={'=': {'instance_id': resource_id}})
220 for interface in interfaces:
221 try:
222 measures = self.client.metric.get_measures(openstack_metric_name,
223 resource_id=interface['id'],
224 limit=1)
225 if measures:
226 if not total_measure:
227 total_measure = 0.0
228 total_measure += measures[-1][2]
229
230 except (gnocchiclient.exceptions.NotFound, TypeError) as e:
231 # Gnocchi in some Openstack versions raise TypeError instead of NotFound
232 log.debug("No metric %s found for interface %s: %s", openstack_metric_name,
233 interface['id'], e)
234 return total_measure
235
236 def _collect_instance_metric(self, openstack_metric_name, resource_id):
237 value = None
238 try:
239 aggregation = METRIC_AGGREGATORS.get(openstack_metric_name)
240
241 try:
242 measures = self.client.metric.get_measures(openstack_metric_name,
243 aggregation=aggregation,
244 start=time.time() - 1200,
245 resource_id=resource_id)
246 if measures:
247 value = measures[-1][2]
248 except (gnocchiclient.exceptions.NotFound, gnocchiclient.exceptions.BadRequest, TypeError) as e:
249 # CPU metric in previous Openstack versions do not support rate:mean aggregation method
250 # Gnocchi in some Openstack versions raise TypeError instead of NotFound or BadRequest
251 if openstack_metric_name == "cpu":
252 log.debug("No metric %s found for instance %s: %s", openstack_metric_name, resource_id, e)
253 log.info("Retrying to get metric %s for instance %s without aggregation",
254 openstack_metric_name, resource_id)
255 measures = self.client.metric.get_measures(openstack_metric_name,
256 resource_id=resource_id,
257 limit=1)
258 else:
259 raise e
260 # measures[-1] is the last measure
261 # measures[-2] is the previous measure
262 # measures[x][2] is the value of the metric
263 if measures and len(measures) >= 2:
264 value = measures[-1][2] - measures[-2][2]
265 if value:
266 # measures[-1][0] is the time of the reporting interval
267 # measures[-1][1] is the duration of the reporting interval
268 if aggregation:
269 # If this is an aggregate, we need to divide the total over the reported time period.
270 # Even if the aggregation method is not supported by Openstack, the code will execute it
271 # because aggregation is specified in METRIC_AGGREGATORS
272 value = value / measures[-1][1]
273 if openstack_metric_name in METRIC_MULTIPLIERS:
274 value = value * METRIC_MULTIPLIERS[openstack_metric_name]
275 except gnocchiclient.exceptions.NotFound as e:
276 log.debug("No metric %s found for instance %s: %s", openstack_metric_name, resource_id,
277 e)
278 return value
279
280
281 class CeilometerBackend(OpenstackBackend):
282 def __init__(self, vim_account: dict):
283 self.client = self._build_ceilometer_client(vim_account)
284
285 def _build_ceilometer_client(self, vim_account: dict) -> ceilometer_client.Client:
286 sess = OpenstackUtils.get_session(vim_account)
287 return ceilometer_client.Client("2", session=sess)
288
289 def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str):
290 if metric_type != MetricType.INSTANCE:
291 raise NotImplementedError('Ceilometer backend only support instance metrics')
292 measures = self.client.samples.list(meter_name=metric_name, limit=1, q=[
293 {'field': 'resource_id', 'op': 'eq', 'value': resource_id}])
294 return measures[0].counter_volume if measures else None