kvm-tools/kvmstats/kvmstats
Alexis Ben Miloud--Josselin 1cc11d39ee [kvmstats] Typo
"I don't sing my mother tongue"
2019-09-03 11:10:53 +02:00

103 lines
2.2 KiB
Bash

#!/bin/sh
set -e
error () {
echo "$0": "$@" >&2
exit 1
}
usage () {
echo 'usage: kvmstats [-a] [-u k|m|g] [-o human|html|csv]' >&2
exit 1
}
for DEP in bc virsh
do
command -v $DEP > /dev/null || error $DEP command not found
done
POW=$(echo 1024 ^ 3 | bc)
FMT=human
while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]'
do
case $1 in
'-a')
SHOW_AVAIL=y
;;
'-o')
case $2 in
'human')
FMT=human
;;
'html')
FMT=html
;;
'csv')
FMT=csv
;;
*)
usage
;;
esac
;;
'-u')
case $2 in
'k')
POW=$(echo 1024 ^ 1 | bc)
;;
'm')
POW=$(echo 1024 ^ 2 | bc)
;;
'g')
POW=$(echo 1024 ^ 3 | bc)
;;
*)
usage
esac
;;
*)
usage
esac
shift
done
for VM in $(virsh list --name --all)
do
echo "$VM"
# cpu
virsh vcpucount --current "$VM"
# mem
# libvirt stores memory in KiB, POW must be lowered by 1
virsh dommemstat "$VM" 2>&- | 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))'}'
# disk
for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | cut -d\ -f1)
do
virsh domblkinfo "$VM" "$BLK" 2>&-
done | awk '/Physical:/ { size += $2 } END { print int(size / '${POW}') }'
# state
virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no
done | xargs -n5 | {
echo vm 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" && {
nproc
awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo
} | xargs -r printf 'AVAILABLE %s %s\n'
} | case $FMT in
'human')
column -t
;;
'html')
awk 'BEGIN{print "<html><body>\n<table>"}{printf "<tr>";for(i=1;i<=NF;i++)printf "<td>%s</td>", $i;print "</tr>"}END{print "</table>\n</body></html>"}'
;;
'csv')
tr ' ' ','
;;
esac