Skip to content

Instantly share code, notes, and snippets.

@0x3333
Last active April 9, 2024 13:38
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x3333/1d12b70034f97435bcc8ee41971a79fc to your computer and use it in GitHub Desktop.
Save 0x3333/1d12b70034f97435bcc8ee41971a79fc to your computer and use it in GitHub Desktop.
Show Memory and CPU usage of kvm vms
RAM
---
vm-1                      = 1,024 MiB
vm-2                      = 2,048 MiB

Total: 3,072 MiB

CPU(s)
------
vm-1                      = 4 cpu(s)
vm-2                      = 5 cpu(s)

Total: 9 CPU(s)
#!/bin/bash
#
# RAM
#
echo "RAM"
echo "---"
SUM=0
while read vm
do
if [ ! -z "$vm" ]; then
USED=$(($(virsh dominfo $vm | grep "Max memory" | cut -f 7 -d " ") / 1024))
printf "%-25s = %'.0f MiB\n" $vm $USED
SUM=$((SUM + USED))
fi
done < <(virsh list --all --name)
printf "\nTotal: %'.0f MiB\n\n" $SUM
#
# CPUs
#
echo "CPU(s)"
echo "------"
SUM=0
while read vm
do
if [ ! -z "$vm" ]; then
USED=$(virsh dominfo $vm | grep "CPU(s)" | cut -f 10 -d " ")
printf "%-25s = %'.0f cpu(s)\n" $vm $USED
SUM=$((SUM + USED))
fi
done < <(virsh list --all --name)
printf "\nTotal: %'.0f CPU(s)\n" $SUM
@fanlix
Copy link

fanlix commented Sep 3, 2021

virsh dominfo xxx output mem in KiB now

Max memory:     1048576 KiB
Used memory:    1048576 KiB

@0x3333
Copy link
Author

0x3333 commented Sep 3, 2021

@fanlix thanks for reporting. Updated the script to reflect it. Changed the result to MiB.

@kitsuo
Copy link

kitsuo commented Oct 21, 2022

Very useful, thanks

@pkbarbiedoll
Copy link

Super helpful, thank you.

@zelogik
Copy link

zelogik commented May 13, 2023

Hi,

My updated script if you want :D

#!/bin/bash

printf '%-20s %-4s %-8s\n' "Vm Name" "CPU(s)" "RAM (MiB)"

mapfile -t vm_array < <( virsh list --all --name )

while read -r vm
do
        if [ ! -z "$vm" ]; then
                MEM_USED=$(($(virsh dominfo $vm | grep "Max memory" | cut -f 7 -d " ") / 1024))
                CPU_USED=$(virsh dominfo $vm | grep "CPU(s)" | cut -f 10 -d " ")
                printf "%-20s %4s | %8s\n" "$vm" "$CPU_USED"  "$MEM_USED"
                MEM_SUM=$((MEM_SUM + MEM_USED))
                CPU_SUM=$((CPU_SUM + CPU_USED))
        fi
done < <( printf '%s\n' "${vm_array[@]}")

printf -- '-%.0s' {1..36}
printf '\n%-20s %4s | %8s\n' "Totals:" "$CPU_SUM" "$MEM_SUM"

exit 0

Output:

Vm Name              CPU(s) RAM (MiB)
xxxxxx                 12 |   110592
xxxxxxxxxxx             2 |     2048
xxxxxxxxx               2 |     2048
xxxxxxxx                2 |     3072
xxxxxxxxxxx             1 |     1536
xxxxxxxxxxx             4 |    12000
xxxxx                   2 |      512
------------------------------------
Totals:                25 |   131808

@reukiodo
Copy link

reukiodo commented Mar 8, 2024

Thanks for your script! I expanded upon it to show storage also:

#!/bin/bash

printf '%-20s %3s %4s %7s\n' "VM Name" "CPU" "RAM" "Storage"

mapfile -t vm_array < <( virsh list --all --name )
while read -r vm
do
  if [ ! -z "$vm" ]; then
    CPU=$(virsh dominfo $vm | grep "CPU" | cut -f 10 -d " ")
    MEM=$(($(virsh dominfo $vm | grep "Max memory" | cut -f 7 -d " ") / 1024 / 1024))
    DSK=0
    for disk in $(virsh domblkinfo $vm --all | grep "d" | cut -d " " -f 9); do
      DSK=$((DSK + disk / 1024 / 1024 / 1024))
    done
    printf "%-20s %3s %4s %7s\n" "$vm" "$CPU"  "$MEM"  "$DSK"
    CPU_SUM=$((CPU_SUM + CPU))
    MEM_SUM=$((MEM_SUM + MEM))
    DSK_SUM=$((DSK_SUM + DSK))
  fi
done < <( printf '%s\n' "${vm_array[@]}")

printf -- '-%.0s' {1..37}
printf '\n%-20s %3s %4s %7s\n' "Totals:" "$CPU_SUM" "$MEM_SUM" "$DSK_SUM"

example output:

