Browse Source

Add cpu util to MesosMonitor

Cpu metric is important to a clutser, add cpu util to notification bus.

After this patch get applied, we will get following metrics:

{'metrics': [{'unit': '%', 'name': 'cpu_util', 'value': 30.0}, {'unit':
    '%', 'name': 'memory_util', 'value': 4.795204795204795}]

Change-Id: I44cc2edf2a33ff2991ba0b006d802dc3efee42b6
changes/03/299103/5
Eli Qiao 6 years ago committed by Eli Qiao
parent
commit
196eae1361
  1. 16
      magnum/conductor/mesos_monitor.py
  2. 46
      magnum/tests/unit/conductor/test_monitors.py

16
magnum/conductor/mesos_monitor.py

@ -29,6 +29,10 @@ class MesosMonitor(MonitorBase):
'unit': '%',
'func': 'compute_memory_util',
},
'cpu_util': {
'unit': '%',
'func': 'compute_cpu_util',
},
}
def _build_url(self, url, protocol='http', port='80', path='/'):
@ -40,6 +44,8 @@ class MesosMonitor(MonitorBase):
def pull_data(self):
self.data['mem_total'] = 0
self.data['mem_used'] = 0
self.data['cpu_total'] = 0
self.data['cpu_used'] = 0
for master_addr in self.bay.master_addresses:
mesos_master_url = self._build_url(master_addr, port='5050',
path='/state')
@ -48,10 +54,18 @@ class MesosMonitor(MonitorBase):
for slave in master['slaves']:
self.data['mem_total'] += slave['resources']['mem']
self.data['mem_used'] += slave['used_resources']['mem']
self.data['cpu_total'] += slave['resources']['cpus']
self.data['cpu_used'] += slave['used_resources']['cpus']
break
def compute_memory_util(self):
if self.data['mem_total'] == 0:
if self.data['mem_total'] == 0 or self.data['mem_used'] == 0:
return 0
else:
return self.data['mem_used'] * 100 / self.data['mem_total']
def compute_cpu_util(self):
if self.data['cpu_used'] == 0:
return 0
else:
return self.data['cpu_used'] * 100 / self.data['cpu_total']

46
magnum/tests/unit/conductor/test_monitors.py

@ -219,7 +219,7 @@ class MonitorsTestCase(base.TestCase):
def _test_mesos_monitor_pull_data(
self, mock_url_get, state_json, expected_mem_total,
expected_mem_used):
expected_mem_used, expected_cpu_total, expected_cpu_used):
state_json = jsonutils.dumps(state_json)
mock_url_get.return_value = state_json
self.mesos_monitor.pull_data()
@ -227,6 +227,10 @@ class MonitorsTestCase(base.TestCase):
expected_mem_total)
self.assertEqual(self.mesos_monitor.data['mem_used'],
expected_mem_used)
self.assertEqual(self.mesos_monitor.data['cpu_total'],
expected_cpu_total)
self.assertEqual(self.mesos_monitor.data['cpu_used'],
expected_cpu_used)
@mock.patch('magnum.common.urlfetch.get')
def test_mesos_monitor_pull_data_success(self, mock_url_get):
@ -235,14 +239,17 @@ class MonitorsTestCase(base.TestCase):
'pid': 'master@10.0.0.6:5050',
'slaves': [{
'resources': {
'mem': 100
'mem': 100,
'cpus': 1,
},
'used_resources': {
'mem': 50
'mem': 50,
'cpus': 0.2,
}
}]
}
self._test_mesos_monitor_pull_data(mock_url_get, state_json, 100, 50)
self._test_mesos_monitor_pull_data(mock_url_get, state_json,
100, 50, 1, 0.2)
@mock.patch('magnum.common.urlfetch.get')
def test_mesos_monitor_pull_data_success_not_leader(self, mock_url_get):
@ -251,12 +258,13 @@ class MonitorsTestCase(base.TestCase):
'pid': 'master@1.1.1.1:5050',
'slaves': []
}
self._test_mesos_monitor_pull_data(mock_url_get, state_json, 0, 0)
self._test_mesos_monitor_pull_data(mock_url_get, state_json,
0, 0, 0, 0)
@mock.patch('magnum.common.urlfetch.get')
def test_mesos_monitor_pull_data_success_no_master(self, mock_url_get):
self.bay.master_addresses = []
self._test_mesos_monitor_pull_data(mock_url_get, {}, 0, 0)
self._test_mesos_monitor_pull_data(mock_url_get, {}, 0, 0, 0, 0)
def test_mesos_monitor_get_metric_names(self):
mesos_metric_spec = 'magnum.conductor.mesos_monitor.MesosMonitor.'\
@ -292,3 +300,29 @@ class MonitorsTestCase(base.TestCase):
self.mesos_monitor.data = test_data
mem_util = self.mesos_monitor.compute_memory_util()
self.assertEqual(0, mem_util)
test_data = {
'mem_total': 100,
'mem_used': 0,
'pods': 0,
}
self.mesos_monitor.data = test_data
mem_util = self.mesos_monitor.compute_memory_util()
self.assertEqual(0, mem_util)
def test_mesos_monitor_compute_cpu_util(self):
test_data = {
'cpu_total': 1,
'cpu_used': 0.2,
}
self.mesos_monitor.data = test_data
cpu_util = self.mesos_monitor.compute_cpu_util()
self.assertEqual(20, cpu_util)
test_data = {
'cpu_total': 100,
'cpu_used': 0,
}
self.mesos_monitor.data = test_data
cpu_util = self.mesos_monitor.compute_cpu_util()
self.assertEqual(0, cpu_util)

Loading…
Cancel
Save