# For those usages not covered by the Apache License, Version 2.0 please
# contact: bdiaz@whitestack.com or glavado@whitestack.com
##
-import datetime
-import json
import logging
from enum import Enum
from typing import List
vdur = self.common_db.get_vdur(nsr_id, vnf_member_index, vdur_name)
return vdur['vim-id']
- def _get_granularity(self, vim_account_id: str):
- creds = CollectorUtils.get_credentials(vim_account_id)
- vim_config = json.loads(creds.config)
- if 'granularity' in vim_config:
- return int(vim_config['granularity'])
- else:
- return int(self.conf.get('openstack', 'default_granularity'))
-
def collect(self, vnfr: dict) -> List[Metric]:
nsr_id = vnfr['nsr-id-ref']
vnf_member_index = vnfr['member-vnf-index-ref']
ceilometer.client.capabilities.get()
return ceilometer
except EndpointNotFound:
- granularity = self._get_granularity(vim_account_id)
- gnocchi = GnocchiBackend(vim_account_id, granularity)
+ gnocchi = GnocchiBackend(vim_account_id)
gnocchi.client.status.get()
return gnocchi
class GnocchiBackend(OpenstackBackend):
- def __init__(self, vim_account_id: str, granularity: int):
+ def __init__(self, vim_account_id: str):
self.client = self._build_gnocchi_client(vim_account_id)
self.neutron = self._build_neutron_client(vim_account_id)
- self.granularity = granularity
def _build_gnocchi_client(self, vim_account_id: str) -> gnocchi_client.Client:
sess = OpenstackBackend.get_session(vim_account_id)
raise Exception('Unknown metric type %s' % metric_type.value)
def _collect_interface_one_metric(self, metric_name, resource_id, interface_name):
- delta = 10 * self.granularity
- start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta)
ports = self.neutron.list_ports(name=interface_name, device_id=resource_id)
if not ports or not ports['ports']:
raise Exception(
interfaces = self.client.resource.search(resource_type='instance_network_interface',
query={'=': {'name': tap_name}})
measures = self.client.metric.get_measures(metric_name,
- start=start_date,
resource_id=interfaces[0]['id'],
- granularity=self.granularity)
+ limit=1)
return measures[-1][2] if measures else None
def _collect_interface_all_metric(self, openstack_metric_name, resource_id):
- delta = 10 * self.granularity
- start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta)
total_measure = None
interfaces = self.client.resource.search(resource_type='instance_network_interface',
query={'=': {'instance_id': resource_id}})
for interface in interfaces:
try:
measures = self.client.metric.get_measures(openstack_metric_name,
- start=start_date,
resource_id=interface['id'],
- granularity=self.granularity)
+ limit=1)
if measures:
if not total_measure:
total_measure = 0.0
return total_measure
def _collect_instance_metric(self, openstack_metric_name, resource_id):
- delta = 10 * self.granularity
- start_date = datetime.datetime.now() - datetime.timedelta(seconds=delta)
value = None
try:
measures = self.client.metric.get_measures(openstack_metric_name,
- start=start_date,
resource_id=resource_id,
- granularity=self.granularity)
+ limit=1)
if measures:
value = measures[-1][2]
except gnocchiclient.exceptions.NotFound as e:
'+00:00')), 60.0, 0.0333070363)]
build_gnocchi_client.return_value = mock_gnocchi_client
- backend = GnocchiBackend('test_uuid', 60)
+ backend = GnocchiBackend('test_uuid')
value = backend._collect_instance_metric('cpu_utilization', 'test_resource_id')
self.assertEqual(value, 0.0333070363)
- mock_gnocchi_client.metric.get_measures.assert_called_once_with('cpu_utilization', granularity=60,
- resource_id='test_resource_id', start=mock.ANY)
+ mock_gnocchi_client.metric.get_measures.assert_called_once_with('cpu_utilization',
+ limit=1,
+ resource_id='test_resource_id')
@mock.patch.object(GnocchiBackend, '_build_neutron_client')
@mock.patch.object(GnocchiBackend, '_build_gnocchi_client')
build_gnocchi_client.return_value = mock_gnocchi_client
build_neutron_client.return_value = mock_neutron_client
- backend = GnocchiBackend('test_uuid', 60)
+ backend = GnocchiBackend('test_uuid')
value = backend._collect_interface_one_metric('packets_received', 'test_resource_id', 'eth0')
self.assertEqual(value, 0.0333070363)
- mock_gnocchi_client.metric.get_measures.assert_called_once_with('packets_received', granularity=60,
- resource_id='test_id', start=mock.ANY)
+ mock_gnocchi_client.metric.get_measures.assert_called_once_with('packets_received', resource_id='test_id',
+ limit=1)
mock_neutron_client.list_ports.assert_called_once_with(device_id='test_resource_id', name='eth0')
@mock.patch.object(GnocchiBackend, '_build_neutron_client')
build_gnocchi_client.return_value = mock_gnocchi_client
- backend = GnocchiBackend('test_uuid', 60)
+ backend = GnocchiBackend('test_uuid')
value = backend._collect_interface_all_metric('packets_received', 'test_resource_id')
self.assertEqual(value, 0.0666140726)
- mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', granularity=60,
- resource_id='test_id_1', start=mock.ANY)
- mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', granularity=60,
- resource_id='test_id_2', start=mock.ANY)
+ mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_1',
+ limit=1)
+ mock_gnocchi_client.metric.get_measures.assert_any_call('packets_received', resource_id='test_id_2',
+ limit=1)