VM Name              CPU  RAM Storage
db32                   4    8     510
gpu2                  92 1280   23602
rc18                   6   10      72
rc19                   6    8     670
app18                  2    2      20
app20                  2   20    1312
app24                 10  100       0
app28                 10   30     121
app30                  2    2      20
app9                   2    4      30
certs                  2    2      10
certs4it               2    2      22
con13                  8   10     122
con17                  2    5      22
db14                   4    4      20
db18                   4    8      20
db35                   2    4     121
db42                  10   30     521
db45                   2    2      52
db8                    2   10     910
ks                     2    2     110
ks4it                  2    2      20
logstash               4    8     112
hotpottest2            2    2       0
hotpottest4            2    2       0
hotpottest6            2    2       0
nfs3                   2    2      21
rit4                  32  200    2210
stf05                  4    4      32
stor6                  2    2      21
web35                  4    2     110
web38                  2    4      30
web51                  1    1       0
web56                  2    2      20
web57                  2    4      10
web65                  2    2      20
web72                  4   10     132
web73                  4    6      20
web76                  2    2      20
web77                  2    2      20
web78                  2    2      20
web97                  2    2      22
web98                  2    6      20
-------------------------------------
Totals:              259 1812   31147

@0x3333
Copy link
Author

0x3333 commented Mar 11, 2024

Thanks for your script! I expanded upon it to show storage also:

#!/bin/bash

printf '%-20s %3s %4s %7s\n' "VM Name" "CPU" "RAM" "Storage"

mapfile -t vm_array < <( virsh list --all --name )
while read -r vm
do
  if [ ! -z "$vm" ]; then
    CPU=$(virsh dominfo $vm | grep "CPU" | cut -f 10 -d " ")
    MEM=$(($(virsh dominfo $vm | grep "Max memory" | cut -f 7 -d " ") / 1024 / 1024))
    DSK=0
    for disk in $(virsh domblkinfo $vm --all | grep "d" | cut -d " " -f 9); do
      DSK=$((DSK + disk / 1024 / 1024 / 1024))
    done
    printf "%-20s %3s %4s %7s\n" "$vm" "$CPU"  "$MEM"  "$DSK"
    CPU_SUM=$((CPU_SUM + CPU))
    MEM_SUM=$((MEM_SUM + MEM))
    DSK_SUM=$((DSK_SUM + DSK))
  fi
done < <( printf '%s\n' "${vm_array[@]}")

printf -- '-%.0s' {1..37}
printf '\n%-20s %3s %4s %7s\n' "Totals:" "$CPU_SUM" "$MEM_SUM" "$DSK_SUM"

example output:

VM Name              CPU  RAM Storage
db32                   4    8     510
gpu2                  92 1280   23602
rc18                   6   10      72
rc19                   6    8     670
app18                  2    2      20
app20                  2   20    1312
app24                 10  100       0
app28                 10   30     121
app30                  2    2      20
app9                   2    4      30
certs                  2    2      10
certs4it               2    2      22
con13                  8   10     122
con17                  2    5      22
db14                   4    4      20
db18                   4    8      20
db35                   2    4     121
db42                  10   30     521
db45                   2    2      52
db8                    2   10     910
ks                     2    2     110
ks4it                  2    2      20
logstash               4    8     112
hotpottest2            2    2       0
hotpottest4            2    2       0
hotpottest6            2    2       0
nfs3                   2    2      21
rit4                  32  200    2210
stf05                  4    4      32
stor6                  2    2      21
web35                  4    2     110
web38                  2    4      30
web51                  1    1       0
web56                  2    2      20
web57                  2    4      10
web65                  2    2      20
web72                  4   10     132
web73                  4    6      20
web76                  2    2      20
web77                  2    2      20
web78                  2    2      20
web97                  2    2      22
web98                  2    6      20
-------------------------------------
Totals:              259 1812   31147

Cool! But it didn't work for me.

This is an example of my domblkinfo output:

 Target   Capacity        Allocation      Physical
---------------------------------------------------------
 vda      1099511627776   1018884301312   1100115869696
 hdb      534818816       246178304       534818816

Don't worry about updating yours, this is just informational. Changing the columns in cut(To 14) work as expected...

Debian 12 here.

@srekkas
Copy link

srekkas commented Apr 9, 2024

Replaced cut with awk and added allocated storage. Plus count storage if multiple disks.

#!/bin/bash

printf '%-20s %3s %4s %7s %10s\n' "VM Name" "CPU" "RAM" "Storage" "Storage Allocated"

mapfile -t vm_array < <( virsh list --all --name )

while read -r vm
do
  if [ ! -z "$vm" ]; then
    CPU=$(virsh dominfo $vm | grep "CPU" | cut -f 10 -d " ")
    MEM=$(($(virsh dominfo $vm | grep "Max memory" | cut -f 7 -d " ") / 1024 / 1024))
    DSK=0
    DAL=0
    for disk in $(virsh domblkinfo $vm --all | grep "vd" | awk  '{s+=$2} END {print s}'); do
      DSK=$((DSK + disk / 1024 / 1024 / 1024))
    done
    for disk2 in $(virsh domblkinfo $vm --all | grep "vd" | awk  '{s+=$3} END {print s}'); do
      DAL=$((DAL + disk2 / 1024 / 1024 / 1024))
    done
    printf "%-20s %3s %4s %7s %10s\n" "$vm" "$CPU"  "$MEM"  "$DSK" "$DAL"
    CPU_SUM=$((CPU_SUM + CPU))
    MEM_SUM=$((MEM_SUM + MEM))
    DSK_SUM=$((DSK_SUM + DSK))
    DAL_SUM=$((DAL_SUM + DAL))
  fi
done < <( printf '%s\n' "${vm_array[@]}")

printf -- '-%.0s' {1..37}
printf '\n%-20s %3s %4s %7s %10s\n' "Totals:" "$CPU_SUM" "$MEM_SUM" "$DSK_SUM" "$DAL_SUM"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment