Fix unit -u option

Also clean the code
This commit is contained in:
Alexis Ben Miloud--Josselin 2018-11-09 16:06:22 +01:00
parent 8e657a28b4
commit e12c362f7b

View file

@ -1,9 +1,9 @@
#!/bin/sh
set -e -u
set -e
usage () {
echo 'usage: kvmstats.sh [-a] [-u K|M|G]'
echo 'usage: kvmstats [-a] [-u k|m|g]' >&2
exit 1
}
@ -11,25 +11,24 @@ for DEP in bc virsh
do
if [ -z "$(which $DEP)" ]
then
echo "kvmstats.sh: $DEP not found in \$PATH" 1>&2
echo "kvmstats: $DEP not found in \$PATH" >&2
exit 1
fi
done
SHOW_AVAIL=''
POW=$(echo 1024 ^ 3 | bc)
while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]'
do
case $1 in
'-u')
case $2 in
'K')
'k')
POW=$(echo 1024 ^ 1 | bc)
;;
'M')
'm')
POW=$(echo 1024 ^ 2 | bc)
;;
'G')
'g')
POW=$(echo 1024 ^ 3 | bc)
;;
*)
@ -45,17 +44,15 @@ do
shift
done
# since libvirt seems to store memoy in KiB, POW must be lowered by 1
POW=$((POW / 1024))
for VM in $(virsh list --name --all)
do
CPU=$(virsh vcpucount --maximum --current "$VM")
RAM=$(virsh domstats --balloon "$VM" | awk 'BEGIN { FS="=" } /balloon.maximum/ { print $2 / 1024 ^ 2 }')
# libvirt store memoy in KiB, POW must be lowered by 1
RAM=$(virsh domstats --balloon "$VM" | awk 'BEGIN { FS="=" } /balloon.maximum/ { print $2 / '$((POW / 1024))'}')
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) }')
done | awk '/Physical:/ { size += $2 } END { print int(size / '${POW}') }')
RUN=$(virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no)
echo "$VM" "$CPU" "$RAM" "$DSK" "$RUN"
done | (
@ -64,7 +61,7 @@ done | (
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)
AV_MEM=$(awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo)
echo AVAILABLE "$AV_CPU" "$AV_MEM"
fi
) | column -t