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