From d8c77c59d36a042172723bb8accda3c446ecba9f Mon Sep 17 00:00:00 2001 From: Alexis Ben Miloud--Josselin Date: Thu, 11 Oct 2018 14:48:44 +0200 Subject: [PATCH] Use virsh instead of hxselect Also remove lvs. The code is much simpler now! --- kvmstats | 43 +++++++++++++------------------------------ 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/kvmstats b/kvmstats index 588ef86..b14586b 100755 --- a/kvmstats +++ b/kvmstats @@ -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' "$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"