From ae2673cdb5bce7ce11376cef25d0b79491dc5ff5 Mon Sep 17 00:00:00 2001 From: zhangguoqing Date: Mon, 7 Mar 2016 14:20:55 +0800 Subject: [PATCH] Ignoring cpu measurement when instance's state is SHUTOFF. Using libvirt, we get the cpu and cpu_util are all 0.0 value when the instance is SHUTOFF, but other meters(eg:memory) are skiped. So this patch will fix it that ignore cpu pollster and give a WARNING, when the instance's state is SHUTOFF. Change-Id: I0df2f9688c10621d685c0e895367252240035e42 Closes-Bug: #1544857 --- ceilometer/compute/pollsters/cpu.py | 7 ++++++- ceilometer/compute/virt/libvirt/inspector.py | 2 +- .../tests/unit/compute/virt/libvirt/test_inspector.py | 11 +++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ceilometer/compute/pollsters/cpu.py b/ceilometer/compute/pollsters/cpu.py index 81b62e3f..9704c392 100644 --- a/ceilometer/compute/pollsters/cpu.py +++ b/ceilometer/compute/pollsters/cpu.py @@ -20,7 +20,7 @@ import ceilometer from ceilometer.compute import pollsters from ceilometer.compute.pollsters import util from ceilometer.compute.virt import inspector as virt_inspector -from ceilometer.i18n import _ +from ceilometer.i18n import _, _LW from ceilometer import sample LOG = log.getLogger(__name__) @@ -48,6 +48,11 @@ class CPUPollster(pollsters.BaseComputePollster): except virt_inspector.InstanceNotFoundException as err: # Instance was deleted while getting samples. Ignore it. LOG.debug('Exception while getting samples %s', err) + except virt_inspector.InstanceShutOffException as e: + LOG.warning(_LW('Instance %(instance_id)s was shut off while ' + 'getting samples of %(pollster)s: %(exc)s'), + {'instance_id': instance.id, + 'pollster': self.__class__.__name__, 'exc': e}) except ceilometer.NotImplementedError: # Selected inspector does not implement this pollster. LOG.debug('Obtaining CPU time is not implemented for %s', diff --git a/ceilometer/compute/virt/libvirt/inspector.py b/ceilometer/compute/virt/libvirt/inspector.py index 83b52486..6f879c32 100644 --- a/ceilometer/compute/virt/libvirt/inspector.py +++ b/ceilometer/compute/virt/libvirt/inspector.py @@ -110,7 +110,7 @@ class LibvirtInspector(virt_inspector.Inspector): raise virt_inspector.InstanceNotFoundException(msg) def inspect_cpus(self, instance): - domain = self._lookup_by_uuid(instance) + domain = self._get_domain_not_shut_off_or_raise(instance) dom_info = domain.info() return virt_inspector.CPUStats(number=dom_info[3], time=dom_info[4]) diff --git a/ceilometer/tests/unit/compute/virt/libvirt/test_inspector.py b/ceilometer/tests/unit/compute/virt/libvirt/test_inspector.py index 7f5108df..16220cfa 100644 --- a/ceilometer/tests/unit/compute/virt/libvirt/test_inspector.py +++ b/ceilometer/tests/unit/compute/virt/libvirt/test_inspector.py @@ -60,6 +60,17 @@ class TestLibvirtInspection(base.BaseTestCase): self.assertEqual(2, cpu_info.number) self.assertEqual(999999, cpu_info.time) + def test_inspect_cpus_with_domain_shutoff(self): + connection = self.inspector.connection + with mock.patch.object(connection, 'lookupByUUIDString', + return_value=self.domain): + with mock.patch.object(self.domain, 'info', + return_value=(5, 0, 0, + 2, 999999)): + self.assertRaises(virt_inspector.InstanceShutOffException, + self.inspector.inspect_cpus, + self.instance) + def test_inspect_vnics(self): dom_xml = """