34e0f1cf6d
RIP 8-width *real* tab :(
77 lines
1.6 KiB
Bash
77 lines
1.6 KiB
Bash
#!/bin/sh
|
|
|
|
usage () {
|
|
echo 'usage: kvmstats.sh [-u K|M|G]'
|
|
exit 1
|
|
}
|
|
|
|
POW=$(echo 1024 ^ 3 | bc)
|
|
while 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
|
|
;;
|
|
*)
|
|
usage
|
|
esac
|
|
shift
|
|
done
|
|
|
|
# since libvirt seems to store memoy in KiB, POW must be lowered by 1
|
|
POW=$(expr $POW / 1024)
|
|
|
|
for DEP in hxselect lvs tempfile
|
|
do
|
|
if [ -z "$(which $DEP)" ]
|
|
then
|
|
echo "kvmstats.sh: $DEP not found in \$PATH" 1>&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
TMPFILE=$(tempfile)
|
|
LVSOUT=$(tempfile)
|
|
|
|
lvs --units b --nosuffix >$LVSOUT
|
|
|
|
for VM in $(virsh list --all --name)
|
|
do
|
|
VCPU=$(hxselect -c 'domain vcpu' </etc/libvirt/qemu/$VM.xml)
|
|
RAM=$(expr $(hxselect -c 'domain memory' </etc/libvirt/qemu/$VM.xml) / $POW)
|
|
for DEV in $(hxselect -s'\n' 'domain devices disk[device=disk] source' </etc/libvirt/qemu/$VM.xml | cut -d\" -f2)
|
|
do
|
|
case $DEV in
|
|
/dev/drbd/*)
|
|
DISK=$(cat $LVSOUT | awk "/$VM/ { ans += \$NF } END { print ans / 1024 ^ 3 }")
|
|
;;
|
|
*.qcow2)
|
|
DISK=$(du -sBG $DEV | awk '{ print substr($1, 0, length($1) - 1) }')
|
|
;;
|
|
*)
|
|
DISK=0
|
|
esac
|
|
done
|
|
echo $VM $VCPU $RAM $DISK
|
|
done >$TMPFILE
|
|
|
|
(
|
|
echo vm vcpu ram disk
|
|
cat $TMPFILE
|
|
awk '{ vcpu += $2; ram += $3; disk += $4 } END { print "TOTAL", vcpu, ram, disk }' <$TMPFILE
|
|
) | column -t
|
|
|
|
rm $TMPFILE $LVSOUT
|