1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: bdiaz@whitestack.com or glavado@whitestack.com
24 from typing
import List
26 import gnocchiclient
.exceptions
27 from ceilometerclient
import client
as ceilometer_client
28 from ceilometerclient
.exc
import HTTPException
29 from gnocchiclient
.v1
import client
as gnocchi_client
30 from keystoneclient
.v3
import client
as keystone_client
31 from keystoneauth1
.exceptions
.catalog
import EndpointNotFound
32 from neutronclient
.v2_0
import client
as neutron_client
34 from osm_mon
.collector
.metric
import Metric
35 from osm_mon
.collector
.utils
.openstack
import OpenstackUtils
36 from osm_mon
.collector
.vnf_collectors
.base_vim
import BaseVimCollector
37 from osm_mon
.collector
.vnf_metric
import VnfMetric
38 from osm_mon
.core
.common_db
import CommonDbClient
39 from osm_mon
.core
.config
import Config
41 log
= logging
.getLogger(__name__
)
44 "average_memory_utilization": "memory.usage",
45 "disk_read_ops": "disk.read.requests.rate",
46 "disk_write_ops": "disk.write.requests.rate",
47 "disk_read_bytes": "disk.read.bytes.rate",
48 "disk_write_bytes": "disk.write.bytes.rate",
49 "packets_in_dropped": "network.outgoing.packets.drop",
50 "packets_out_dropped": "network.incoming.packets.drop",
51 "packets_received": "network.incoming.packets.rate",
52 "packets_sent": "network.outgoing.packets.rate",
53 "cpu_utilization": "cpu_util",
56 INTERFACE_METRICS
= ['packets_in_dropped', 'packets_out_dropped', 'packets_received', 'packets_sent']
59 class MetricType(Enum
):
61 INTERFACE_ALL
= 'interface_all'
62 INTERFACE_ONE
= 'interface_one'
65 class OpenstackCollector(BaseVimCollector
):
66 def __init__(self
, config
: Config
, vim_account_id
: str):
67 super().__init
__(config
, vim_account_id
)
68 self
.common_db
= CommonDbClient(config
)
69 vim_account
= self
.common_db
.get_vim_account(vim_account_id
)
70 self
.backend
= self
._get
_backend
(vim_account
)
72 def _build_keystone_client(self
, vim_account
: dict) -> keystone_client
.Client
:
73 sess
= OpenstackUtils
.get_session(vim_account
)
74 return keystone_client
.Client(session
=sess
)
76 def _get_resource_uuid(self
, nsr_id
: str, vnf_member_index
: str, vdur_name
: str) -> str:
77 vdur
= self
.common_db
.get_vdur(nsr_id
, vnf_member_index
, vdur_name
)
80 def collect(self
, vnfr
: dict) -> List
[Metric
]:
81 nsr_id
= vnfr
['nsr-id-ref']
82 vnf_member_index
= vnfr
['member-vnf-index-ref']
83 vnfd
= self
.common_db
.get_vnfd(vnfr
['vnfd-id'])
85 # Populate extra tags for metrics
87 tags
['ns_name'] = self
.common_db
.get_nsr(nsr_id
)['name']
88 if vnfr
['_admin']['projects_read']:
89 tags
['project_id'] = vnfr
['_admin']['projects_read'][0]
91 tags
['project_id'] = None
94 for vdur
in vnfr
['vdur']:
95 # This avoids errors when vdur records have not been completely filled
96 if 'name' not in vdur
:
99 filter(lambda vdu
: vdu
['id'] == vdur
['vdu-id-ref'], vnfd
['vdu'])
101 if 'monitoring-param' in vdu
:
102 for param
in vdu
['monitoring-param']:
103 metric_name
= param
['nfvi-metric']
104 interface_name
= param
['interface-name-ref'] if 'interface-name-ref' in param
else None
105 openstack_metric_name
= METRIC_MAPPINGS
[metric_name
]
106 metric_type
= self
._get
_metric
_type
(metric_name
, interface_name
)
108 resource_id
= self
._get
_resource
_uuid
(nsr_id
, vnf_member_index
, vdur
['name'])
111 "Could not find resource_uuid for vdur %s, vnf_member_index %s, nsr_id %s. "
112 "Was it recently deleted?",
113 vdur
['name'], vnf_member_index
, nsr_id
)
116 value
= self
.backend
.collect_metric(metric_type
, openstack_metric_name
, resource_id
,
118 if value
is not None:
120 tags
['interface'] = interface_name
121 metric
= VnfMetric(nsr_id
, vnf_member_index
, vdur
['name'], metric_name
, value
, tags
)
122 metrics
.append(metric
)
124 log
.exception("Error collecting metric %s for vdu %s" % (metric_name
, vdur
['name']))
127 def _get_backend(self
, vim_account
: dict):
129 ceilometer
= CeilometerBackend(vim_account
)
130 ceilometer
.client
.capabilities
.get()
132 except (HTTPException
, EndpointNotFound
):
133 gnocchi
= GnocchiBackend(vim_account
)
134 gnocchi
.client
.metric
.list(limit
=1)
137 def _get_metric_type(self
, metric_name
: str, interface_name
: str) -> MetricType
:
138 if metric_name
not in INTERFACE_METRICS
:
139 return MetricType
.INSTANCE
142 return MetricType
.INTERFACE_ONE
143 return MetricType
.INTERFACE_ALL
146 class OpenstackBackend
:
147 def collect_metric(self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str, interface_name
: str):
151 class GnocchiBackend(OpenstackBackend
):
153 def __init__(self
, vim_account
: dict):
154 self
.client
= self
._build
_gnocchi
_client
(vim_account
)
155 self
.neutron
= self
._build
_neutron
_client
(vim_account
)
157 def _build_gnocchi_client(self
, vim_account
: dict) -> gnocchi_client
.Client
:
158 sess
= OpenstackUtils
.get_session(vim_account
)
159 return gnocchi_client
.Client(session
=sess
)
161 def _build_neutron_client(self
, vim_account
: dict) -> neutron_client
.Client
:
162 sess
= OpenstackUtils
.get_session(vim_account
)
163 return neutron_client
.Client(session
=sess
)
165 def collect_metric(self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str, interface_name
: str):
166 if metric_type
== MetricType
.INTERFACE_ONE
:
167 return self
._collect
_interface
_one
_metric
(metric_name
, resource_id
, interface_name
)
169 if metric_type
== MetricType
.INTERFACE_ALL
:
170 return self
._collect
_interface
_all
_metric
(metric_name
, resource_id
)
172 elif metric_type
== MetricType
.INSTANCE
:
173 return self
._collect
_instance
_metric
(metric_name
, resource_id
)
176 raise Exception('Unknown metric type %s' % metric_type
.value
)
178 def _collect_interface_one_metric(self
, metric_name
, resource_id
, interface_name
):
179 ports
= self
.neutron
.list_ports(name
=interface_name
, device_id
=resource_id
)
180 if not ports
or not ports
['ports']:
182 'Port not found for interface %s on instance %s' % (interface_name
, resource_id
))
183 port
= ports
['ports'][0]
184 port_uuid
= port
['id'][:11]
185 tap_name
= 'tap' + port_uuid
186 interfaces
= self
.client
.resource
.search(resource_type
='instance_network_interface',
187 query
={'=': {'name': tap_name
}})
188 measures
= self
.client
.metric
.get_measures(metric_name
,
189 resource_id
=interfaces
[0]['id'],
191 return measures
[-1][2] if measures
else None
193 def _collect_interface_all_metric(self
, openstack_metric_name
, resource_id
):
195 interfaces
= self
.client
.resource
.search(resource_type
='instance_network_interface',
196 query
={'=': {'instance_id': resource_id
}})
197 for interface
in interfaces
:
199 measures
= self
.client
.metric
.get_measures(openstack_metric_name
,
200 resource_id
=interface
['id'],
203 if not total_measure
:
205 total_measure
+= measures
[-1][2]
207 except gnocchiclient
.exceptions
.NotFound
as e
:
208 log
.debug("No metric %s found for interface %s: %s", openstack_metric_name
,
212 def _collect_instance_metric(self
, openstack_metric_name
, resource_id
):
215 measures
= self
.client
.metric
.get_measures(openstack_metric_name
,
216 resource_id
=resource_id
,
219 value
= measures
[-1][2]
220 except gnocchiclient
.exceptions
.NotFound
as e
:
221 log
.debug("No metric %s found for instance %s: %s", openstack_metric_name
, resource_id
,
226 class CeilometerBackend(OpenstackBackend
):
227 def __init__(self
, vim_account
: dict):
228 self
.client
= self
._build
_ceilometer
_client
(vim_account
)
230 def _build_ceilometer_client(self
, vim_account
: dict) -> ceilometer_client
.Client
:
231 sess
= OpenstackUtils
.get_session(vim_account
)
232 return ceilometer_client
.Client("2", session
=sess
)
234 def collect_metric(self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str, interface_name
: str):
235 if metric_type
!= MetricType
.INSTANCE
:
236 raise NotImplementedError('Ceilometer backend only support instance metrics')
237 measures
= self
.client
.samples
.list(meter_name
=metric_name
, limit
=1, q
=[
238 {'field': 'resource_id', 'op': 'eq', 'value': resource_id
}])
239 return measures
[0].counter_volume
if measures
else None