Add virsh-stats and virsh-cluster-stats scripts

This commit is contained in:
Benjamin Renard 2017-05-22 20:01:54 +02:00 committed by Benjamin Renard
parent cc8b1e55bc
commit 8839c7ed3c
2 changed files with 233 additions and 0 deletions

146
virsh-cluster-stats Executable file
View file

@ -0,0 +1,146 @@
#!/bin/bash
#
# Script to make usage stats on Linux Libvirtd hosts cluster
#
# Copyright (c) 2017 Benjamin Renard <brenard@zionetrix.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
HTML_OUTPUT=0
[ "$1" == "html" ] && HTML_OUTPUT=1
HYPS="
hyp-01
hyp-02"
TABLE_VMS_LINES=""
TABLE_HYPS_LINES=""
TOTAL_VM=0
TOTAL_VCPU=0
TOTAL_MEMORY_AFFECTED=0
TOTAL_MEMORY_USED=0
TOTAL_HYP=0
HYP_VM=0
TOTAL_HYP_RAM=0
TOTAL_HYP_RAM_USED=0
TOTAL_HYP_RAM_CACHED=0
TOTAL_HYP_SWAP_USED=0
TOTAL_HYP_TOTAL_RAM_USED=0
TOTAL_HYP_CPU=0
HYP_CPU_USED=0
IFS="
"
for hyp in $HYPS
do
HYP_VM=0
HYP_CPU_USED=0
HYP_MEMORY_AFFECTED=0
for line in $( ssh $hyp 'ps aux'|egrep '(qemu-system|/usr/bin/kvm)'|grep ' -name ' )
do
PID=$( echo "$line"|awk '{print $2}' )
NAME=$( echo "$line"|sed 's/^.* -name \([^ ]*\) .*$/\1/' )
MEMORY_AFFECTED=$( echo "$line"|sed 's/^.* -m \([0-9]*\) .*$/\1/' )
MEMORY_USED=$( echo -e "$line"|awk '{print $6}' )
let MEMORY_USED=MEMORY_USED/1024
let TOTAL_MEMORY_AFFECTED=MEMORY_AFFECTED+TOTAL_MEMORY_AFFECTED
let HYP_RAM_AFFECTED=HYP_RAM_AFFECTED+MEMORY_AFFECTED
let TOTAL_MEMORY_USED=MEMORY_USED+TOTAL_MEMORY_USED
VCPU=$( echo "$line"|sed 's/^.* -smp \([0-9]*\),.*$/\1/' )
let TOTAL_VCPU=VCPU+TOTAL_VCPU
let HYP_CPU_USED=HYP_CPU_USED+VCPU
let HYP_MEMORY_AFFECTED=HYP_MEMORY_AFFECTED+MEMORY_AFFECTED
CONSOLE=""
if [ $( echo "$line"|grep -c ' -vnc ' ) -gt 0 ]
then
CONSOLE="VNC : $( echo "$line"|sed 's/^.* -vnc \([^ ]*\) .*$/\1/' )"
elif [ $( echo "$line"|grep -c ' -spice ' ) -gt 0 ]
then
SPICE_PARAMS=$( echo "$line"|sed 's/^.* -spice -spice \([^ ]*\) .*$/\1/' )
SPICE_PORT=$( echo "$SPICE_PARAMS"|sed 's/^.*port=\([0-9]*\).*/\1/' )
SPICE_ADDR=$( echo "$SPICE_PARAMS"|sed 's/^.*addr=\([0-9]*\).*/\1/' )
CONSOLE="Spice : "
[ -n "$SPICE_ADDR" -a "$SPICE_ADDR" != "$SPICE_PARAMS" ] && CONSOLE="$CONSOLE$SPICE_ADDR"
[ -n "$SPICE_PORT" -a "$SPICE_PORT" != "$SPICE_PARAMS" ] && CONSOLE="$CONSOLE:$SPICE_PORT"
fi
TABLE_VMS_LINES="$TABLE_VMS_LINES<tr><td>$hyp</td><td>$PID</td><td>$NAME</td><td>$VCPU</td><td>$MEMORY_AFFECTED Mo</td><td>$MEMORY_USED Mo</td><td>$CONSOLE</td>"
let TOTAL_VM=TOTAL_VM+1
let HYP_VM=HYP_VM+1
done
HYP_FREE_RES=$( ssh $hyp 'free -m' )
HYP_RAM=$( echo "$HYP_FREE_RES"|grep ^Mem:|awk '{print $2}' )
HYP_RAM_USED=$( echo "$HYP_FREE_RES"|grep ^Mem:|awk '{print $3}' )
HYP_RAM_CACHED=$( echo "$HYP_FREE_RES"|grep ^Mem:|awk '{print $7}' )
HYP_SWAP_USED=$( echo "$HYP_FREE_RES"|grep ^Swap:|awk '{print $3}' )
let HYP_TOTAL_RAM_USED=HYP_RAM_USED+HYP_SWAP_USED-HYP_RAM_CACHED
let TOTAL_HYP_RAM=TOTAL_HYP_RAM+HYP_RAM
let TOTAL_HYP_RAM_USED=TOTAL_HYP_RAM_USED+HYP_RAM_USED
let TOTAL_HYP_RAM_CACHED=TOTAL_HYP_RAM_CACHED+HYP_RAM_CACHED
let TOTAL_HYP_SWAP_USED=TOTAL_HYP_SWAP_USED+HYP_SWAP_USED
let TOTAL_HYP_TOTAL_RAM_USED=TOTAL_HYP_TOTAL_RAM_USED+HYP_TOTAL_RAM_USED
HYP_CPU=$( ssh $hyp 'cat /proc/cpuinfo'|grep -c ^processor )
let TOTAL_HYP_CPU=TOTAL_HYP_CPU+HYP_CPU
let HYP_CPU_USED_PERC=HYP_CPU_USED*100/HYP_CPU
[ $HYP_CPU_USED_PERC -gt 100 ] && HYP_CPU_USED_PERC="<strong>$HYP_CPU_USED_PERC</strong>"
let HYP_MEMORY_AFFECTED_PERC=HYP_MEMORY_AFFECTED*100/HYP_RAM
[ $HYP_MEMORY_AFFECTED_PERC -gt 100 ] && HYP_MEMORY_AFFECTED_PERC="<strong>$HYP_MEMORY_AFFECTED_PERC</strong>"
let HYP_TOTAL_RAM_USED_PERC=HYP_TOTAL_RAM_USED*100/HYP_RAM
[ $HYP_TOTAL_RAM_USED_PERC -gt 100 ] && HYP_TOTAL_RAM_USED_PERC="<strong>$HYP_TOTAL_RAM_USED_PERC</strong>"
TABLE_HYPS_LINES="$TABLE_HYPS_LINES<tr><th colspan='2' align='right'>Total $hyp :</th><th>$HYP_VM VM(s)</th><th>$HYP_CPU_USED / $HYP_CPU ($HYP_CPU_USED_PERC%)</th><th>$HYP_MEMORY_AFFECTED Mo / $HYP_RAM Mo ($HYP_MEMORY_AFFECTED_PERC%)</th><th>$HYP_TOTAL_RAM_USED Mo / $HYP_RAM Mo ($HYP_TOTAL_RAM_USED_PERC%)</th><th></th></tr>"
let TOTAL_HYP=TOTAL_HYP+1
done
let TOTAL_HYP_CPU_USED_PERC=TOTAL_VCPU*100/TOTAL_HYP_CPU
[ $TOTAL_HYP_CPU_USED_PERC -gt 100 ] && TOTAL_HYP_CPU_USED_PERC="<strong>$TOTAL_HYP_CPU_USED_PERC</strong>"
let TOTAL_HYP_RAM_USED_PERC=TOTAL_MEMORY_AFFECTED*100/TOTAL_HYP_RAM
[ $TOTAL_HYP_RAM_USED_PERC -gt 100 ] && TOTAL_HYP_RAM_USED_PERC="<strong>$TOTAL_HYP_RAM_USED_PERC</strong>"
let TOTAL_MEMORY_USED_PERC=TOTAL_MEMORY_USED*100/TOTAL_HYP_RAM
[ $TOTAL_MEMORY_USED_PERC -gt 100 ] && TOTAL_MEMORY_USED_PERC="<strong>$TOTAL_MEMORY_USED_PERC</strong>"
let TOTAL_HYP_TOTAL_RAM_USED_PERC=TOTAL_HYP_TOTAL_RAM_USED*100/TOTAL_HYP_RAM
[ $TOTAL_HYP_TOTAL_RAM_USED_PERC -gt 100 ] && TOTAL_HYP_TOTAL_RAM_USED_PERC="<strong>$TOTAL_HYP_TOTAL_RAM_USED_PERC</strong>"
HTML="<table align='center'><tr><th>Physical host<th>PID</th><th>Name</th><th align='center'>vCPU</th><th align='center'>Memory affected</th><th align='center'>Memory used</th><th>Console</th></tr>
$TABLE_VMS_LINES
$TABLE_HYPS_LINES
<tr>
<th align='right'>Total :</th>
<th>$TOTAL_HYP hosts</th>
<th>$TOTAL_VM VM(s)</th>
<th>$TOTAL_VCPU / $TOTAL_HYP_CPU ($TOTAL_HYP_CPU_USED_PERC%)</th>
<th>$TOTAL_MEMORY_AFFECTED Mo / $TOTAL_HYP_RAM Mo ($TOTAL_HYP_RAM_USED_PERC%)</th>
<th>$TOTAL_MEMORY_USED Mo ($TOTAL_MEMORY_USED_PERC%)</th>
<th></th>
</tr>
</table>"
if [ $HTML_OUTPUT -eq 1 ]
then
echo -e "$HTML"
else
echo -e "$HTML"|lynx -stdin
fi

