Trim
logo xen

Ceux qui suivent un petit peu mon blog savent que j'utilise principalement Xen comme système de vitualisation.
La plupart du temps sous Open Suse, SLES, et maintenant Ubuntu 12.04
Et LVM2 pour la gestion des disques.

J'aime gérer ce système avec des scripts maison.
Ce qui me faisait le plus défaut c'est avoir rapidement les infos sur les domU installés.

  Les infos à lister:

  1. Nom du DomU.
  2. Son status On ou OFF
  3. Son ID
  4. Son Uptime
  5. Son temps CPU
  6. Son poids
  7. Son Cap
  8. Son State
  9. Sa Memory
  10. Ses Disques et leurs tailles
  11. Le port VNC attribué à la VM
  12. Qui est connecté  sur le VNC

Autant dire que ca fait beaucoup de choses....
J'ai commencé à gratter un petit script bash, bien pratique qui demande à être amélioré.

Ce script fonctionne si les DomU utilise des disques lvm



Le script:

ramfs.sh (pour travailler en ram sur les petit fichiers temporaires)

#!/bin/bash

#Antony MARTINEAU 2009.
#Mounting a temporary file system
#/sbin/ramfs.sh

ramfs="/tmpramfs"

if [ ! -d $ramfs ]; then
mkdir $ramfs
fi
if [ -z $(mount|grep "$ramfs"|sed -n "1{p;q;}"|awk '{print $1}') ]; then
mount -t tmpfs -o size=4M, tmpfs $ramfs > /dev/null 2>&1
fi


Domlist
#!/bin/bash
#DOMLIST V1.0
#Antony MARTINEAU
#Xen Virtual Machine Information by Antony MARTINEAU.

####################
ramfs.sh
###################
ramfs="/tmpramfs"
#Fonction pour afficher les echo en couleur
#31=rouge, 32=vert, 33=jaune,34=bleu, 35=rose, 36=cyan, 37= blanc
color()
{
printf '\033[%sm%s\033[m\n' "$@"
}
###############################
xmlistfic=$(ls $ramfs|grep xmlistfic.); for i in $xmlistfic; do pidlock=$(echo $i|cut -d"." -f2); if [ -z $(pidstat -p $pidlock|gawk '{print $2}'|grep $pidlock) ]; then rm -f $ramfs/$i; fi; done
schedlist=$(ls $ramfs|grep schedlist.); for i in $schedlist; do pidlock=$(echo $i|cut -d"." -f2); if [ -z $(pidstat -p $pidlock|gawk '{print $2}'|grep $pidlock) ]; then rm -f $ramfs/$i; fi; done
xmuptimefic=$(ls $ramfs|grep xmuptimefic.); for i in $xmuptimefic; do pidlock=$(echo $i|cut -d"." -f2); if [ -z $(pidstat -p $pidlock|gawk '{print $2}'|grep $pidlock) ]; then rm -f $ramfs/$i; fi; done
lvdispl=$(ls $ramfs|grep lvdispl.); for i in $lvdispl; do pidlock=$(echo $i|cut -d"." -f2); if [ -z $(pidstat -p $pidlock|gawk '{print $2}'|grep $pidlock) ]; then rm -f $ramfs/$i; fi; done
psaux=$(ls $ramfs|grep psaux.); for i in $psaux; do pidlock=$(echo $i|cut -d"." -f2); if [ -z $(pidstat -p $pidlock|gawk '{print $2}'|grep $pidlock) ]; then rm -f $ramfs/$i; fi; done
lsof=$(ls $ramfs|grep lsof.); for i in $lsof; do pidlock=$(echo $i|cut -d"." -f2); if [ -z $(pidstat -p $pidlock|gawk '{print $2}'|grep $pidlock) ]; then rm -f $ramfs/$i; fi; done
##############################
pid=$$
xmlist="$ramfs/xmlistfic.$pid"
xmsched="$ramfs/schedlist.$pid"
xmlv="$ramfs/lvdispl.$pid"
psaux="$ramfs/psaux.$pid"
dompath="/etc/xen/vm"
xmuptime="$ramfs/xmuptimefic.$pid"
lsof="$ramfs/lsof.$pid"
hostname=$(uname -n)
param=$(echo $*)
arg=$(echo $param| sed 's/[ \t]*$//')

