From 5106b550d36f50572cea55690075f0f07cee2994 Mon Sep 17 00:00:00 2001 From: Alexis Ben Miloud--Josselin Date: Mon, 16 Jul 2018 16:56:25 +0200 Subject: [PATCH] Make the code warning-free Using `shellcheck -s sh kvmstats.sh`. --- kvmstats.sh | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/kvmstats.sh b/kvmstats.sh index 2daaa9d..d465b8c 100644 --- a/kvmstats.sh +++ b/kvmstats.sh @@ -12,7 +12,7 @@ usage () { } POW=$(echo 1024 ^ 3 | bc) -while echo $1 | grep -q '^-[[:alnum:]]' +while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]' do case $1 in '-u') @@ -37,7 +37,7 @@ do done # since libvirt seems to store memoy in KiB, POW must be lowered by 1 -POW=$(expr $POW / 1024) +POW=$((POW / 1024)) for DEP in hxselect lvs tempfile do @@ -51,34 +51,35 @@ done TMPFILE=$(tempfile) LVSOUT=$(tempfile) -lvs --units b --nosuffix >$LVSOUT +lvs --units b --nosuffix >"$LVSOUT" for VM in $(virsh list --all --name) do - VCPU=$(hxselect -c 'domain vcpu' $TMPFILE + RUNNING=$(virsh domstate "$VM" | grep -q '^running$' && echo X) + echo "$VM $VCPU $RAM $DISK $RUNNING" +done >"$TMPFILE" ( echo vm vcpu ram disk running - cat $TMPFILE - awk '{ vcpu += $2; ram += $3; disk += $4; running += length($5) } END { print "TOTAL", vcpu, ram, disk, running }' <$TMPFILE + cat "$TMPFILE" + awk '{ vcpu += $2; ram += $3; disk += $4; running += length($5) } END { print "TOTAL", vcpu, ram, disk, running }' <"$TMPFILE" ) | column -t -rm $TMPFILE $LVSOUT +rm "$TMPFILE" "$LVSOUT"