Make the code warning-free
Using `shellcheck -s sh kvmstats.sh`.
This commit is contained in:
parent
c9905f5c4c
commit
5106b550d3
29
kvmstats.sh
29
kvmstats.sh
|
@ -12,7 +12,7 @@ usage () {
|
||||||
}
|
}
|
||||||
|
|
||||||
POW=$(echo 1024 ^ 3 | bc)
|
POW=$(echo 1024 ^ 3 | bc)
|
||||||
while echo $1 | grep -q '^-[[:alnum:]]'
|
while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]'
|
||||||
do
|
do
|
||||||
case $1 in
|
case $1 in
|
||||||
'-u')
|
'-u')
|
||||||
|
@ -37,7 +37,7 @@ do
|
||||||
done
|
done
|
||||||
|
|
||||||
# since libvirt seems to store memoy in KiB, POW must be lowered by 1
|
# 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
|
for DEP in hxselect lvs tempfile
|
||||||
do
|
do
|
||||||
|
@ -51,34 +51,35 @@ done
|
||||||
TMPFILE=$(tempfile)
|
TMPFILE=$(tempfile)
|
||||||
LVSOUT=$(tempfile)
|
LVSOUT=$(tempfile)
|
||||||
|
|
||||||
lvs --units b --nosuffix >$LVSOUT
|
lvs --units b --nosuffix >"$LVSOUT"
|
||||||
|
|
||||||
for VM in $(virsh list --all --name)
|
for VM in $(virsh list --all --name)
|
||||||
do
|
do
|
||||||
VCPU=$(hxselect -c 'domain vcpu' </etc/libvirt/qemu/$VM.xml)
|
VCPU=$(hxselect -c 'domain vcpu' </etc/libvirt/qemu/"$VM.xml")
|
||||||
RAM=$(expr $(hxselect -c 'domain memory' </etc/libvirt/qemu/$VM.xml) / $POW)
|
RAM_KIB=$(hxselect -c 'domain memory' </etc/libvirt/qemu/"$VM.xml")
|
||||||
for DEV in $(hxselect -s'\n' 'domain devices disk[device=disk] source' </etc/libvirt/qemu/$VM.xml | cut -d\" -f2)
|
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
|
do
|
||||||
case $DEV in
|
case $DEV in
|
||||||
/dev/drbd/*)
|
/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
|
break # avoid to compute DISK for each disk
|
||||||
;;
|
;;
|
||||||
*.qcow2)
|
*.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
|
DISK=0
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
RUNNING=$(virsh domstate $VM | grep -q '^running$' && echo X)
|
RUNNING=$(virsh domstate "$VM" | grep -q '^running$' && echo X)
|
||||||
echo $VM $VCPU $RAM $DISK $RUNNING
|
echo "$VM $VCPU $RAM $DISK $RUNNING"
|
||||||
done >$TMPFILE
|
done >"$TMPFILE"
|
||||||
|
|
||||||
(
|
(
|
||||||
echo vm vcpu ram disk running
|
echo vm vcpu ram disk running
|
||||||
cat $TMPFILE
|
cat "$TMPFILE"
|
||||||
awk '{ vcpu += $2; ram += $3; disk += $4; running += length($5) } END { print "TOTAL", vcpu, ram, disk, running }' <$TMPFILE
|
awk '{ vcpu += $2; ram += $3; disk += $4; running += length($5) } END { print "TOTAL", vcpu, ram, disk, running }' <"$TMPFILE"
|
||||||
) | column -t
|
) | column -t
|
||||||
|
|
||||||
rm $TMPFILE $LVSOUT
|
rm "$TMPFILE" "$LVSOUT"
|
||||||
|
|
Loading…
Reference in a new issue