Created
March 14, 2022 13:30
-
-
Save alanbchristie/7e2ac86f1d6461b75eb834570a1b201e to your computer and use it in GitHub Desktop.
Keep max CPU and Memory using container stats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Simple utility to watch the stats for a single docker container. | |
# It writes stats to two files 'max-cpu.txt' and 'max-mem.txt' | |
# for the maximum observed CPU and memory. | |
# The utility runs the stats every 60 seconds. | |
# | |
# Stats look like this... | |
# | |
# CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS | |
# 438738fa238c fragmentor_db_1 574.60% 127.2GiB / 243.9GiB 52.16% 47kB / 118kB 177GB / 8.88GB 38 | |
# | |
# Run with: nohup ./max_stats_watcher.py & | |
import subprocess | |
import re | |
import time | |
_STATS_RE = re.compile(r'([0-9a-f]+)\s*(\S+)\s*(?P<cpu_pcent>\S+)%\s*(?P<mem>[\d.]+)(?P<mem_unit>\S+)\s+/\s+(\S+)\s+(\S+)%.*', re.MULTILINE) | |
_MAX_CPU = 2546.01 | |
_MAX_MEM = 163.2 | |
if __name__ == '__main__': | |
while True: | |
process = subprocess.run(['docker', 'stats', '--no-stream'], stdout=subprocess.PIPE) | |
stats = process.stdout.decode('utf-8').replace('\n', '\n') | |
match = _STATS_RE.search(stats) | |
cpu = float(match.group('cpu_pcent')) | |
mem = float(match.group('mem')) | |
mem_unit = match.group('mem_unit') | |
if cpu >= _MAX_CPU: | |
print(f'New max cpu ({cpu}%)') | |
_MAX_CPU = cpu | |
with open('max-cpu.txt', 'w') as cpu_file: | |
cpu_file.write(stats) | |
if mem_unit == 'GiB' and mem >= _MAX_MEM: | |
print(f'New max mem ({mem}GiB)') | |
_MAX_MEM = mem | |
with open('max-mem.txt', 'w') as mem_file: | |
mem_file.write(stats) | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment