kvm-host: update kvmstats and add-vm
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Jérémy Lecour 2021-06-10 11:24:16 +02:00 committed by Jérémy Lecour
parent 3d715bae35
commit 4d7e6fd271
4 changed files with 92 additions and 68 deletions

View file

@ -37,6 +37,8 @@ disks="${disks:-}"
bridgeName="${bridgeName:-br0}"
doDryRun=${doDryRun:-false}
isoImagePath="${isoImagePath:-}"
debianVersion="${debianAuto:-stable}"
preseedURL="${preseedURL:-}"
export DIALOGOUT=$(mktemp --tmpdir=/tmp addvm.XXX)
# TODO: How to replace _ with a space??
@ -184,7 +186,7 @@ ${drbdadm} adjust "$vmName"
ssh $slaveKVMIP ${drbdadm} adjust "$vmName"
${drbdadm} -- --overwrite-data-of-peer primary "$vmName"
if !($doDryRun); then
if ! ($doDryRun); then
sleep 5 && drbd-overview | tail -4
drbdDiskPath="/dev/drbd/by-res/${vmName}/0"
@ -200,6 +202,7 @@ fi
virtHome=""
[ "$volhomeDisk" != "none" ] && virtHome="--disk path=/dev/drbd/by-disk/${volhomeDisk}/${vmName}_home,bus=virtio,io=threads,cache=none,format=raw"
bootMode="--pxe"
[ -n "${preseedURL}" ] && bootMode="--location https://deb.debian.org/debian/dists/${debianVersion}/main/installer-amd64/ --extra-args \"auto=true priority=critical url=${preseedURL} hostname=${vmName}\""
[ -f "$isoImagePath" ] && bootMode="--cdrom=$isoImagePath"
dryRun virt-install --connect=qemu:///system \

View file

@ -0,0 +1,4 @@
#!/bin/sh
{{ kvm_scripts_dir }}/kvmstats -a -o html > /var/www/kvmstats.html
/bin/chmod go+r /var/www/kvmstats.html

View file

@ -1,46 +1,54 @@
#!/bin/sh
# NOTE: kvmstats relies on the hxselect(1) 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 [-a] [-u K|M|G]'
error () {
echo "$0": "$@" >&2
exit 1
}
for DEP in hxselect lvs tempfile bc
usage () {
echo 'usage:' "$0" '[-a] [-u k|m|g] [-o human|html|csv]' >&2
exit 1
}
for DEP in bc virsh
do
if [ -z "$(which $DEP)" ]
then
echo "kvmstats.sh: $DEP not found in \$PATH" 1>&2
exit 1
fi
command -v "$DEP" > /dev/null || error "$DEP" 'command not found'
done
POW=$(echo 1024 ^ 3 | bc)
while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]'
POW="$(echo '1024 ^ 3' | bc)"
FMT='human'
while [ "$#" -ne 0 ]
do
case $1 in
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)
case "$2" in
'k')
POW="$(echo '1024 ^ 1' | bc)"
;;
'M')
POW=$(echo 1024 ^ 2 | bc)
'm')
POW="$(echo '1024 ^ 2' | bc)"
;;
'G')
POW=$(echo 1024 ^ 3 | bc)
'g')
POW="$(echo '1024 ^ 3' | bc)"
;;
*)
usage
esac
;;
'-a')
SHOW_AVAIL=y
shift
;;
*)
usage
@ -48,47 +56,41 @@ do
shift
done
# since libvirt seems to store memoy in KiB, POW must be lowered by 1
POW=$((POW / 1024))
TMPFILE=$(tempfile -s kvmstats)
LVSOUT=$(tempfile -s kvmstats)
lvs --units b --nosuffix >"$LVSOUT"
for VM in $(virsh list --all --name)
for VM in $(virsh list --name --all)
do
VCPU=$(hxselect -c 'domain vcpu' </etc/libvirt/qemu/"$VM.xml")
RAM_KIB=$(hxselect -c 'domain memory' </etc/libvirt/qemu/"$VM.xml")
RAM=$((RAM_KIB / POW))
for DEV in $(hxselect -s'\n' 'domain devices disk[device=disk] source' </etc/libvirt/qemu/"$VM.xml" | cut -d\" -f2)
echo "$VM"
# cpu
virsh vcpucount --current "$VM"
# mem
# libvirt stores memory in KiB, POW must be lowered by 1
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))'}'
# disk
for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | awk '{print $1}')
do
case $DEV in
/dev/drbd/*)
DISK=$(awk "/$VM/ { ans += \$NF } END { print ans / 1024 ^ 3 }" <"$LVSOUT")
break # avoid to compute DISK for each disk
;;
*.qcow2)
DISK=$(du -sBG "$DEV" | awk '{ print substr($1, 0, length($1) - 1) }')
;;
*)
DISK=0
esac
done
RUNNING=$(virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no)
echo "$VM" "$VCPU" "$RAM" "$DISK" "$RUNNING"
done >"$TMPFILE"
virsh domblkinfo "$VM" "$BLK" 2> /dev/null
done | awk '/Physical:/ { size += $2 } END { print int(size / '${POW}') }'
(
# state
virsh domstate "$VM" | grep -q '^running$' && echo yes || echo no
done | xargs -n5 | {
echo vm vcpu ram disk running
cat "$TMPFILE"
awk '/yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }' <"$TMPFILE"
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)
echo AVAILABLE "$AV_CPU" "$AV_MEM"
fi
) | column -t
rm "$TMPFILE" "$LVSOUT"
awk '{ print } /yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }'
test "$SHOW_AVAIL" && {
nproc
awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo
} | xargs -r printf 'AVAILABLE %s %s %s %s\n'
} | case "$FMT" in
'human')
column -t
;;
'html')
awk 'BEGIN{print "<html><body>\n<table>"}{printf "<tr>";for(i=1;i<=NF;i++)printf "<td>%s</td>", $i;print "</tr>"}END{print "</table>\n</body></html>"}'
;;
'csv')
tr ' ' ','
;;
esac

View file

@ -37,6 +37,21 @@
group: root
force: yes
- name: kvmstats cron is present
template:
src: kvmstats.cron.j2
dest: "/etc/cron.hourly/kvmstats"
mode: "0755"
owner: root
group: root
- name: entry for kvmstats in web page is present
lineinfile:
dest: /var/www/index.html
insertbefore: '</ul>'
line: '<li><a href="/kvmstats.html">kvmstats</a></li>'
# backward compatibility
- name: remove old migrate-vm script