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)
Last active
August 22, 2024 14:05
-
-
Save 0x3333/1d12b70034f97435bcc8ee41971a79fc to your computer and use it in GitHub Desktop.
Show Memory and CPU usage of kvm vms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 thanks for reporting. Updated the script to reflect it. Changed the result to MiB.
Very useful, thanks
Super helpful, thank you.
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
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
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.
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
virsh dominfo xxx
output mem in KiB now