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
26 from typing
import List
28 import gnocchiclient
.exceptions
29 from ceilometerclient
.v2
import client
as ceilometer_client
30 from gnocchiclient
.v1
import client
as gnocchi_client
31 from keystoneauth1
import session
32 from keystoneauth1
.exceptions
.catalog
import EndpointNotFound
33 from keystoneauth1
.identity
import v3
34 from keystoneclient
.v3
import client
as keystone_client
35 from neutronclient
.v2_0
import client
as neutron_client
37 from osm_mon
.collector
.metric
import Metric
38 from osm_mon
.collector
.utils
import CollectorUtils
39 from osm_mon
.collector
.vnf_collectors
.base_vim
import BaseVimCollector
40 from osm_mon
.collector
.vnf_metric
import VnfMetric
41 from osm_mon
.core
.common_db
import CommonDbClient
42 from osm_mon
.core
.config
import Config
44 log
= logging
.getLogger(__name__
)
47 "average_memory_utilization": "memory.usage",
48 "disk_read_ops": "disk.read.requests.rate",
49 "disk_write_ops": "disk.write.requests.rate",
50 "disk_read_bytes": "disk.read.bytes.rate",
51 "disk_write_bytes": "disk.write.bytes.rate",
52 "packets_in_dropped": "network.outgoing.packets.drop",
53 "packets_out_dropped": "network.incoming.packets.drop",
54 "packets_received": "network.incoming.packets.rate",
55 "packets_sent": "network.outgoing.packets.rate",
56 "cpu_utilization": "cpu_util",
59 INTERFACE_METRICS
= ['packets_in_dropped', 'packets_out_dropped', 'packets_received', 'packets_sent']
62 class MetricType(Enum
):
64 INTERFACE_ALL
= 'interface_all'
65 INTERFACE_ONE
= 'interface_one'
68 class OpenstackCollector(BaseVimCollector
):
69 def __init__(self
, config
: Config
, vim_account_id
: str):
70 super().__init
__(config
, vim_account_id
)
72 self
.common_db
= CommonDbClient(config
)
73 self
.backend
= self
._get
_backend
(vim_account_id
)
75 def _build_keystone_client(self
, vim_account_id
: str) -> keystone_client
.Client
:
76 sess
= OpenstackBackend
.get_session(vim_account_id
)
77 return keystone_client
.Client(session
=sess
)
79 def _get_resource_uuid(self
, nsr_id
: str, vnf_member_index
: str, vdur_name
: str) -> str:
80 vdur
= self
.common_db
.get_vdur(nsr_id
, vnf_member_index
, vdur_name
)
83 def _get_granularity(self
, vim_account_id
: str):
84 creds
= CollectorUtils
.get_credentials(vim_account_id
)
85 vim_config
= json
.loads(creds
.config
)
86 if 'granularity' in vim_config
:
87 return int(vim_config
['granularity'])
89 return int(self
.conf
.get('openstack', 'default_granularity'))
91 def collect(self
, vnfr
: dict) -> List
[Metric
]:
92 nsr_id
= vnfr
['nsr-id-ref']
93 vnf_member_index
= vnfr
['member-vnf-index-ref']
94 vnfd
= self
.common_db
.get_vnfd(vnfr
['vnfd-id'])
96 for vdur
in vnfr
['vdur']:
97 # This avoids errors when vdur records have not been completely filled
98 if 'name' not in vdur
:
101 filter(lambda vdu
: vdu
['id'] == vdur
['vdu-id-ref'], vnfd
['vdu'])
103 if 'monitoring-param' in vdu
:
104 for param
in vdu
['monitoring-param']:
105 metric_name
= param
['nfvi-metric']
106 interface_name
= param
['interface-name-ref'] if 'interface-name-ref' in param
else None
107 openstack_metric_name
= METRIC_MAPPINGS
[metric_name
]
108 metric_type
= self
._get
_metric
_type
(metric_name
, interface_name
)
110 resource_id
= self
._get
_resource
_uuid
(nsr_id
, vnf_member_index
, vdur
['name'])
113 "Could not find resource_uuid for vdur %s, vnf_member_index %s, nsr_id %s. "
114 "Was it recently deleted?",
115 vdur
['name'], vnf_member_index
, nsr_id
)
118 value
= self
.backend
.collect_metric(metric_type
, openstack_metric_name
, resource_id
,
120 if value
is not None:
123 tags
['interface'] = interface_name
124 metric
= VnfMetric(nsr_id
, vnf_member_index
, vdur
['name'], metric_name
, value
, tags
)
125 metrics
.append(metric
)
127 log
.exception("Error collecting metric %s for vdu %s" % (metric_name
, vdur
['name']))
130 def _get_backend(self
, vim_account_id
: str):
132 ceilometer
= CeilometerBackend(vim_account_id
)
133 ceilometer
.client
.capabilities
.get()
135 except EndpointNotFound
:
136 granularity
= self
._get
_granularity
(vim_account_id
)
137 gnocchi
= GnocchiBackend(vim_account_id
, granularity
)
138 gnocchi
.client
.status
.get()
141 def _get_metric_type(self
, metric_name
: str, interface_name
: str) -> MetricType
:
142 if metric_name
not in INTERFACE_METRICS
:
143 return MetricType
.INSTANCE
146 return MetricType
.INTERFACE_ONE
147 return MetricType
.INTERFACE_ALL
150 class OpenstackBackend
:
151 def collect_metric(self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str, interface_name
: str):
155 def get_session(vim_account_id
: str):
156 creds
= CollectorUtils
.get_credentials(vim_account_id
)
157 verify_ssl
= CollectorUtils
.is_verify_ssl(creds
)
158 auth
= v3
.Password(auth_url
=creds
.url
,
160 password
=creds
.password
,
161 project_name
=creds
.tenant_name
,
162 project_domain_id
='default',
163 user_domain_id
='default')
164 return session
.Session(auth
=auth
, verify
=verify_ssl
)
167 class GnocchiBackend(OpenstackBackend
):
169 def __init__(self
, vim_account_id
: str, granularity
: int):
170 self
.client
= self
._build
_gnocchi
_client
(vim_account_id
)
171 self
.neutron
= self
._build
_neutron
_client
(vim_account_id
)
172 self
.granularity
= granularity
174 def _build_gnocchi_client(self
, vim_account_id
: str) -> gnocchi_client
.Client
:
175 sess
= OpenstackBackend
.get_session(vim_account_id
)
176 return gnocchi_client
.Client(session
=sess
)
178 def _build_neutron_client(self
, vim_account_id
: str) -> neutron_client
.Client
:
179 sess
= OpenstackBackend
.get_session(vim_account_id
)
180 return neutron_client
.Client(session
=sess
)
182 def collect_metric(self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str, interface_name
: str):
183 if metric_type
== MetricType
.INTERFACE_ONE
:
184 return self
._collect
_interface
_one
_metric
(metric_name
, resource_id
, interface_name
)
186 if metric_type
== MetricType
.INTERFACE_ALL
:
187 return self
._collect
_interface
_all
_metric
(metric_name
, resource_id
)
189 elif metric_type
== MetricType
.INSTANCE
:
190 return self
._collect
_instance
_metric
(metric_name
, resource_id
)
193 raise Exception('Unknown metric type %s' % metric_type
.value
)
195 def _collect_interface_one_metric(self
, metric_name
, resource_id
, interface_name
):
196 delta
= 10 * self
.granularity
197 start_date
= datetime
.datetime
.now() - datetime
.timedelta(seconds
=delta
)
198 ports
= self
.neutron
.list_ports(name
=interface_name
, device_id
=resource_id
)
199 if not ports
or not ports
['ports']:
201 'Port not found for interface %s on instance %s' % (interface_name
, resource_id
))
202 port
= ports
['ports'][0]
203 port_uuid
= port
['id'][:11]
204 tap_name
= 'tap' + port_uuid
205 interfaces
= self
.client
.resource
.search(resource_type
='instance_network_interface',
206 query
={'=': {'name': tap_name
}})
207 measures
= self
.client
.metric
.get_measures(metric_name
,
209 resource_id
=interfaces
[0]['id'],
210 granularity
=self
.granularity
)
211 return measures
[-1][2] if measures
else None
213 def _collect_interface_all_metric(self
, openstack_metric_name
, resource_id
):
214 delta
= 10 * self
.granularity
215 start_date
= datetime
.datetime
.now() - datetime
.timedelta(seconds
=delta
)
217 interfaces
= self
.client
.resource
.search(resource_type
='instance_network_interface',
218 query
={'=': {'instance_id': resource_id
}})
219 for interface
in interfaces
:
221 measures
= self
.client
.metric
.get_measures(openstack_metric_name
,
223 resource_id
=interface
['id'],
224 granularity
=self
.granularity
)
226 if not total_measure
:
228 total_measure
+= measures
[-1][2]
230 except gnocchiclient
.exceptions
.NotFound
as e
:
231 log
.debug("No metric %s found for interface %s: %s", openstack_metric_name
,
235 def _collect_instance_metric(self
, openstack_metric_name
, resource_id
):
236 delta
= 10 * self
.granularity
237 start_date
= datetime
.datetime
.now() - datetime
.timedelta(seconds
=delta
)
240 measures
= self
.client
.metric
.get_measures(openstack_metric_name
,
242 resource_id
=resource_id
,
243 granularity
=self
.granularity
)
245 value
= measures
[-1][2]
246 except gnocchiclient
.exceptions
.NotFound
as e
:
247 log
.debug("No metric %s found for instance %s: %s", openstack_metric_name
, resource_id
,
252 class CeilometerBackend(OpenstackBackend
):
253 def __init__(self
, vim_account_id
: str):
254 self
.client
= self
._build
_ceilometer
_client
(vim_account_id
)
256 def _build_ceilometer_client(self
, vim_account_id
: str) -> ceilometer_client
.Client
:
257 sess
= OpenstackBackend
.get_session(vim_account_id
)
258 return ceilometer_client
.Client(session
=sess
)
260 def collect_metric(self
, metric_type
: MetricType
, metric_name
: str, resource_id
: str, interface_name
: str):
261 if metric_type
!= MetricType
.INSTANCE
:
262 raise NotImplementedError('Ceilometer backend only support instance metrics')
263 measures
= self
.client
.samples
.list(meter_name
=metric_name
, limit
=1, q
=[
264 {'field': 'resource_id', 'op': 'eq', 'value': resource_id
}])
265 return measures
[0].counter_volume
if measures
else None