kvm-tools/kvmstats
Alexis Ben Miloud--Josselin d8c77c59d3 Use virsh instead of hxselect
Also remove lvs. The code is much simpler now!
2018-10-11 14:48:44 +02:00

78 lines
1.8 KiB
Bash
Executable file

#!/bin/sh
set -e -u
usage () {
echo 'usage: kvmstats.sh [-a] [-u K|M|G]'
exit 1
}
for DEP in bc tempfile virsh
do
if [ -z "$(which $DEP)" ]
then
echo "kvmstats.sh: $DEP not found in \$PATH" 1>&2
exit 1
fi
done
SHOW_AVAIL=''
POW=$(echo 1024 ^ 3 | bc)
while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]'
do
case $1 in
'-u')
case $2 in
'K')
POW=$(echo 1024 ^ 1 | bc)
;;
'M')
POW=$(echo 1024 ^ 2 | bc)
;;
'G')
POW=$(echo 1024 ^ 3 | bc)
;;
*)
usage
esac
;;
'-a')
SHOW_AVAIL=y
;;
*)
usage
esac
shift
done
# since libvirt seems to store memoy in KiB, POW must be lowered by 1
POW=$((POW / 1024))
TMPFILE=$(tempfile -s kvmstats)
for VM in $(virsh list --name --all)
do
CPU=$(virsh vcpucount --maximum --current "$VM")
RAM=$(virsh domstats --balloon "$VM" | awk 'BEGIN { FS="=" } /balloon.maximum/ { print $2 / 1024 ^ 2 }')
DSK=$(for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | cut -d\ -f1)
do
virsh domblkinfo "$VM" "$BLK"
done | awk '/Physical:/ { size += $2 } END { print int(size / 1024 ^ 3) }')
RUN=$(virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no)
echo "$VM" "$CPU" "$RAM" "$DSK" "$RUN"
done >"$TMPFILE"
(
echo vm vcpu ram disk running
cat "$TMPFILE"
awk '/yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }' <"$TMPFILE"
if [ "$SHOW_AVAIL" ]
then
AV_CPU=$(awk '/^processor/ { cpu++ } END { print cpu }' /proc/cpuinfo)
AV_MEM=$(awk '/^MemTotal:/ { print int($2 / 1024 ^ 2) }' /proc/meminfo)
echo AVAILABLE "$AV_CPU" "$AV_MEM"
fi
) | column -t
rm "$TMPFILE"