#!/usr/bin/env bash set -Eeuo pipefail stop_file="${1:?stop file is required}" interval_seconds="${2:-1}" if ! [[ "$interval_seconds" =~ ^[1-9][0-9]*$ ]]; then echo "Sampling interval must be a positive integer." >&2 exit 1 fi printf '%s\n' 'captured_at,load_1m,mem_available_kib,container,cpu_percent,memory_usage,network_io,block_io,pids' while [[ ! -e "$stop_file" ]]; do captured_at="$(date --iso-8601=seconds)" load_1m="$(awk '{print $1}' /proc/loadavg)" mem_available_kib="$(awk '/^MemAvailable:/ {print $2}' /proc/meminfo)" docker stats --no-stream \ --format "${captured_at},${load_1m},${mem_available_kib},{{.Name}},{{.CPUPerc}},{{.MemUsage}},{{.NetIO}},{{.BlockIO}},{{.PIDs}}" \ || true sleep "$interval_seconds" done