def test_monitor_metric_list_to_list(self): obj = objects.MonitorMetric(name='cpu.frequency', value=1000, timestamp=_ts_now, source='nova.virt.libvirt.driver') list_obj = objects.MonitorMetricList(objects=[obj]) self.assertEqual(_monitor_metric_list_spec, list_obj.to_list())
def _get_host_metrics(self, context, nodename): """Get the metrics from monitors and notify information to message bus. """ metrics = objects.MonitorMetricList() metrics_info = {} for monitor in self.monitors: try: monitor.add_metrics_to_list(metrics) except Exception as exc: LOG.warning( _LW("Cannot get the metrics from %(mon)s; " "error: %(exc)s"), { 'mon': monitor, 'exc': exc }) # TODO(jaypipes): Remove this when compute_node.metrics doesn't need # to be populated as a JSON-ified string. metrics = metrics.to_list() if len(metrics): metrics_info['nodename'] = nodename metrics_info['metrics'] = metrics metrics_info['host'] = self.host metrics_info['host_ip'] = CONF.my_ip notifier = rpc.get_notifier(service='compute', host=nodename) notifier.info(context, 'compute.metrics.update', metrics_info) return metrics
def test_get_metrics(self): metrics = objects.MonitorMetricList() monitor = virt_driver.Monitor(FakeResourceTracker()) monitor.add_metrics_to_list(metrics) names = monitor.get_metric_names() for metric in metrics.objects: self.assertIn(metric.name, names) # Some conversion to a dict to ease testing... metrics = {m.name: m.value for m in metrics.objects} self.assertEqual(metrics["cpu.frequency"], 800) self.assertEqual(metrics["cpu.user.time"], 26728850000000) self.assertEqual(metrics["cpu.kernel.time"], 5664160000000) self.assertEqual(metrics["cpu.idle.time"], 1592705190000000) self.assertEqual(metrics["cpu.iowait.time"], 6121490000000) self.assertTrue(metrics["cpu.user.percent"] <= 1 and metrics["cpu.user.percent"] >= 0) self.assertTrue(metrics["cpu.kernel.percent"] <= 1 and metrics["cpu.kernel.percent"] >= 0) self.assertTrue(metrics["cpu.idle.percent"] <= 1 and metrics["cpu.idle.percent"] >= 0) self.assertTrue(metrics["cpu.iowait.percent"] <= 1 and metrics["cpu.iowait.percent"] >= 0) self.assertTrue(metrics["cpu.percent"] <= 1 and metrics["cpu.percent"] >= 0)
def test_ensure_single_sampling(self): # We want to ensure that the virt driver's get_host_cpu_stats() # is only ever called once, otherwise values for monitor metrics # might be illogical -- e.g. pct cpu times for user/system/idle # may add up to more than 100. metrics = objects.MonitorMetricList() monitor = virt_driver.Monitor(FakeResourceTracker()) with mock.patch.object(FakeDriver, 'get_host_cpu_stats') as mocked: monitor.populate_metrics(metrics) mocked.assert_called_once_with()
def test_metrics_filter_missing_metrics(self): _ts_now = datetime.datetime(2015, 11, 11, 11, 0, 0) obj1 = objects.MonitorMetric(name='cpu.frequency', value=1000, timestamp=_ts_now, source='nova.virt.libvirt.driver') metrics_list = objects.MonitorMetricList(objects=[obj1]) self.flags(weight_setting=['foo=1', 'bar=2'], group='metrics') filt_cls = metrics_filter.MetricsFilter() host = fakes.FakeHostState('host1', 'node1', attribute_dict={'metrics': metrics_list}) self.assertFalse(filt_cls.host_passes(host, None))
def _get_fake_host_state(self, index=0): host_state = host_manager.HostState('host_%s' % index, 'node_%s' % index) host_state.free_ram_mb = 50000 host_state.total_usable_ram_mb = 50000 host_state.free_disk_mb = 4096 host_state.service = { "disabled": False, "updated_at": timeutils.utcnow(), "created_at": timeutils.utcnow(), } host_state.cpu_allocation_ratio = 16.0 host_state.ram_allocation_ratio = 1.5 host_state.metrics = objects.MonitorMetricList(objects=[]) return host_state
def test_metrics_filter_pass(self): _ts_now = datetime.datetime(2015, 11, 11, 11, 0, 0) obj1 = objects.MonitorMetric(name='cpu.frequency', value=1000, timestamp=_ts_now, source='nova.virt.libvirt.driver') obj2 = objects.MonitorMetric(name='numa.membw.current', numa_membw_values={"0": 10, "1": 43}, timestamp=_ts_now, source='nova.virt.libvirt.driver') metrics_list = objects.MonitorMetricList(objects=[obj1, obj2]) self.flags(weight_setting=[ 'cpu.frequency=1', 'numa.membw.current=2'], group='metrics') filt_cls = metrics_filter.MetricsFilter() host = fakes.FakeHostState('host1', 'node1', attribute_dict={'metrics': metrics_list}) self.assertTrue(filt_cls.host_passes(host, None))