From e179979d23befefa50148ebbfeb82f27651e72f2 Mon Sep 17 00:00:00 2001 From: Alexis Ben Miloud--Josselin Date: Mon, 29 Jul 2019 16:47:42 +0200 Subject: [PATCH] Add -o option This option control the output of the script. Possible output are human (same as usual), html and csv. This commit also does some minor syntax changes. --- kvmstats | 49 +++++++++++++++++++++++++++++++++++++------------ kvmstats.html | 20 ++++++++++++++++++++ 2 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 kvmstats.html diff --git a/kvmstats b/kvmstats index 449b0e0..89cfc7a 100755 --- a/kvmstats +++ b/kvmstats @@ -8,7 +8,7 @@ error () { } usage () { - echo 'usage: kvmstats [-a] [-u k|m|g]' >&2 + echo 'usage: kvmstats [-a] [-u k|m|g] [-o h|human|html|csv]' >&2 exit 1 } @@ -18,9 +18,29 @@ do done POW=$(echo 1024 ^ 3 | bc) +FMT=human while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]' do case $1 in + '-a') + SHOW_AVAIL=y + ;; + '-o') + case $2 in + 'h' | 'human') + FMT=human + ;; + 'html') + FMT=html + ;; + 'csv') + FMT=csv + ;; + *) + usage + ;; + esac + ;; '-u') case $2 in 'k') @@ -36,9 +56,6 @@ do usage esac ;; - '-a') - SHOW_AVAIL=y - ;; *) usage esac @@ -50,7 +67,7 @@ do echo "$VM" virsh vcpucount --maximum --current "$VM" # libvirt store memory in KiB, POW must be lowered by 1 - virsh domstats --balloon "$VM" | awk 'BEGIN { FS="=" } /balloon.maximum/ { print $2 / '$((POW / 1024))'}' + virsh domstats --balloon "$VM" | awk -F= '/balloon.maximum/ { print $2 / '$((POW / 1024))'}' for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | cut -d\ -f1) do virsh domblkinfo "$VM" "$BLK" @@ -59,10 +76,18 @@ do done | xargs -n5 | { echo vm vcpu ram disk running awk '{ print } /yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }' - if [ "$SHOW_AVAIL" ] - then - AV_CPU=$(nproc) - AV_MEM=$(awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo) - echo AVAILABLE "$AV_CPU" "$AV_MEM" - fi -} | column -t + test "$SHOW_AVAIL" && { + nproc + awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo + } | xargs printf 'AVAILABLE %s %s\n' +} | case $FMT in +'human') + column -t + ;; +'html') + awk 'BEGIN{print ""}{printf "";for(i=1;i<=NF;i++)printf "", $i;print ""}END{print "
%s
"}' + ;; +'csv') + tr ' ' ',' + ;; +esac diff --git a/kvmstats.html b/kvmstats.html new file mode 100644 index 0000000..90c90a3 --- /dev/null +++ b/kvmstats.html @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + +
vm vcpu ram disk running
icinga2-web4430yes
icinga2-node4430yes
bruno4212yes
evoadmin-web_packweb213no
icinga-test8415no
debian102222yes
ansible-test12117no
debian7440no
debian84418no
debian94430no
EvoBSD2142no
EvoMaquette4815no
for-build-rescue-cd844no
grafana445no
installbuster1120no
TOTAL(running)0004
AVAILABLE839