Scripts for managing KVM hypervisors used with DRBD and LVM.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kvmstats 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/sh
  2. set -e
  3. usage () {
  4. echo 'usage: kvmstats [-a] [-u k|m|g]' >&2
  5. exit 1
  6. }
  7. for DEP in bc virsh
  8. do
  9. if [ -z "$(which $DEP)" ]
  10. then
  11. echo "kvmstats: $DEP not found in \$PATH" >&2
  12. exit 1
  13. fi
  14. done
  15. POW=$(echo 1024 ^ 3 | bc)
  16. while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]'
  17. do
  18. case $1 in
  19. '-u')
  20. case $2 in
  21. 'k')
  22. POW=$(echo 1024 ^ 1 | bc)
  23. ;;
  24. 'm')
  25. POW=$(echo 1024 ^ 2 | bc)
  26. ;;
  27. 'g')
  28. POW=$(echo 1024 ^ 3 | bc)
  29. ;;
  30. *)
  31. usage
  32. esac
  33. ;;
  34. '-a')
  35. SHOW_AVAIL=y
  36. ;;
  37. *)
  38. usage
  39. esac
  40. shift
  41. done
  42. for VM in $(virsh list --name --all)
  43. do
  44. CPU=$(virsh vcpucount --maximum --current "$VM")
  45. # libvirt store memoy in KiB, POW must be lowered by 1
  46. RAM=$(virsh domstats --balloon "$VM" | awk 'BEGIN { FS="=" } /balloon.maximum/ { print $2 / '$((POW / 1024))'}')
  47. DSK=$(for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | cut -d\ -f1)
  48. do
  49. virsh domblkinfo "$VM" "$BLK"
  50. done | awk '/Physical:/ { size += $2 } END { print int(size / '${POW}') }')
  51. RUN=$(virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no)
  52. echo "$VM" "$CPU" "$RAM" "$DSK" "$RUN"
  53. done | (
  54. echo vm vcpu ram disk running
  55. awk '{ print } /yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }'
  56. if [ "$SHOW_AVAIL" ]
  57. then
  58. AV_CPU=$(nproc)
  59. AV_MEM=$(awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo)
  60. echo AVAILABLE "$AV_CPU" "$AV_MEM"
  61. fi
  62. ) | column -t