[kvmstats] Some syntax arrangements
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Alexis Ben Miloud--Josselin 2020-08-07 11:59:03 +02:00
parent 883979e3e2
commit e27b9a29ce

View file

@ -1,40 +1,32 @@
#!/bin/sh #!/bin/sh
set -e
error () { error () {
echo "$0": "$@" >&2 echo "$0": "$@" >&2
exit 1 exit 1
} }
usage () { usage () {
echo 'usage: kvmstats [-a] [-u k|m|g] [-o human|html|csv]' >&2 echo 'usage:' "$0" '[-a] [-u k|m|g] [-o human|html|csv]' >&2
exit 1 exit 1
} }
for DEP in bc virsh for DEP in bc virsh
do do
command -v $DEP > /dev/null || error $DEP command not found command -v "$DEP" > /dev/null || error "$DEP" 'command not found'
done done
POW=$(echo 1024 ^ 3 | bc) POW="$(echo '1024 ^ 3' | bc)"
FMT=human FMT='human'
while [ $# -ne 0 ] while [ "$#" -ne 0 ]
do do
case $1 in case "$1" in
'-a') '-a')
SHOW_AVAIL=y SHOW_AVAIL='y'
;; ;;
'-o') '-o')
case $2 in case "$2" in
'human') 'csv'|'html'|'human')
FMT=human FMT="$2"
;;
'html')
FMT=html
;;
'csv')
FMT=csv
;; ;;
*) *)
usage usage
@ -43,15 +35,15 @@ do
shift shift
;; ;;
'-u') '-u')
case $2 in case "$2" in
'k') 'k')
POW=$(echo 1024 ^ 1 | bc) POW="$(echo '1024 ^ 1' | bc)"
;; ;;
'm') 'm')
POW=$(echo 1024 ^ 2 | bc) POW="$(echo '1024 ^ 2' | bc)"
;; ;;
'g') 'g')
POW=$(echo 1024 ^ 3 | bc) POW="$(echo '1024 ^ 3' | bc)"
;; ;;
*) *)
usage usage
@ -73,13 +65,13 @@ do
# mem # mem
# libvirt stores memory in KiB, POW must be lowered by 1 # libvirt stores memory in KiB, POW must be lowered by 1
virsh dommemstat "$VM" 2>&- | awk 'BEGIN{ret=1}$1~/^actual$/{print $2 / '$((POW / 1024))';ret=0}END{exit ret}' || virsh dommemstat "$VM" 2> /dev/null | awk 'BEGIN{ret=1}$1~/^actual$/{print $2 / '$((POW / 1024))';ret=0}END{exit ret}' ||
virsh dumpxml "$VM" | awk -F'[<>]' '$2~/^memory unit/{print $3/'$((POW / 1024))'}' virsh dumpxml "$VM" | awk -F'[<>]' '$2~/^memory unit/{print $3/'$((POW / 1024))'}'
# disk # disk
for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | awk '{print $1}') for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | awk '{print $1}')
do do
virsh domblkinfo "$VM" "$BLK" 2>&- virsh domblkinfo "$VM" "$BLK" 2> /dev/null
done | awk '/Physical:/ { size += $2 } END { print int(size / '${POW}') }' done | awk '/Physical:/ { size += $2 } END { print int(size / '${POW}') }'
# state # state
@ -91,7 +83,7 @@ done | xargs -n5 | {
nproc nproc
awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo
} | xargs -r printf 'AVAILABLE %s %s %s %s\n' } | xargs -r printf 'AVAILABLE %s %s %s %s\n'
} | case $FMT in } | case "$FMT" in
'human') 'human')
column -t column -t
;; ;;