Added container disk usage
This commit is contained in:
@@ -59,6 +59,18 @@ class ContainerCollector(BaseCollector):
|
||||
'timestamp': timestamp
|
||||
})
|
||||
|
||||
# Disk usage metrics (available for all containers)
|
||||
try:
|
||||
disk_usage = self._get_container_disk_usage(container)
|
||||
if disk_usage is not None:
|
||||
metrics.append({
|
||||
'name': f'containers.{container_name}.disk_usage_bytes',
|
||||
'value': disk_usage,
|
||||
'timestamp': timestamp
|
||||
})
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not collect disk usage for {container_name}: {e}")
|
||||
|
||||
# Only collect resource metrics for running containers
|
||||
if state == 'running':
|
||||
try:
|
||||
@@ -226,3 +238,27 @@ class ContainerCollector(BaseCollector):
|
||||
print(f"Warning: Error getting I/O metrics: {e}")
|
||||
|
||||
return metrics
|
||||
|
||||
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).
|
||||
"""
|
||||
try:
|
||||
# Reload container to get latest attributes with size information
|
||||
container.reload()
|
||||
attrs = container.attrs
|
||||
|
||||
# SizeRw: Size of files that have been created or changed
|
||||
size_rw = attrs.get('SizeRw', 0)
|
||||
|
||||
# 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
|
||||
|
||||
except Exception as e:
|
||||
print(f"Warning: Error getting disk usage: {e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user