Use virsh instead of hxselect

Also remove lvs. The code is much simpler now!
This commit is contained in:
Alexis Ben Miloud--Josselin 2018-10-11 14:48:44 +02:00
parent 33c452464f
commit d8c77c59d3
1 changed files with 13 additions and 30 deletions

View File

@ -1,9 +1,5 @@
#!/bin/sh
# NOTE: kvmstats relies on the hxselect(1) command to parse virsh' xml
# files. On Debian, this command is provided by the 'html-xml-utils'
# package.
set -e -u
usage () {
@ -11,7 +7,7 @@ usage () {
exit 1
}
for DEP in hxselect lvs tempfile bc
for DEP in bc tempfile virsh
do
if [ -z "$(which $DEP)" ]
then
@ -20,6 +16,7 @@ do
fi
done
SHOW_AVAIL=''
POW=$(echo 1024 ^ 3 | bc)
while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]'
do
@ -52,38 +49,24 @@ done
POW=$((POW / 1024))
TMPFILE=$(tempfile -s kvmstats)
LVSOUT=$(tempfile -s kvmstats)
lvs --units b --nosuffix >"$LVSOUT"
for VM in $(virsh list --all --name)
for VM in $(virsh list --name --all)
do
VCPU=$(hxselect -c 'domain vcpu' </etc/libvirt/qemu/"$VM.xml")
RAM_KIB=$(hxselect -c 'domain memory' </etc/libvirt/qemu/"$VM.xml")
RAM=$((RAM_KIB / 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=$(awk "/$VM/ { ans += \$NF } END { print ans / 1024 ^ 3 }" <"$LVSOUT")
break # avoid to compute DISK for each disk
;;
*.qcow2)
DISK=$(du -sBG "$DEV" | awk '{ print substr($1, 0, length($1) - 1) }')
;;
*)
DISK=0
esac
done
RUNNING=$(virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no)
echo "$VM" "$VCPU" "$RAM" "$DISK" "$RUNNING"
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 ]
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)
@ -91,4 +74,4 @@ done >"$TMPFILE"
fi
) | column -t
rm "$TMPFILE" "$LVSOUT"
rm "$TMPFILE"