Use DF for disk sizes
This commit is contained in:
@@ -241,23 +241,28 @@ class ContainerCollector(BaseCollector):
|
||||
|
||||
def _get_container_disk_usage(self, container) -> int:
|
||||
"""
|
||||
Get the disk usage for a container in bytes.
|
||||
This includes the writable layer size (SizeRw) and root filesystem size (SizeRootFs).
|
||||
Get the disk usage for a container in bytes using the Docker system df API.
|
||||
This provides accurate size information including writable layer and virtual size.
|
||||
"""
|
||||
try:
|
||||
# Reload container to get latest attributes with size information
|
||||
container.reload()
|
||||
attrs = container.attrs
|
||||
# Use the system df API to get accurate container size information
|
||||
# This is more reliable than container.attrs which often doesn't include size data
|
||||
df_data = self.client.df()
|
||||
containers_info = df_data.get('Containers', [])
|
||||
|
||||
# SizeRw: Size of files that have been created or changed
|
||||
size_rw = attrs.get('SizeRw', 0)
|
||||
# Find the matching container by ID
|
||||
for container_info in containers_info:
|
||||
if container_info.get('Id', '').startswith(container.id):
|
||||
# SizeRw: Size of files created or changed in the writable layer
|
||||
size_rw = container_info.get('SizeRw', 0)
|
||||
# SizeRootFs: Total size including all layers (image + writable)
|
||||
size_rootfs = container_info.get('SizeRootFs', 0)
|
||||
|
||||
# Return SizeRootFs (total size) if available, otherwise SizeRw
|
||||
return size_rootfs if size_rootfs > 0 else size_rw
|
||||
|
||||
# SizeRootFs: Total size of container filesystem (read-only + writable layers)
|
||||
size_rootfs = attrs.get('SizeRootFs', 0)
|
||||
|
||||
# Return the total size (SizeRootFs includes SizeRw)
|
||||
# If SizeRootFs is not available, fall back to SizeRw
|
||||
return size_rootfs if size_rootfs > 0 else size_rw
|
||||
# Container not found in df data
|
||||
return 0
|
||||
|
||||
except Exception as e:
|
||||
print(f"Warning: Error getting disk usage: {e}")
|
||||
|
||||
Reference in New Issue
Block a user