virsh-stats/virsh-stats

88 lines
3.0 KiB
Bash
Executable File

#!/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