#!/bin/sh # NOTE: kvmstats relies on the hxselect command to parse virsh' xml # files. On Debian, this command is provided by the 'html-xml-utils' # package. set -e -u usage () { echo 'usage: kvmstats.sh [-u K|M|G]' exit 1 } POW=$(echo 1024 ^ 3 | bc) while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]' do case $1 in '-u') case $2 in 'K') POW=$(echo 1024 ^ 1 | bc) ;; 'M') POW=$(echo 1024 ^ 2 | bc) ;; 'G') POW=$(echo 1024 ^ 3 | bc) ;; *) usage esac ;; *) usage esac shift done # since libvirt seems to store memoy in KiB, POW must be lowered by 1 POW=$((POW / 1024)) for DEP in hxselect lvs tempfile do if [ -z "$(which $DEP)" ] then echo "kvmstats.sh: $DEP not found in \$PATH" 1>&2 exit 1 fi done TMPFILE=$(tempfile -s kvmstats) LVSOUT=$(tempfile -s kvmstats) lvs --units b --nosuffix >"$LVSOUT" for VM in $(virsh list --all --name) do VCPU=$(hxselect -c 'domain vcpu' "$TMPFILE" ( echo vm vcpu ram disk running cat "$TMPFILE" awk '{ vcpu += $2; ram += $3; disk += $4; running += length($5) } END { print "TOTAL", vcpu, ram, disk, running }' <"$TMPFILE" ) | column -t rm "$TMPFILE" "$LVSOUT"