Commit 8346e23b authored by Daniele Venzano's avatar Daniele Venzano

Calculate container count only from running containers

parent e73f2d81
......@@ -243,33 +243,28 @@ class DockerClient:
if not res:
break
def list(self, only_label=None) -> List[dict]:
def list(self, only_label=None, status=None) -> List[dict]:
"""
List running or defined containers.
:param only_label: filter containers with only a certain label
:param status: filter containers with only a certain status (one of restarting, running, paused, exited)
:return: a list of containers
"""
filters = {}
if only_label is not None:
filters['label'] = only_label
if status is not None:
filters['status'] = status
try:
ret = self.cli.containers.list(all=True)
ret = self.cli.containers.list(all=True, filters=filters)
except docker.errors.APIError as ex:
raise ZoeException(str(ex))
except requests.exceptions.RequestException as ex:
raise ZoeException(str(ex))
if only_label is None:
only_label = {}
conts = []
for cont_info in ret:
match = True
for key, value in only_label.items():
if key not in cont_info.attrs['Config']['Labels']:
match = False
break
if cont_info.attrs['Config']['Labels'][key] != value:
match = False
break
if match:
conts.append(self._container_summary(cont_info))
conts.append(self._container_summary(cont_info))
return conts
......
......@@ -104,71 +104,6 @@ class DockerEngineBackend(zoe_master.backends.base.BaseBackend):
platform_stats.timestamp = time.time()
return platform_stats
def _update_node_state(self, host_conf: DockerHostConfig, node_stats: NodeStats, get_usage_stats: bool):
node_stats.labels = host_conf.labels
try:
my_engine = DockerClient(host_conf)
except ZoeException as e:
log.error(str(e))
node_stats.status = 'offline'
log.info('Node {} is offline'.format(host_conf.name))
return
else:
node_stats.status = 'online'
try:
container_list = my_engine.list(only_label={'zoe_deployment_name': get_conf().deployment_name})
info = my_engine.info()
except ZoeException:
return
node_stats.container_count = len(container_list)
node_stats.cores_total = info['NCPU']
node_stats.memory_total = info['MemTotal']
if info['Labels'] is not None:
node_stats.labels += set(info['Labels'])
node_stats.memory_reserved = sum([cont['memory_hard_limit'] for cont in container_list if cont['memory_hard_limit'] != node_stats.memory_total])
node_stats.cores_reserved = sum([cont['cpu_quota'] / cont['cpu_period'] for cont in container_list if cont['cpu_period'] != 0])
stats = {}
for cont in container_list:
stats[cont['id']] = {}
stats[cont['id']]['core_limit'] = cont['cpu_quota'] / cont['cpu_period']
stats[cont['id']]['mem_limit'] = cont['memory_hard_limit']
node_stats.service_stats = stats
if get_usage_stats:
if get_conf().kairosdb_enable:
kdb = KairosDBInMetrics()
for cont in container_list:
stats[cont['id']].update(kdb.get_service_usage(cont['name']))
node_stats.memory_in_use = sum([stat['mem_usage'] for stat in stats.values()])
node_stats.cores_in_use = sum([stat['cpu_usage'] for stat in stats.values()])
else:
for cont in container_list:
try:
aux = my_engine.stats(cont['id'], stream=False) # this call is very slow (>~1sec)
if 'usage' in aux['memory_stats']:
stats[cont['id']]['mem_usage'] = aux['memory_stats']['usage']
else:
stats[cont['id']]['mem_usage'] = 0
stats[cont['id']]['cpu_usage'] = self._get_core_usage(aux)
except ZoeException:
continue
node_stats.memory_in_use = sum([stat['mem_usage'] for stat in stats.values()])
node_stats.cores_in_use = sum([stat['cpu_usage'] for stat in stats.values()])
else:
node_stats.memory_in_use = 0
node_stats.cores_in_use = 0
def _get_core_usage(self, stat):
cpu_time_now = stat['cpu_stats']['cpu_usage']['total_usage']
cpu_time_pre = stat['precpu_stats']['cpu_usage']['total_usage']
return (cpu_time_now - cpu_time_pre) / 1000000000
def node_list(self):
"""Return a list of node names."""
return [node.name for node in self.docker_config]
......
......@@ -60,6 +60,7 @@ class DockerStateSynchronizer(threading.Thread):
try:
my_engine = DockerClient(host_config)
container_list = my_engine.list(only_label={'zoe_deployment_name': get_conf().deployment_name})
running_container_list = my_engine.list(status='running')
info = my_engine.info()
except ZoeException as e:
self.host_stats[host_config.name].status = 'offline'
......@@ -70,7 +71,7 @@ class DockerStateSynchronizer(threading.Thread):
log.info('Node {} is now online'.format(host_config.name))
self.host_stats[host_config.name].status = 'online'
self.host_stats[host_config.name].container_count = info['Containers']
self.host_stats[host_config.name].container_count = len(running_container_list)
self.host_stats[host_config.name].cores_total = info['NCPU']
self.host_stats[host_config.name].memory_total = info['MemTotal']
self.host_stats[host_config.name].labels = host_config.labels
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment