From f7db6040b135853005fb73395fb542e9f1786148 Mon Sep 17 00:00:00 2001 From: evgeniy Date: Thu, 7 Dec 2023 02:26:11 +0200 Subject: [PATCH 1/6] Add container monitoring script --- compose/bin/container-monitoring | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 compose/bin/container-monitoring diff --git a/compose/bin/container-monitoring b/compose/bin/container-monitoring new file mode 100755 index 000000000..3b93d2f22 --- /dev/null +++ b/compose/bin/container-monitoring @@ -0,0 +1,37 @@ +#!/bin/bash + +stty -echo + +INTERVAL=3 + +trap 'stty echo; exit' INT EXIT + +while true; do + DOCKER_STATS=$(docker stats --no-stream --format "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}") + + clear + + if [[ ! -z "$DOCKER_STATS" ]]; then + echo "+----------------------------------------------------+------------+-----------------+-----------------+" + printf "| %-50s | %-10s | %-15s | %-15s |\n" "Name" "CPU %" "Memory Usage" "Memory Limit" + echo "+----------------------------------------------------+------------+-----------------+-----------------+" + + while IFS= read -r line; do + line=$(echo "$line" | tr '/' ' ') + + container_info=($line) + container_name=${container_info[0]} + container_stats=(${container_info[@]:1}) + + printf "| %-50s | %-10s | %-15s | %-15s |\n" "$container_name" "${container_stats[0]}" "${container_stats[1]}" "${container_stats[2]}" + done <<< "$DOCKER_STATS" + + echo "+----------------------------------------------------+------------+-----------------+-----------------+" + else + echo "No active containers found" + break + fi + + sleep $INTERVAL +done + From 2dfb8a740f5b9d32ebc1c54219b1ef82ef124fd8 Mon Sep 17 00:00:00 2001 From: evgeniy Date: Thu, 7 Dec 2023 02:42:06 +0200 Subject: [PATCH 2/6] Improve readability and code structure --- compose/bin/container-monitoring | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/compose/bin/container-monitoring b/compose/bin/container-monitoring index 3b93d2f22..cbcdd2b2b 100755 --- a/compose/bin/container-monitoring +++ b/compose/bin/container-monitoring @@ -6,24 +6,30 @@ INTERVAL=3 trap 'stty echo; exit' INT EXIT +print_header() { + echo "+----------------------------------------------------+------------+-----------------+-----------------+" + printf "| %-50s | %-10s | %-15s | %-15s |\n" "Name" "CPU %" "Memory Usage" "Memory Limit" + echo "+----------------------------------------------------+------------+-----------------+-----------------+" +} + +print_container_info() { + local container_info=($1) + local container_name=${container_info[0]} + local container_stats=(${container_info[@]:1}) + + printf "| %-50s | %-10s | %-15s | %-15s |\n" "$container_name" "${container_stats[0]}" "${container_stats[1]}" "${container_stats[2]}" +} + while true; do DOCKER_STATS=$(docker stats --no-stream --format "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}") clear if [[ ! -z "$DOCKER_STATS" ]]; then - echo "+----------------------------------------------------+------------+-----------------+-----------------+" - printf "| %-50s | %-10s | %-15s | %-15s |\n" "Name" "CPU %" "Memory Usage" "Memory Limit" - echo "+----------------------------------------------------+------------+-----------------+-----------------+" + print_header while IFS= read -r line; do - line=$(echo "$line" | tr '/' ' ') - - container_info=($line) - container_name=${container_info[0]} - container_stats=(${container_info[@]:1}) - - printf "| %-50s | %-10s | %-15s | %-15s |\n" "$container_name" "${container_stats[0]}" "${container_stats[1]}" "${container_stats[2]}" + print_container_info "$(echo "$line" | awk '{gsub(/\//, " "); print}')" done <<< "$DOCKER_STATS" echo "+----------------------------------------------------+------------+-----------------+-----------------+" @@ -34,4 +40,3 @@ while true; do sleep $INTERVAL done - From b7418753bebbe7aeaf70b35bffc025480733b435 Mon Sep 17 00:00:00 2001 From: evgeniy Date: Thu, 7 Dec 2023 13:12:28 +0200 Subject: [PATCH 3/6] Simplify script --- compose/bin/container-monitoring | 46 +++++--------------------------- 1 file changed, 7 insertions(+), 39 deletions(-) diff --git a/compose/bin/container-monitoring b/compose/bin/container-monitoring index cbcdd2b2b..a74a62afe 100755 --- a/compose/bin/container-monitoring +++ b/compose/bin/container-monitoring @@ -1,42 +1,10 @@ -#!/bin/bash +#!/usr/bin/env bash -stty -echo +container_ids=$(bin/docker-compose ps -q) -INTERVAL=3 +if [ -z "$container_ids" ]; then + echo "No active containers found" + exit 1 +fi -trap 'stty echo; exit' INT EXIT - -print_header() { - echo "+----------------------------------------------------+------------+-----------------+-----------------+" - printf "| %-50s | %-10s | %-15s | %-15s |\n" "Name" "CPU %" "Memory Usage" "Memory Limit" - echo "+----------------------------------------------------+------------+-----------------+-----------------+" -} - -print_container_info() { - local container_info=($1) - local container_name=${container_info[0]} - local container_stats=(${container_info[@]:1}) - - printf "| %-50s | %-10s | %-15s | %-15s |\n" "$container_name" "${container_stats[0]}" "${container_stats[1]}" "${container_stats[2]}" -} - -while true; do - DOCKER_STATS=$(docker stats --no-stream --format "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}") - - clear - - if [[ ! -z "$DOCKER_STATS" ]]; then - print_header - - while IFS= read -r line; do - print_container_info "$(echo "$line" | awk '{gsub(/\//, " "); print}')" - done <<< "$DOCKER_STATS" - - echo "+----------------------------------------------------+------------+-----------------+-----------------+" - else - echo "No active containers found" - break - fi - - sleep $INTERVAL -done +docker stats $container_ids From 59a8d33e9011085ec87ca7c420eff25d74bf85b9 Mon Sep 17 00:00:00 2001 From: Mark Shust Date: Sat, 24 Feb 2024 14:26:16 -0500 Subject: [PATCH 4/6] Renamed script to bin/docker-stats, added README --- README.md | 1 + compose/bin/{container-monitoring => docker-stats} | 0 2 files changed, 1 insertion(+) rename compose/bin/{container-monitoring => docker-stats} (100%) diff --git a/README.md b/README.md index 485884b80..13d38677f 100644 --- a/README.md +++ b/README.md @@ -282,6 +282,7 @@ It is recommended to keep your root docker config files in one repository, and y - `bin/download`: Download specific Magento version from Composer to the container, with optional arguments of the version (2.4.6-p3 [default]) and type ("community" [default], "enterprise", or "mageos"). Ex. `bin/download 2.4.6-p3 enterprise` - `bin/debug-cli`: Enable Xdebug for bin/magento, with an optional argument of the IDE key. Defaults to PHPSTORM Ex. `bin/debug-cli enable PHPSTORM` - `bin/deploy`: Runs the standard Magento deployment process commands. Pass extra locales besides `en_US` via an optional argument. Ex. `bin/deploy nl_NL` +- `bin/docker-stats`: Display status for CPU, memory usage, and memory limit of currently-running Docker containers. - `bin/fixowns`: This will fix filesystem ownerships within the container. - `bin/fixperms`: This will fix filesystem permissions within the container. - `bin/grunt`: Run the grunt binary. Ex. `bin/grunt exec` diff --git a/compose/bin/container-monitoring b/compose/bin/docker-stats similarity index 100% rename from compose/bin/container-monitoring rename to compose/bin/docker-stats From 39eae489328c2c181e68c51142a77bf5f936bb05 Mon Sep 17 00:00:00 2001 From: Mark Shust Date: Sat, 24 Feb 2024 14:27:38 -0500 Subject: [PATCH 5/6] Fix failing shellcheck SC2086 --- compose/bin/docker-stats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compose/bin/docker-stats b/compose/bin/docker-stats index a74a62afe..83cac7a78 100755 --- a/compose/bin/docker-stats +++ b/compose/bin/docker-stats @@ -7,4 +7,4 @@ if [ -z "$container_ids" ]; then exit 1 fi -docker stats $container_ids +docker stats "$container_ids" From 82ffbaf7475feaae58c50b9c67349f3f0352ac2a Mon Sep 17 00:00:00 2001 From: Mark Shust Date: Sat, 24 Feb 2024 14:31:32 -0500 Subject: [PATCH 6/6] Added documentation to make command --- compose/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/compose/Makefile b/compose/Makefile index c02b05f22..a564af9cb 100644 --- a/compose/Makefile +++ b/compose/Makefile @@ -31,6 +31,7 @@ help: @echo "$(call format,dev-urn-catalog-generate,'Generate URNs for PHPStorm and remap paths to local host.')" @echo "$(call format,devconsole,'Alias for n98-magerun2 dev:console.')" @echo "$(call format,devtools-cli-check,'Check & install the CLI devtools if missing from system.')" + @echo "$(call format,docker-stats,'Display status for CPU, memory usage, and memory limit of currently-running Docker containers.')" @echo "$(call format,download,'Download & extract specific Magento version to the src directory.')" @echo "$(call format,fixowns,'This will fix filesystem ownerships within the container.')" @echo "$(call format,fixperms,'This will fix filesystem permissions within the container.')"