From edee5d4275a62a7fcafa33a9fe7fa951b8e8edc5 Mon Sep 17 00:00:00 2001 From: Qiaowei Ren Date: Mon, 18 Aug 2014 10:28:05 +0800 Subject: [PATCH] XenAPI support: Disk rates Implemented inspect_disk_rates() to inspect the disk statistics as rates for an instance. Change-Id: I0220e612b156a967745ff281ce09329fa0d15a12 Implements: blueprint xenapi-support --- ceilometer/compute/virt/xenapi/inspector.py | 24 ++++++++++++ .../compute/virt/xenapi/test_inspector.py | 37 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/ceilometer/compute/virt/xenapi/inspector.py b/ceilometer/compute/virt/xenapi/inspector.py index 829d2cac..ad840638 100644 --- a/ceilometer/compute/virt/xenapi/inspector.py +++ b/ceilometer/compute/virt/xenapi/inspector.py @@ -167,3 +167,27 @@ class XenapiInspector(virt_inspector.Inspector): tx_rate = float(vif_metrics_rec['io_write_kbs']) * units.Ki stats = virt_inspector.InterfaceRateStats(rx_rate, tx_rate) yield (interface, stats) + + def inspect_disk_rates(self, instance, duration=None): + instance_name = util.instance_name(instance) + vm_ref = self._lookup_by_name(instance_name) + vbd_refs = self._call_xenapi("VM.get_VBDs", vm_ref) + if vbd_refs: + for vbd_ref in vbd_refs: + vbd_rec = self._call_xenapi("VBD.get_record", vbd_ref) + vbd_metrics_ref = self._call_xenapi("VBD.get_metrics", + vbd_ref) + vbd_metrics_rec = self._call_xenapi("VBD_metrics.get_record", + vbd_metrics_ref) + + disk = virt_inspector.Disk(device=vbd_rec['device']) + # Stats provided from XenServer are in KB/s, + # converting it to B/s. + read_rate = float(vbd_metrics_rec['io_read_kbs']) * units.Ki + write_rate = float(vbd_metrics_rec['io_write_kbs']) * units.Ki + disk_rate_info = virt_inspector.DiskRateStats( + read_bytes_rate=read_rate, + read_requests_rate=0, + write_bytes_rate=write_rate, + write_requests_rate=0) + yield(disk, disk_rate_info) diff --git a/ceilometer/tests/compute/virt/xenapi/test_inspector.py b/ceilometer/tests/compute/virt/xenapi/test_inspector.py index a7239b42..2f59ae5e 100644 --- a/ceilometer/tests/compute/virt/xenapi/test_inspector.py +++ b/ceilometer/tests/compute/virt/xenapi/test_inspector.py @@ -139,3 +139,40 @@ class TestXenapiInspection(base.BaseTestCase): self.assertEqual('vif_mac', vnic0.mac) self.assertEqual(1024, info0.rx_bytes_rate) self.assertEqual(2048, info0.tx_bytes_rate) + + def test_inspect_disk_rates(self): + fake_instance = {'OS-EXT-SRV-ATTR:instance_name': 'fake_instance_name', + 'id': 'fake_instance_id'} + + def fake_xenapi_request(method, args): + vbd_rec = { + 'device': 'xvdd' + } + + vbd_metrics_rec = { + 'io_read_kbs': '1', + 'io_write_kbs': '2' + } + if method == 'VM.get_by_name_label': + return ['vm_ref'] + elif method == 'VM.get_VBDs': + return ['vbd_ref'] + elif method == 'VBD.get_record': + return vbd_rec + elif method == 'VBD.get_metrics': + return 'vbd_metrics_ref' + elif method == 'VBD_metrics.get_record': + return vbd_metrics_rec + else: + return None + + session = self.inspector.session + with mock.patch.object(session, 'xenapi_request', + side_effect=fake_xenapi_request): + disks = list(self.inspector.inspect_disk_rates(fake_instance)) + + self.assertEqual(1, len(disks)) + disk0, info0 = disks[0] + self.assertEqual('xvdd', disk0.device) + self.assertEqual(1024, info0.read_bytes_rate) + self.assertEqual(2048, info0.write_bytes_rate)