Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Virsh get total cpu allocations

tl;dr

virsh list | awk '{print $1}' | grep -oIE "[0-9]*" | while read word; do virsh dominfo ${word} | grep "CPU.s"; done | awk 'BEGIN {a=0;} {a=a+$2;} END {print a;}'

The explanation

If you want to get the total allocation of vCPUs to all the guests on a kvm host, you can use this one-liner. virsh list gets the list of running domains (virtual machines). The awk and grep get only the domain id numbers (could do it by domain name if you wish). virsh dominfo gets the cpu allocation for each listed domain, by iterating over the list. The final awk statements counts the numbers.

Get total physical CPUs available

virsh nodeinfo


CPU model:           x86_64
CPU(s):              24
CPU frequency:       1899 MHz
CPU socket(s):       1
Core(s) per socket:  6
Thread(s) per core:  2
NUMA cell(s):        2
Memory size:         198310648 KiB

Comments