kvm-host: reorganize code for kvmstats
* add -V|--version flag * add -h|--help flag * normalize options parsing
This commit is contained in:
parent
03f846b94b
commit
72e8200d5b
|
@ -2,62 +2,44 @@
|
||||||
|
|
||||||
VERSION="21.10"
|
VERSION="21.10"
|
||||||
|
|
||||||
|
PROGNAME=$(basename "$0")
|
||||||
|
|
||||||
|
show_version() {
|
||||||
|
cat <<END
|
||||||
|
${PROGNAME} version ${VERSION}
|
||||||
|
|
||||||
|
Copyright 2018-2021 Evolix <info@evolix.fr>,
|
||||||
|
Alexis Ben Miloud--Josselin <abenmiloud@evolix.fr>,
|
||||||
|
Jérémy Lecour <jlecour@evolix.fr>
|
||||||
|
and others.
|
||||||
|
|
||||||
|
${PROGNAME} comes with ABSOLUTELY NO WARRANTY. This is free software,
|
||||||
|
and you are welcome to redistribute it under certain conditions.
|
||||||
|
See the GNU General Public Licence for details.
|
||||||
|
END
|
||||||
|
}
|
||||||
|
|
||||||
|
show_help() {
|
||||||
|
cat <<END
|
||||||
|
${PROGNAME} print stats about configured virtal servers
|
||||||
|
|
||||||
|
END
|
||||||
|
show_usage
|
||||||
|
}
|
||||||
|
show_usage() {
|
||||||
|
cat <<END
|
||||||
|
Usage: ${PROGNAME} --all
|
||||||
|
or ${PROGNAME} --output <human|html|csv>
|
||||||
|
or ${PROGNAME} --units <k|m|g>
|
||||||
|
END
|
||||||
|
}
|
||||||
|
|
||||||
error () {
|
error () {
|
||||||
echo "$0": "$@" >&2
|
echo "$0": "$@" >&2
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
usage () {
|
main() {
|
||||||
echo 'usage:' "$0" '[-a] [-u k|m|g] [-o human|html|csv]' >&2
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for DEP in bc virsh
|
|
||||||
do
|
|
||||||
command -v "$DEP" > /dev/null || error "$DEP" 'command not found'
|
|
||||||
done
|
|
||||||
|
|
||||||
POW="$(echo '1024 ^ 3' | bc)"
|
|
||||||
FMT='human'
|
|
||||||
while [ "$#" -ne 0 ]
|
|
||||||
do
|
|
||||||
case "$1" in
|
|
||||||
'-a')
|
|
||||||
SHOW_AVAIL='y'
|
|
||||||
;;
|
|
||||||
'-o')
|
|
||||||
case "$2" in
|
|
||||||
'csv'|'html'|'human')
|
|
||||||
FMT="$2"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
usage
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
'-u')
|
|
||||||
case "$2" in
|
|
||||||
'k')
|
|
||||||
POW="$(echo '1024 ^ 1' | bc)"
|
|
||||||
;;
|
|
||||||
'm')
|
|
||||||
POW="$(echo '1024 ^ 2' | bc)"
|
|
||||||
;;
|
|
||||||
'g')
|
|
||||||
POW="$(echo '1024 ^ 3' | bc)"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
usage
|
|
||||||
esac
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
usage
|
|
||||||
esac
|
|
||||||
shift
|
|
||||||
done
|
|
||||||
|
|
||||||
for VM in $(virsh list --name --all)
|
for VM in $(virsh list --name --all)
|
||||||
do
|
do
|
||||||
echo "$VM"
|
echo "$VM"
|
||||||
|
@ -96,3 +78,126 @@ done | xargs -n5 | {
|
||||||
tr ' ' ','
|
tr ' ' ','
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_units() {
|
||||||
|
case "$1" in
|
||||||
|
'k')
|
||||||
|
POW="$(echo '1024 ^ 1' | bc)"
|
||||||
|
;;
|
||||||
|
'm')
|
||||||
|
POW="$(echo '1024 ^ 2' | bc)"
|
||||||
|
;;
|
||||||
|
'g')
|
||||||
|
POW="$(echo '1024 ^ 3' | bc)"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf 'ERROR: Unknown unit value: %s. Possible values: %s\n' "$1" "k, m, g" >&2
|
||||||
|
echo "" >&2
|
||||||
|
show_usage >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
parse_output() {
|
||||||
|
case "$1" in
|
||||||
|
'csv'|'html'|'human')
|
||||||
|
FMT="$1"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf 'ERROR: Unknown output value : %s. Possible values: %s\n' "$1" "csv, html, human" >&2
|
||||||
|
echo "" >&2
|
||||||
|
show_usage >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check dependencies
|
||||||
|
for DEP in bc virsh
|
||||||
|
do
|
||||||
|
command -v "$DEP" > /dev/null || error "$DEP" 'command not found'
|
||||||
|
done
|
||||||
|
|
||||||
|
# default values
|
||||||
|
POW="$(echo '1024 ^ 3' | bc)"
|
||||||
|
FMT='human'
|
||||||
|
|
||||||
|
# Parse options
|
||||||
|
# based on https://gist.github.com/deshion/10d3cb5f88a21671e17a
|
||||||
|
while :; do
|
||||||
|
case $1 in
|
||||||
|
-h|-\?|--help)
|
||||||
|
show_help
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-V|--version)
|
||||||
|
show_version
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-a|--all)
|
||||||
|
SHOW_AVAIL='y'
|
||||||
|
;;
|
||||||
|
-u|--units)
|
||||||
|
# with value separated by space
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
parse_units "$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
printf 'ERROR: "-u|--units" requires a non-empty option argument.\n' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--units=?*)
|
||||||
|
# with value speparated by =
|
||||||
|
parse_units ${1#*=}
|
||||||
|
;;
|
||||||
|
--units=)
|
||||||
|
# without value
|
||||||
|
printf 'ERROR: "--units" requires a non-empty option argument.\n' >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
|
||||||
|
-o|--output)
|
||||||
|
# with value separated by space
|
||||||
|
if [ -n "$2" ]; then
|
||||||
|
parse_output "$2"
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
printf 'ERROR: "-o|--output" requires a non-empty option argument.\n' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--output=?*)
|
||||||
|
# with value speparated by =
|
||||||
|
parse_output ${1#*=}
|
||||||
|
;;
|
||||||
|
--output=)
|
||||||
|
# without value
|
||||||
|
printf 'ERROR: "--output" requires a non-empty option argument.\n' >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
|
||||||
|
--)
|
||||||
|
# End of all options.
|
||||||
|
shift
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
-?*|[[:alnum:]]*)
|
||||||
|
# ignore unknown options
|
||||||
|
printf 'ERROR: Unknown option : %s\n' "$1" >&2
|
||||||
|
echo "" >&2
|
||||||
|
show_usage >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
# Default case: If no more options then break out of the loop.
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
main
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue