Fix bug 1258: metrics not properly collected in some Openstacks
[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 METRIC_MULTIPLIERS = {
59 "cpu": 0.0000001
60 }
61
62 METRIC_AGGREGATORS = {
63 "cpu": "rate:mean"
64 }
65
66 INTERFACE_METRICS = ['packets_in_dropped', 'packets_out_dropped', 'packets_received', 'packets_sent']
67
68
69 class MetricType(Enum):
70 INSTANCE = 'instance'
71 INTERFACE_ALL = 'interface_all'
72 INTERFACE_ONE = 'interface_one'
73
74
75 class OpenstackCollector(BaseVimCollector):
76 def __init__(self, config: Config, vim_account_id: str):
77 super().__init__(config, vim_account_id)
78 self.common_db = CommonDbClient(config)
79 vim_account = self.common_db.get_vim_account(vim_account_id)
80 self.backend = self._get_backend(vim_account)
81
82 def _build_keystone_client(self, vim_account: dict) -> keystone_client.Client:
83 sess = OpenstackUtils.get_session(vim_account)
84 return keystone_client.Client(session=sess)
85
86 def _get_resource_uuid(self, nsr_id: str, vnf_member_index: str, vdur_name: str) -> str:
87 vdur = self.common_db.get_vdur(nsr_id, vnf_member_index, vdur_name)
88 return vdur['vim-id']
89
90 def collect(self, vnfr: dict) -> List[Metric]:
91 nsr_id = vnfr['nsr-id-ref']
92 vnf_member_index = vnfr['member-vnf-index-ref']
93 vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
94
95 # Populate extra tags for metrics
96 tags = {}
97 tags['ns_name'] = self.common_db.get_nsr(nsr_id)['name']
98 if vnfr['_admin']['projects_read']:
99 tags['project_id'] = vnfr['_admin']['projects_read'][0]
100 else:
101 tags['project_id'] = ''
102
103 metrics = []
104 for vdur in vnfr['vdur']:
105 # This avoids errors when vdur records have not been completely filled
106 if 'name' not in vdur:
107 continue
108 vdu = next(
109 filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu'])
110 )
111 if 'monitoring-param' in vdu:
112 for param in vdu['monitoring-param']:
113 metric_name = param['nfvi-metric']
114 interface_name = param['interface-name-ref'] if 'interface-name-ref' in param else None
115 openstack_metric_name = METRIC_MAPPINGS[metric_name]
116 metric_type = self._get_metric_type(metric_name, interface_name)
117 try:
118 resource_id = self._get_resource_uuid(nsr_id, vnf_member_index, vdur['name'])
119 except ValueError:
120 log.warning(
121 "Could not find resource_uuid for vdur %s, vnf_member_index %s, nsr_id %s. "
122 "Was it recently deleted?",
123 vdur['name'], vnf_member_index, nsr_id)
124 continue
125 try:
126 log.info("Collecting metric type: %s and metric_name: %s and resource_id %s and "
127 "interface_name: %s", metric_type, metric_name, resource_id, interface_name)
128 value = self.backend.collect_metric(metric_type, openstack_metric_name, resource_id,
129 interface_name)
130 if value is not None:
131 log.info("value: %s", value)
132 if interface_name:
133 tags['interface'] = interface_name
134 metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name, value, tags)
135 metrics.append(metric)
136 else:
137 log.info("metric value is empty")
138 except Exception as e:
139 log.exception("Error collecting metric %s for vdu %s" % (metric_name, vdur['name']))
140 log.info("Error in metric collection: %s" % e)
141 return metrics
142
143 def _get_backend(self, vim_account: dict):
144 try:
145 ceilometer = CeilometerBackend(vim_account)
146 ceilometer.client.capabilities.get()
147 log.info("Using ceilometer backend to collect metric")
148 return ceilometer
149 except (HTTPException, EndpointNotFound):
150 gnocchi = GnocchiBackend(vim_account)
151 gnocchi.client.metric.list(limit=1)
152 log.info("Using gnocchi backend to collect metric")
153 return gnocchi
154
155 def _get_metric_type(self, metric_name: str, interface_name: str) -> MetricType:
156 if metric_name not in INTERFACE_METRICS:
157 return MetricType.INSTANCE
158 else:
159 if interface_name:
160 return MetricType.INTERFACE_ONE
161 return MetricType.INTERFACE_ALL
162
163
164 class OpenstackBackend:
165 def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str):
166 pass
167
168
169 class GnocchiBackend(OpenstackBackend):
170
171 def __init__(self, vim_account: dict):
172 self.client = self._build_gnocchi_client(vim_account)
173 self.neutron = self._build_neutron_client(vim_account)
174
175 def _build_gnocchi_client(self, vim_account: dict) -> gnocchi_client.Client:
176 sess = OpenstackUtils.get_session(vim_account)
177 return gnocchi_client.Client(session=sess)
178
179 def _build_neutron_client(self, vim_account: dict) -> neutron_client.Client:
180 sess = OpenstackUtils.get_session(vim_account)
181 return neutron_client.Client(session=sess)
182
183 def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str):
184 if metric_type == MetricType.INTERFACE_ONE:
185 return self._collect_interface_one_metric(metric_name, resource_id, interface_name)
186
187 if metric_type == MetricType.INTERFACE_ALL:
188 return self._collect_interface_all_metric(metric_name, resource_id)
189
190 elif metric_type == MetricType.INSTANCE:
191 return self._collect_instance_metric(metric_name, resource_id)
192
193 else:
194 raise Exception('Unknown metric type %s' % metric_type.value)
195
196 def _collect_interface_one_metric(self, metric_name, resource_id, interface_name):
197 ports = self.neutron.list_ports(name=interface_name, device_id=resource_id)
198 if not ports or not ports['ports']:
199 raise Exception(
200 'Port not found for interface %s on instance %s' % (interface_name, resource_id))
201 port = ports['ports'][0]
202 port_uuid = port['id'][:11]
203 tap_name = 'tap' + port_uuid
204 interfaces = self.client.resource.search(resource_type='instance_network_interface',
205 query={'=': {'name': tap_name}})
206 measures = self.client.metric.get_measures(metric_name,
207 resource_id=interfaces[0]['id'],
208 limit=1)
209 return measures[-1][2] if measures else None
210
211 def _collect_interface_all_metric(self, openstack_metric_name, resource_id):
212 total_measure = None
213 interfaces = self.client.resource.search(resource_type='instance_network_interface',
214 query={'=': {'instance_id': resource_id}})
215 for interface in interfaces:
216 try:
217 measures = self.client.metric.get_measures(openstack_metric_name,
218 resource_id=interface['id'],
219 limit=1)
220 if measures:
221 if not total_measure:
222 total_measure = 0.0
223 total_measure += measures[-1][2]
224
225 except gnocchiclient.exceptions.NotFound as e:
226 log.debug("No metric %s found for interface %s: %s", openstack_metric_name,
227 interface['id'], e)
228 return total_measure
229
230 def _collect_instance_metric(self, openstack_metric_name, resource_id):
231 value = None
232 try:
233 aggregation = METRIC_AGGREGATORS.get(openstack_metric_name)
234
235 try:
236 measures = self.client.metric.get_measures(openstack_metric_name,
237 aggregation=aggregation,
238 start=time.time() - 1200,
239 resource_id=resource_id)
240 if measures:
241 value = measures[-1][2]
242 except gnocchiclient.exceptions.NotFound as e:
243 # CPU metric in previous Openstack versions do not support rate:mean aggregation method
244 if openstack_metric_name == "cpu":
245 log.debug("No metric %s found for instance %s: %s", openstack_metric_name, resource_id, e)
246 log.debug("Retrying to get metric %s for instance %s without aggregation",
247 openstack_metric_name, resource_id)
248 measures = self.client.metric.get_measures(openstack_metric_name,
249 resource_id=resource_id,
250 limit=1)
251 else:
252 raise e
253 # measures[-1] is the last measure
254 # measures[-2] is the previous measure
255 # measures[x][2] is the value of the metric
256 if measures and len(measures) >= 2:
257 value = measures[-1][2] - measures[-2][2]
258 if value:
259 # measures[-1][0] is the time of the reporting interval
260 # measures[-1][1] is the duration of the reporting interval
261 if aggregation:
262 # If this is an aggregate, we need to divide the total over the reported time period.
263 # Even if the aggregation method is not supported by Openstack, the code will execute it
264 # because aggregation is specified in METRIC_AGGREGATORS
265 value = value / measures[-1][1]
266 if openstack_metric_name in METRIC_MULTIPLIERS:
267 value = value * METRIC_MULTIPLIERS[openstack_metric_name]
268 except gnocchiclient.exceptions.NotFound as e:
269 log.debug("No metric %s found for instance %s: %s", openstack_metric_name, resource_id,
270 e)
271 return value
272
273
274 class CeilometerBackend(OpenstackBackend):
275 def __init__(self, vim_account: dict):
276 self.client = self._build_ceilometer_client(vim_account)
277
278 def _build_ceilometer_client(self, vim_account: dict) -> ceilometer_client.Client:
279 sess = OpenstackUtils.get_session(vim_account)
280 return ceilometer_client.Client("2", session=sess)
281
282 def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str):
283 if metric_type != MetricType.INSTANCE:
284 raise NotImplementedError('Ceilometer backend only support instance metrics')
285 measures = self.client.samples.list(meter_name=metric_name, limit=1, q=[
286 {'field': 'resource_id', 'op': 'eq', 'value': resource_id}])
287 return measures[0].counter_volume if measures else None