|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
3 |
| -IFS=$'\n' read -d '' -r -a container_ids < <(bin/docker-compose ps -q) |
| 3 | +stty -echo |
4 | 4 |
|
5 |
| -if [ ${#container_ids[@]} -eq 0 ]; then |
6 |
| - echo "No active containers found" |
7 |
| - exit 1 |
8 |
| -fi |
| 5 | +INTERVAL=3 |
| 6 | + |
| 7 | +trap 'stty echo; exit' INT EXIT |
| 8 | + |
| 9 | +print_header() { |
| 10 | + echo "+----------------------------------------------------+--------------+----------+----------+------------------------+" |
| 11 | + printf "| %-50s | %-12s | %-8s | %-8s | %-22s |\n" "NAME" "CONTAINER ID" "CPU %" "MEM %" "MEM USAGE / MEM LIMIT" |
| 12 | + echo "+----------------------------------------------------+--------------+----------+----------+------------------------+" |
| 13 | +} |
| 14 | + |
| 15 | +print_container_info() { |
| 16 | + local container_info |
| 17 | + local container_name |
| 18 | + local container_id |
| 19 | + local cpu_perc |
| 20 | + local mem_perc |
| 21 | + local mem_usage |
| 22 | + |
| 23 | + container_info="$1" |
| 24 | + container_name=$(echo "$container_info" | cut -f1) |
| 25 | + container_id=$(echo "$container_info" | cut -f2) |
| 26 | + cpu_perc=$(echo "$container_info" | cut -f3) |
| 27 | + mem_perc=$(echo "$container_info" | cut -f4) |
| 28 | + mem_usage=$(echo "$container_info" | cut -f5) |
| 29 | + |
| 30 | + |
| 31 | +print_container_info() { |
| 32 | + local container_info |
| 33 | + local container_name |
| 34 | + local container_id |
| 35 | + local cpu_perc |
| 36 | + local mem_perc |
| 37 | + local mem_usage |
| 38 | + |
| 39 | + container_info="$1" |
| 40 | + container_name=$(echo "$container_info" | cut -f1) |
| 41 | + container_id=$(echo "$container_info" | cut -f2) |
| 42 | + cpu_perc=$(echo "$container_info" | cut -f3) |
| 43 | + mem_perc=$(echo "$container_info" | cut -f4) |
| 44 | + mem_usage=$(echo "$container_info" | cut -f5) |
| 45 | + |
| 46 | + printf "| %-50s | %-12s | %-8s | %-8s | %-22s |\n" "$container_name" "$container_id" "$cpu_perc" "$mem_perc" "$mem_usage" |
| 47 | +} |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +while true; do |
| 52 | + DOCKER_STATS=$(docker stats --no-stream --format "{{.Name}}\t{{.ID}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.MemUsage}}") |
| 53 | + |
| 54 | + clear |
| 55 | + |
| 56 | + if [[ -n "$DOCKER_STATS" ]]; then |
| 57 | + print_header |
| 58 | + |
| 59 | + while IFS= read -r line; do |
| 60 | + print_container_info "$(echo "$line" | awk '{gsub(/\//, " "); print}')" |
| 61 | + done <<< "$DOCKER_STATS" |
| 62 | + |
| 63 | + echo "+----------------------------------------------------+--------------+----------+----------+------------------------+" |
| 64 | + else |
| 65 | + echo "No active containers found" |
| 66 | + break |
| 67 | + fi |
| 68 | + |
| 69 | + sleep $INTERVAL |
| 70 | +done |
9 | 71 |
|
10 |
| -docker stats "${container_ids[@]}" |
|
0 commit comments