87
virsh-stats Executable file
View file

@ -0,0 +1,87 @@
#!/bin/bash
#
# Script to make usage stats on Linux Libvirtd host
#
# Copyright (c) 2017 Benjamin Renard <brenard@zionetrix.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
HTML_OUTPUT=0
[ "$1" == "html" ] && HTML_OUTPUT=1
TABLE_LINES=""
TOTAL_VCPU=0
TOTAL_MEMORY=0
TOTAL_MEMORY_USED=0
IFS="
"
for line in $( ps aux|egrep '(qemu-system|/usr/bin/kvm)'|grep ' -name ' )
do
PID=$( echo "$line"|awk '{print $2}' )
NAME=$( echo "$line"|sed 's/^.* -name \([^ ]*\) .*$/\1/' )
MEMORY=$( echo "$line"|sed 's/^.* -m \([0-9]*\) .*$/\1/' )
MEMORY_USED=$( echo -e "$line"|awk '{print $6}' )
let MEMORY_USED=MEMORY_USED/1024
let TOTAL_MEMORY=MEMORY+TOTAL_MEMORY
let TOTAL_MEMORY_USED=MEMORY_USED+TOTAL_MEMORY_USED
VCPU=$( echo "$line"|sed 's/^.* -smp \([0-9]*\),.*$/\1/' )
let TOTAL_VCPU=VCPU+TOTAL_VCPU
CONSOLE=""
if [ $( echo "$line"|grep -c ' -vnc ' ) -gt 0 ]
then
CONSOLE="VNC : $( echo "$line"|sed 's/^.* -vnc \([^ ]*\) .*$/\1/' )"
elif [ $( echo "$line"|grep -c ' -spice ' ) -gt 0 ]
then
SPICE_PARAMS=$( echo "$line"|sed 's/^.* -spice -spice \([^ ]*\) .*$/\1/' )
SPICE_PORT=$( echo "$SPICE_PARAMS"|sed 's/^.*port=\([0-9]*\).*/\1/' )
SPICE_ADDR=$( echo "$SPICE_PARAMS"|sed 's/^.*addr=\([0-9]*\).*/\1/' )
CONSOLE="Spice : "
[ -n "$SPICE_ADDR" -a "$SPICE_ADDR" != "$SPICE_PARAMS" ] && CONSOLE="$CONSOLE$SPICE_ADDR"
[ -n "$SPICE_PORT" -a "$SPICE_PORT" != "$SPICE_PARAMS" ] && CONSOLE="$CONSOLE:$SPICE_PORT"
fi
TABLE_LINES="$TABLE_LINES<tr><td>$PID</td><td>$NAME</td><td>$VCPU</td><td>$MEMORY Mo</td><td>$MEMORY_USED Mo</td><td>$CONSOLE</td>"
done
HYP_RAM=$( free -m|grep ^Mem:|awk '{print $2}' )
HYP_RAM_USED=$( free -m|grep ^Mem:|awk '{print $3}' )
HYP_RAM_CACHED=$( free -m|grep ^Mem:|awk '{print $7}' )
HYP_SWAP_USED=$( free -m|grep ^Swap:|awk '{print $3}' )
let HYP_TOTAL_RAM_USED=HYP_RAM_USED+HYP_SWAP_USED-HYP_RAM_CACHED
HYP_CPU=$( cat /proc/cpuinfo |grep -c ^processor )
HTML="<table><tr><th>PID</th><th>Name</th><th align='center'>vCPU</th><th align='center'>Memory</th><th align='center'>Memory used</th><th>Console</th></tr>
$TABLE_LINES
<tr>
<th colspan='2' align='right'>Total :</th>
<th>$TOTAL_VCPU</th>
<th>$TOTAL_MEMORY Mo</th>
<th>$TOTAL_MEMORY_USED Mo</th>
<th></th>
</tr>
<tr>
<th colspan='2' align='right'>Hyperviseur :</th>
<th>$HYP_CPU</th>
<th>$HYP_RAM Mo</th>
<th>$HYP_TOTAL_RAM_USED Mo</th>
<th></th>
</tr>
</table>"
if [ $HTML_OUTPUT -eq 1 ]
then
echo -e "$HTML"
else
echo -e "$HTML"|lynx -stdin
fi