container-monitoring 1.2 KB

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