1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import subprocess
def run(command, label, timeout=180):
result = subprocess.run(command, capture_output=True, text=True, timeout=timeout)
if result.returncode:
raise RuntimeError(f'{label}: exit {result.returncode}\n{result.stderr[-1200:]}')
return result.stdout
def snapshot(compose, services):
result = {}
for name in services:
container = run([*compose, 'ps', '-q', name], 'container lookup').strip()
result[name] = {
path: run(['docker', 'exec', container, 'cat', path], 'resource snapshot')
for path in ['/sys/fs/cgroup/cpu.stat', '/sys/fs/cgroup/memory.peak', '/sys/fs/cgroup/io.stat']
}
return result