Use DF for disk sizes

This commit is contained in:
Simon Gruber
2025-12-14 20:10:07 +01:00
parent 5afbf4e428
commit ed432545ad
+17 -12
View File
@@ -241,24 +241,29 @@ 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 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 return size_rootfs if size_rootfs > 0 else size_rw
# Container not found in df data
return 0
except Exception as e: except Exception as e:
print(f"Warning: Error getting disk usage: {e}") print(f"Warning: Error getting disk usage: {e}")
return None return None