docker-stats 1.3 KB

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