diff --git a/vmware_nsx/neutron/plugins/vmware/vshield/edge_loadbalancer_driver.py b/vmware_nsx/neutron/plugins/vmware/vshield/edge_loadbalancer_driver.py index a644d5fc49..7ec8baaf2b 100644 --- a/vmware_nsx/neutron/plugins/vmware/vshield/edge_loadbalancer_driver.py +++ b/vmware_nsx/neutron/plugins/vmware/vshield/edge_loadbalancer_driver.py @@ -776,10 +776,29 @@ class EdgeLbDriver(object): self._lb_driver.delete_pool_health_monitor_successful( context, health_monitor, pool_id, mon_mapping) - def stats(self, context, pool_id): + def stats(self, context, pool_id, pool_mapping): LOG.debug('Retrieving stats for pool %s', pool_id) - # NSXv LBaaS API doesn't provide stats + try: + lb_stats = self.vcns.get_loadbalancer_statistics( + pool_mapping['edge_id']) + + pools_stats = lb_stats.get('pool', []) + for pool_stats in pools_stats: + if pool_stats['poolId'] == pool_mapping['edge_pool_id']: + return {'bytes_in': pool_stats.get('bytesIn', 0), + 'bytes_out': pool_stats.get('bytesOut', 0), + 'active_connections': + pool_stats.get('curSessions', 0), + 'total_connections': + pool_stats.get('totalSessions', 0)} + + except nsxv_exc.VcnsApiException: + with excutils.save_and_reraise_exception(): + LOG.error( + _LE('Failed to read load balancer statistics, edge: %s'), + pool_mapping['edge_id']) + return {'bytes_in': 0, 'bytes_out': 0, 'active_connections': 0, diff --git a/vmware_nsx/neutron/plugins/vmware/vshield/vcns.py b/vmware_nsx/neutron/plugins/vmware/vshield/vcns.py index 4223454f9a..d209ae00d0 100644 --- a/vmware_nsx/neutron/plugins/vmware/vshield/vcns.py +++ b/vmware_nsx/neutron/plugins/vmware/vshield/vcns.py @@ -44,6 +44,7 @@ SPOOFGUARD_PREFIX = '/api/4.0/services/spoofguard' #LbaaS Constants LOADBALANCER_SERVICE = "loadbalancer/config" +LOADBALANCER_STATS = "loadbalancer/statistics" VIP_RESOURCE = "virtualservers" POOL_RESOURCE = "pools" MONITOR_RESOURCE = "monitors" @@ -200,6 +201,10 @@ class Vcns(object): uri = self._build_uri_path(edge_id, LOADBALANCER_SERVICE) return self.do_request(HTTP_GET, uri, decode=True) + def get_loadbalancer_statistics(self, edge_id): + uri = self._build_uri_path(edge_id, LOADBALANCER_STATS) + return self.do_request(HTTP_GET, uri, decode=True) + def enable_service_loadbalancer(self, edge_id, config): uri = self._build_uri_path(edge_id, LOADBALANCER_SERVICE) return self.do_request(HTTP_PUT, uri, config) diff --git a/vmware_nsx/neutron/tests/unit/vmware/nsx_v/test_edge_loadbalancer_driver.py b/vmware_nsx/neutron/tests/unit/vmware/nsx_v/test_edge_loadbalancer_driver.py index 36c3e5a0d0..642976b1a0 100644 --- a/vmware_nsx/neutron/tests/unit/vmware/nsx_v/test_edge_loadbalancer_driver.py +++ b/vmware_nsx/neutron/tests/unit/vmware/nsx_v/test_edge_loadbalancer_driver.py @@ -600,3 +600,49 @@ class TestEdgeLbDriver(base.BaseTestCase): mock_del_mon.assert_called_with(EDGE_ID, EDGE_MON_ID) mock_del_successful.assert_called_with(self.context, hmon, POOL_ID, mon_mapping) + + def test_stats(self): + pool_mapping = {'edge_id': EDGE_ID, 'edge_pool_id': EDGE_POOL_ID} + pool_stats = { + 'timeStamp': 1427358733, + 'virtualServer': [ + {'name': 'MdSrv', + 'virtualServerId': 'virtualServer-1', + 'bytesIn': 0, + 'bytesOut': 0, + 'totalSessions': 0, + 'ipAddress': '169.254.128.2', + 'curSessions': 0}], + 'pool': [ + {'status': 'UP', + 'totalSessions': 10000, + 'rateMax': 0, + 'name': 'MDSrvPool', + 'bytesOut': 100000, + 'rateLimit': 0, + 'member': [ + {'status': 'UP', + 'name': 'Member-1', + 'bytesOut': 0, + 'memberId': 'member-1', + 'totalSessions': 20000, + 'ipAddress': '192.168.55.101', + 'httpReqRateMax': 0, + 'curSessions': 0, + 'bytesIn': 0}], + 'poolId': EDGE_POOL_ID, + 'maxSessions': 10000, + 'httpReqRateMax': 0, + 'curSessions': 5000, + 'bytesIn': 1000000}]} + expected_stats = { + 'active_connections': 5000, + 'bytes_in': 1000000, + 'bytes_out': 100000, + 'total_connections': 10000} + + with mock.patch.object(self.edge_driver.vcns, + 'get_loadbalancer_statistics', + return_value=pool_stats): + stats = self.edge_driver.stats(self.context, POOL_ID, pool_mapping) + self.assertEqual(stats, expected_stats)