#!/bin/bash # # Script to make usage stats on Linux Libvirtd hosts cluster # # Copyright (c) 2017 Benjamin Renard # # 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$hyp$PID$NAME$VCPU$MEMORY_AFFECTED Mo$MEMORY_USED Mo$CONSOLE" 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="$HYP_CPU_USED_PERC" let HYP_MEMORY_AFFECTED_PERC=HYP_MEMORY_AFFECTED*100/HYP_RAM [ $HYP_MEMORY_AFFECTED_PERC -gt 100 ] && HYP_MEMORY_AFFECTED_PERC="$HYP_MEMORY_AFFECTED_PERC" 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="$HYP_TOTAL_RAM_USED_PERC" TABLE_HYPS_LINES="$TABLE_HYPS_LINESTotal $hyp :$HYP_VM VM(s)$HYP_CPU_USED / $HYP_CPU ($HYP_CPU_USED_PERC%)$HYP_MEMORY_AFFECTED Mo / $HYP_RAM Mo ($HYP_MEMORY_AFFECTED_PERC%)$HYP_TOTAL_RAM_USED Mo / $HYP_RAM Mo ($HYP_TOTAL_RAM_USED_PERC%)" 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="$TOTAL_HYP_CPU_USED_PERC" 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="$TOTAL_HYP_RAM_USED_PERC" let TOTAL_MEMORY_USED_PERC=TOTAL_MEMORY_USED*100/TOTAL_HYP_RAM [ $TOTAL_MEMORY_USED_PERC -gt 100 ] && TOTAL_MEMORY_USED_PERC="$TOTAL_MEMORY_USED_PERC" 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="$TOTAL_HYP_TOTAL_RAM_USED_PERC" HTML=" $TABLE_VMS_LINES $TABLE_HYPS_LINES
Physical hostPIDNamevCPUMemory affectedMemory usedConsole
Total : $TOTAL_HYP hosts $TOTAL_VM VM(s) $TOTAL_VCPU / $TOTAL_HYP_CPU ($TOTAL_HYP_CPU_USED_PERC%) $TOTAL_MEMORY_AFFECTED Mo / $TOTAL_HYP_RAM Mo ($TOTAL_HYP_RAM_USED_PERC%) $TOTAL_MEMORY_USED Mo ($TOTAL_MEMORY_USED_PERC%)
" if [ $HTML_OUTPUT -eq 1 ] then echo -e "$HTML" else echo -e "$HTML"|lynx -stdin fi