Make the code warning-free

Using `shellcheck -s sh kvmstats.sh`.
This commit is contained in:
Alexis Ben Miloud--Josselin 2018-07-16 16:56:25 +02:00
parent c9905f5c4c
commit 5106b550d3
1 changed files with 15 additions and 14 deletions

View File

@ -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' </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)
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=$(cat $LVSOUT | awk "/$VM/ { ans += \$NF } END { print ans / 1024 ^ 3 }")
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=$(du -sBG "$DEV" | awk '{ print substr($1, 0, length($1) - 1) }')
;;
*)
DISK=0
esac
done
RUNNING=$(virsh domstate $VM | grep -q '^running$' && echo X)
echo $VM $VCPU $RAM $DISK $RUNNING
done >$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"