################################HELP##################################
if [ "$param" = "--help" ]; then
echo "domlist v1.0 by Antony MARTINEAU"
echo "Xen virtual machine informations"
echo "Usage: domlist DomU_name (ex: domlist lippi-web)"
echo "Without argument, --all Domu will be show"
echo "domlist --all show all domU on all cluster node"
echo "option -g (gui) show list whithout column and backspace, separated by ;"
exit 0
fi
#####################################################################
for i in $*; do
if [ $i = "-g" ]; then
flaggui="true"
arg=$(echo $param|sed -r 's/(^| )-g( |$)/\1/g' | sed 's/[ \t]*$//')
flag="-g"
elif [ $i = "--all" ]; then
flagall="true"
elif [ $i = "all" ]; then
flagall="true"
fi
done
######################################################################

if [[ $arg = "all" ]]; then
arg="--all"
fi

if [[ -z $arg ]] || [[ "$arg" = "--all" ]]; then

vmlist=$(grep -w "name=" $dompath/* |cut -d'"' -f2)
vmline=$( echo $vmlist | wc -l)
caracini="0"
carac="0"
caracdiskini="0"
caracdisk="0"

for k in $vmlist
do
caracini=$(echo $k | wc -c )
caracdiskini=$(grep -o phy $dompath/$k | wc -l)

if [ "$caracdiskini" -gt "$caracdisk" ]; then
caracdisk=$(grep -o phy $dompath/$k | wc -l)

if [ "$caracdisk" = "1" ]; then
caracdisk=$(grep -o phy $dompath/$k | wc -l)
caracdisktitl="23"
caradiskclist="20"
list="1"
fi

if [ "$caracdisk" = "2" ]; then
caracdisk=$(grep -o phy $dompath/$k | wc -l)
caracdisktitl="56"
caradiskclist="103"
list="2"
fi

if [ "$caracdisk" = "3" ]; then
caracdisk=$(grep -o phy $dompath/$k | wc -l)
caracdisktitl="67"
caradiskclist="67"
list="3"
fi

if [ "$caracdisk" = "4" ]; then
caracdisk=$(grep -o phy $dompath/$k | wc -l)
caracdisktitl="88"
caradiskclist="88"
list="4"
fi
fi

if [ "$caracini" -gt "$carac" ]; then
carac=$(echo $k | wc -c )
caractitl=$(($carac+4))
caraclist=$(($carac-1))
fi

done
else
carac=$(echo $arg | wc -c)
caractitl=$(($carac+4))
caraclist=$(($carac-1))

fi
######################################################################
tab1()
{
echo `color 31 "On $hostname"`
echo -e `color 32 "Name Status ID Uptime Vcpus Time Weight Cap State Memory Disks-Resources VNC-Port"` |gawk -F" " ' { printf("%-'$caractitl's %-6s %-5s %-10s %-3s %-6s %-2s %-3s %-6s %-6s %-73s %-8s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) }'
}

tab2()
{
echo `color 31 "On $hostname"`
echo -e `color 32 "Name Status ID Uptime Vcpus Time Weight Cap State Memory Disks-Resources VNC-Port" | sed 's/ /;/g'`
}

xm list > $xmlist
xm sched-credit > $xmsched
xm uptime > $xmuptime
lvdisplay 2>/dev/null > $xmlv
ps aux > $psaux
lsof -Pn > $lsof

if [[ $flaggui = "true" ]]; then
tab2
else
tab1
fi

lines=$(grep -w "name" $dompath/* | wc -l)
if [ -z $lines ]; then
lines=1
fi

if [[ $flagall = "true" ]]; then
dom=$(echo $vmlist)
else
dom=$(echo $arg)
fi


if [[ $flaggui = "true" ]]; then
if [ -z $arg ]; then
dom=$(echo $vmlist)
fi
fi

if [ -z $1 ]; then
dom=$(echo $vmlist)
fi

for i in $dom; do
vmname[$i]=$(echo $i)
#vmname[$i]=$(grep -w "name=" $dompath/* |cut -d'"' -f2 |sed -n "$i{p;q;}")
vname=${vmname[$i]}
numofdisk=$(grep -o phy $dompath/$vname | wc -l)
migrating=$(grep -w $vname $xmlist | grep "migrating" | awk '{print $1}')
if [ ! -z $migrating ]; then
migrating="-Migrating-"
else
migrating=$empty
fi
pidvm=$(cat $psaux | grep -w qemu-dm |grep -w $vname | awk '{print $2}')
vmid=$(cat $xmlist | grep -w $vname | awk '{print $2}' | cut -d":" -f2)
vmcpu=$(cat $dompath/$vname | grep vcpus | cut -d"=" -f2)
vmstate=$(cat $xmlist | grep -w $vname | awk '{print $5}' | cut -d":" -f2)
vmtime=$empty
lvname=$(echo $res | sed 's/r/vm/')
vmexist=$(cat $xmlist | grep -w $vname | awk '{print $1}')
uptime=$empty
vmweigt=$empty
res1=$empty
res2=$empty
res3=$empty
res4=$empty
resd1=$empty
resd2=$empty
resd3=$empty
resd4=$empty
vncwho=$empty
vncport=$empty
vglvdisp1=$empty
vglvdisp2=$empty
vglvdisp3=$empty
vglvdisp4=$empty



for j in `seq 2 5`
do
disk="hda hdb hdc hdd"
res[$j]=$(awk -F"phy:/dev/" '{print $'$j'}' $dompath/$vname|sed '/./!d'|cut -d"," -f2,1)
hdlist[$j]=$(echo ${res[$j]} | cut -d"," -f2)
r[$j]=$(echo ${res[$j]} | cut -d"," -f1 | sed 's/drbd/r/')

for k in $disk
do
if [ "$k" = "${hdlist[$j]}" ]; then
if [ "$k" = "hda" ]; then
res1="${r[$j]}"
resd1="${r[$j]}(hda)"
fi
if [ "$k" = "hdb" ]; then
res2="${r[$j]}"
resd2="${r[$j]}(hdb)"
fi
if [ "$k" = "hdc" ]; then
res3="${r[$j]}"
resd3="${r[$j]}(hdc)"
fi
if [ "$k" = "hdd" ]; then
res4="${r[$j]}"
resd4="${r[$j]}(hdd)"
fi
fi
done
done

if [ ! -z $vmid ]; then
vmweight=$(cat $xmsched | grep -w $vname | awk '{print $3}' | cut -d":" -f2)
else
vmweight=$(cat $dompath/$vname | grep "cpu_weight" | cut -d"=" -f2)
fi

vmcap=$empty
if [ ! -z $vmid ]; then
vmcap=$(cat $xmsched | grep -w $vname | awk '{print $4}' | cut -d":" -f2)
else
vmcap=$(cat $dompath/$vname | grep cpu_cap | cut -d"=" -f2)
fi

second=$(cat $xmlist | grep -w $vname | awk '{print $6}' | cut -d":" -f2 | cut -d"." -f-1)
if [ -z $second ]; then #second est nulle
vmtime=""
else

if [ "$second" -lt 3600 ]; then
second=$(cat $xmlist | grep -w $vname | awk '{print $6}' | cut -d":" -f2 | cut -d"." -f-1)
vmtime=$(echo $second's')
else

min=$(($second/60))

if [ "$min" -lt 1440 ]; then
vmtime=$(echo $min'm')
else
hour=$(($min/60))
vmtime=$(echo $hour'h')
fi
fi
fi

day=$(cat $xmuptime | grep -w $vname | cut -d"," -f1|awk '{print $4}'| sed '/^$/d')

if [ ! -z $day ]; then #day est non nul
day=$(cat $xmuptime | grep -w $vname | awk '{print $3}')
hours=$(cat $xmuptime | grep -w $vname | cut -d":" -f1 | cut -d"," -f2 | sed 's/ //g')

if [ "$hours" -lt 10 ]; then
hours=$(cat $xmuptime | grep -w $vname | cut -d":" -f1 | cut -d"," -f2 | sed 's/ //g'| sed 's/^/0/g')
uptime=$(cat $xmuptime | grep -w $vname | awk '{print $3,$4,$5}'|cut -d":" -f2-3 | cut -d":" -f1)
uptime=$(echo $hours:$uptime |sed 's/^/'$day'd,/g'| sed 's/:/h:/2' | sed 's/$//')
else
uptime=$(cat $xmuptime | grep -w $vname | awk '{print $3,$4,$5}'|cut -d":" -f2-3 | cut -d":" -f1)
uptime=$(echo $hours:$uptime |sed 's/^/'$day'd,/g'| sed 's/:/h:/2' | sed 's/$//' )
fi

else #day est nul
hours=$(cat $xmuptime | grep -w $vname | cut -d"," -f1 | awk '{print $3}' | cut -d":" -f1)
if [ -z $hours ]; then
uptime=$(cat $xmuptime | grep -w $vname | awk '{print $3,$4,$5}'| sed 's/^/'$day'd,/g' | cut -d":" -f1 | sed 's/:/h:/2' | sed 's/$/m/' )
else
if [ "$hours" -lt 10 ]; then
uptime=$(cat $xmuptime | grep -w $vname | cut -d"," -f1 | awk '{print $3}' | cut -d":" -f-2 | sed 's/^/0/' | sed 's/^//' )
else
uptime=$(cat $xmuptime | grep -w $vname | awk '{print $3,$4,$5}'| sed 's/$//'| cut -d":" -f-2 |sed 's/^//')
fi
fi
fi

vmmem=$empty
if [ ! -z $vmid ]; then
if [ "$1" = "-g" ]; then
vmmem=$(cat $xmlist | grep -w $vname | awk '{print $3}' | cut -d":" -f2)
else
vmmem=$( cat $xmlist | grep -w $vname | awk '{print $3}' | cut -d":" -f2 |sed 's/$/MB/')
fi
else
if [ "$1" = "-g" ]; then
vmmem=$(cat /etc/xen/vm/$vname | grep memory | cut -d"=" -f2)
else
vmmem=$(cat $dompath/$vname | grep memory | cut -d"=" -f2 |sed 's/$/MB/')
fi
fi


sizer1=$empty
if [ ! -z $res1 ]; then
lv1=$(echo $res1 | sed 's/r/lv/')
if [ ! -z $lv1 ]; then
sizer1=$(cat $xmlv | grep -A8 -w $lv1 | grep "LV Size" | awk '{print $3,$4}' | sed 's/ //' | sed 's/\.00//'| sed -n '1p')
fi
fi


sizer2=$empty
if [ ! -z $res2 ]; then
lv2=$(echo $res2 | sed 's/r/lv/')
if [ ! -z $lv2 ]; then
sizer2=$(cat $xmlv | grep -A8 -w $lv2 | grep "LV Size" | awk '{print $3,$4}' | sed 's/ //' | sed 's/\.00//'| sed -n '1p')
fi
fi

sizer3=$empty
if [ ! -z $res3 ]; then
lv3=$(echo $res3 | sed 's/r/lv/')
if [ ! -z $lv3 ]; then
sizer3=$(cat $xmlv | grep -A8 -w $lv3 | grep "LV Size" | awk '{print $3,$4}' | sed 's/ //' | sed 's/\.00//'| sed -n '1p')
fi
fi

sizer4=$empty
if [ ! -z $res4 ]; then
lv4=$(echo $res4 | sed 's/r/lv/')
if [ ! -z $lv4 ]; then
sizer1=$(cat $xmlv | grep -A8 -w $lv4 | grep "LV Size" | awk '{print $3,$4}' | sed 's/ //' | sed 's/\.00//'| sed -n '1p')
fi
fi


if [ -z $pidvm ]; then
vncport="No vnc"
else
vncport=$( cat $lsof | grep $pidvm | grep "TCP *" | grep "LISTEN"| awk -F":*" '{print $2}' | awk '{print $1}'| sed -n '1p')
vncwho=$( cat $lsof | grep $pidvm | grep "TCP *" | awk -F"TCP" '{print $2}' | grep -v teradataordbms | grep "ESTABLISHED" | cut -d":" -f2 | sed 's/^....//')
fi

disk1=$(echo $vglvdisp1$resd1$sizer1)
disk2=$(echo $vglvdisp2$resd2$sizer2)
disk3=$(echo $vglvdisp3$resd3$sizer3)
disk4=$(echo $vglvdisp4$resd4$sizer4)

if [ -z $vmexist ]; then
vmrun="off"
else
vmrun="on"
fi

echo -e "$vname|$vmrun|$vmid|$uptime|$vmcpu|$vmtime|$vmweight|$vmcap|$vmstate|$vmmem|$disk1|$disk2|$disk3|$disk4|$vncport|$vncwho|$migrating"

done |

if [[ $flaggui = "true" ]]; then
sed 's/|/;/g'
else
gawk -F"|" '{ printf("%-'$caraclist's %-6s %-5s %-10s %-5s %-6s %-6s %-3s %-6s %-6s %-22s %-24s %-18s %-6s %-2s %-s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) }'
fi

if [ "$arg" = "--all" ]; then
arg=$empty
var="1"
fi

if [ "$var" = "1" ]; then ssh=0; else ssh=1; fi

rm -f $xmlist
rm -f $xmsched
rm -f $xmuptime
rm -f $xmlv
rm -f $psaux
rm -f $lsof

Voilà ce que ca donné au final:

Domlist

Je ne suis pas mécontent.