2
0

container-monitoring 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/bash
  2. stty -echo
  3. INTERVAL=3
  4. trap 'stty echo; exit' INT EXIT
  5. print_header() {
  6. echo "+----------------------------------------------------+------------+-----------------+-----------------+"
  7. printf "| %-50s | %-10s | %-15s | %-15s |\n" "Name" "CPU %" "Memory Usage" "Memory Limit"
  8. echo "+----------------------------------------------------+------------+-----------------+-----------------+"
  9. }
  10. print_container_info() {
  11. local container_info=($1)
  12. local container_name=${container_info[0]}
  13. local container_stats=(${container_info[@]:1})
  14. printf "| %-50s | %-10s | %-15s | %-15s |\n" "$container_name" "${container_stats[0]}" "${container_stats[1]}" "${container_stats[2]}"
  15. }
  16. while true; do
  17. DOCKER_STATS=$(docker stats --no-stream --format "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}")
  18. clear
  19. if [[ ! -z "$DOCKER_STATS" ]]; then
  20. print_header
  21. while IFS= read -r line; do
  22. print_container_info "$(echo "$line" | awk '{gsub(/\//, " "); print}')"
  23. done <<< "$DOCKER_STATS"
  24. echo "+----------------------------------------------------+------------+-----------------+-----------------+"
  25. else
  26. echo "No active containers found"
  27. break
  28. fi
  29. sleep $INTERVAL
  30. done