-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
25 additions
and
18 deletions.
There are no files selected for viewing
43 changes: 25 additions & 18 deletions
43
src/middlewared/middlewared/plugins/apps/ix_apps/docker/stats.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,31 @@ | ||
from collections import defaultdict | ||
|
||
from .utils import get_docker_client, PROJECT_KEY | ||
|
||
|
||
def list_resources_stats_by_project(project_name: str) -> dict: | ||
def list_resources_stats_by_project(project_name: str | None = None) -> dict: | ||
projects = defaultdict(lambda: { | ||
'cpu_usage': 0, | ||
'memory_stats': 0, | ||
'networks': defaultdict(lambda: {'rx_bytes': 0, 'tx_bytes': 0}), | ||
'blkio_stats': {'read': 0, 'write': 0}, | ||
}) | ||
with get_docker_client() as client: | ||
label_filter = {'label': f'{PROJECT_KEY}={project_name}' if project_name else PROJECT_KEY} | ||
stats = {} | ||
for container in client.containers.list(all=True, filters=label_filter, sparse=False): | ||
cont_stats = container.stats(stream=False, decode=None) | ||
stats[cont_stats['name'].strip('/')] = { | ||
'cpu_usage': cont_stats.get('cpu_stats', {}).get('cpu_usage', {}).get('total_usage', 0), | ||
'memory_stats': cont_stats.get('memory_stats', {}).get('usage', 0), | ||
'networks': { | ||
net_name: { | ||
'rx_bytes': net_values['rx_bytes'], | ||
'tx_bytes': net_values['tx_bytes'] | ||
} for net_name, net_values in cont_stats.get('networks', {}).items() | ||
}, | ||
'blkio_stats': { | ||
blkio['op']: blkio['value'] | ||
for blkio in cont_stats.get('blkio_stats', {}).get('io_service_bytes_recursive', {}) or {} | ||
} | ||
} | ||
return stats | ||
stats = container.stats(stream=False, decode=None, one_shot=True) | ||
project = container.labels.get(PROJECT_KEY) | ||
if not project: | ||
continue | ||
|
||
blkio_container_stats = stats.get('blkio_stats', {}).get('io_service_bytes_recursive') or {} | ||
project_stats = projects[project] | ||
project_stats['cpu_usage'] += stats.get('cpu_stats', {}).get('cpu_usage', {}).get('total_usage', 0) | ||
project_stats['memory_stats'] += stats.get('memory_stats', {}).get('usage', 0) | ||
for entry in filter(lambda x: x['op'] in ('read', 'write'), blkio_container_stats): | ||
project_stats['blkio_stats'][entry['op']] += entry['value'] | ||
for net_name, net_values in stats.get('networks', {}).items(): | ||
project_stats['networks'][net_name]['rx_bytes'] += net_values.get('rx_bytes', 0) | ||
project_stats['networks'][net_name]['tx_bytes'] += net_values.get('tx_bytes', 0) | ||
|
||
return projects |