kvmstats: Utiliser domstats pour récupérer infos
All checks were successful
gitea/ansible-roles/pipeline/head This commit looks good

Remplacer les multiples commandes virsh par une seule commande
virsh domstats. La sortie est filtrée par une commande awk.

Certains hyperviseurs ne savent pas lister les informations d’un
volume RBD (Ceph) avec domblkinfo. Il semble que domstats
fonctionne mieux pour ça et peut donner toutes les informations
de toute façon.
This commit is contained in:
Alexis Ben Miloud--Josselin 2023-03-10 10:07:00 +01:00
parent 8b26f2f491
commit 058753bcfe

View file

@ -42,25 +42,34 @@ error () {
main() { main() {
for VM in $(virsh list --name --all | sed '/^$/d' | sort) for VM in $(virsh list --name --all | sed '/^$/d' | sort)
do do
echo "$VM" printf '%s ' "${VM}"
virsh domstats "${VM}" | awk '
# cpu BEGIN {
virsh vcpucount --current "$VM" FS = "="
}
# mem /vcpu\.current/ {
# libvirt stores memory in KiB, POW must be lowered by 1 vcpu = $2
virsh dommemstat "$VM" 2>/dev/null | awk 'BEGIN{ret=1}$1~/^actual$/{print $2 / '$((POW / 1024))';ret=0}END{exit ret}' || }
virsh dumpxml "$VM" | awk -F'[<>]' '$2~/^memory unit/{print $3/'$((POW / 1024))'}' /balloon\.current/ {
mem = $2
# disk }
for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | awk '{print $1}') /balloon\.maximum/ {
do if (!mem)
virsh domblkinfo "$VM" "$BLK" 2>/dev/null mem = $2
done | awk '/Physical:/ { size += $2 } END { print int(size / '${POW}') }' }
/block\.[0-9]+\.physical/ {
# state disksize += $2
virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no }
done | xargs -n5 | { /state\.state/ {
if ($2 == 1)
running = "yes"
else
running = "no"
}
END {
print vcpu, mem / 1024 ^ 2, disksize / 1024 ^ 3, running
}'
done | {
echo vm vcpu ram disk running echo vm vcpu ram disk running
awk '{ print } /yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }' awk '{ print } /yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }'
test "$SHOW_AVAIL" && { test "$SHOW_AVAIL" && {