You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
4.6 KiB

#!/usr/bin/env bash
ASCII=0
synopsis() {
cat << EOF
Usage : `basename $0` [-h] [-a]
EOF
}
help() {
cat << EOF
`basename $0` génère le graphe associé au réseau (graph.svgz).
`synopsis`
-h : affiche cette aide.
-a : ne génère pas le graphe dans le fichier mais sur stdout (en ASCII).
EOF
}
usage() {
synopsis
exit 1
}
args() {
local opt
while getopts "ha" opt; do
case $opt in
h) help; exit 0;;
a) ASCII=1;;
?) usage;;
esac
done
shift $(($OPTIND - 1))
[ $# -ne 0 ] && usage;
}
toGnuplot() {
n=1
echo "set term dumb"
while read; do
set - $REPLY
#echo $1
case "$1" in
graph) scale=$2; width=$3; height=$4;;
node) name=$2; x=$3; y=$4
echo "set label \"[$name]\" at $x, $y"
n=$(($n+1))
;;
edge) n1=$2; n2=$3; n3=$4; x1=$5; y1=$6;
shift 10
x2=$1; y2=$2
echo "set arrow $n from $x1,$y1 to $x2,$y2 nohead "
name=$3; x=$5; y=$6
echo "set label $name\" at $x, $y"
n=$(($n+1))
;;
esac
done
#echo "plot \"< echo '0 0'\""
echo "set nokey"
echo "unset tics"
echo "unset border"
echo "set xrange [ 0 : $width ]"
echo "set yrange [ 0 : $height ]"
echo "plot [0:$width] 0 with dot"
}
parse() {
echo
echo -e "\t\"$1\" [ $guestProps ]"
ETH=0
guest=$1
[ "$networks" == '""' -o -z "$networks" ] && return 0
set - $networks
for i; do
#set -x
net="$i"
comment=$(echo $net | cut -d '#' -f 2)
net=$(echo $net | cut -d '#' -f 1)
[ "$net" = "$comment" ] && comment=""
#net1=$(echo $net | sed -re 's/^.*NET_(.*)\(\$NET_.*\)$/\1/')
#net1=$(echo $net | sed -re 's/\$NET_([0-9G]+).*/\1/')
#net2=$(echo $net | sed -nre 's/.*NET_.*NET_(.*)\)$/\1/p')
net=$(echo $net | sed -re 's/.*NET_(.*)$/\1/')
[ "$net" = "G" ] && net=0
#net=$(computeEthLink $GUEST_NAME $net2)
#[ "$net1" = "G" ] && net1=0
#[ "$net2" = "G" ] && net2=0
#echo "$guest net:$net net1=$net1 net2=$net2" >&2
#read
#echo "net=$net."
#echo "comment=$comment"
#comment=""
#net=$net1
#[ -n "$net2" ] && net=$net2
case "$net" in
none) ;;
[0-9]*)
set +u
if [ -z "${switchs[$net]}" ]; then
switchs[$net]=1
props=$switchProps
if [ "$net" = "0" ]; then
props=$(echo $switchProps | sed -re 's,switch,internet,g')
props="$props label=\"Internet\",width=\"0\",height=\"0\""
else
props="$props width=\"0\",height=\"0\" label=\"$net\""
fi
echo -e "\t$net [ $props ]"
fi
set -u
echo -e "\t\"$guest\" -> $net [ taillabel=\"eth$ETH $comment\" $edgeProps ]"
;;
*) error "Eth error for $guest: $net"
esac
ETH=$(($ETH+1))
# ???
#[ -n "$net2" ] && {
# connects["$net1 -> $net2"]=1
#}
done
}
# main
declare -A connects
VDN_PATH=$(readlink -f $(dirname $0)/..); . $VDN_PATH/bin/functions.sh
export PATH="$VDN_PATH/bin:$PATH"
. $VDN_PATH/allocators/$VDN_RESOURCES_ALLOCATOR
args "$@"
guestProps="shape=box style=solid color=lightgray"
#switchProps="shape=ellipse color=gray fontsize=8"
#switchProps="imagepath=\"$VDN_PATH/svg\", image=\"switch.svg\",width=\"100px\",height=\"100px\",imagepos=tc,fontsize=8,shape=none"
switchProps="fontsize=12 shape=none"
edgeProps="labeldistance=4.0 len=1.8 fontsize=12 fontcolor=gray arrowhead=none"
networkDir=$NETWORK_DIR
echo "Build graph ($NETWORK_DIR/graph.svgz). "
#IDENT=0; computeNetworks
dot1=`mktemp`
#set -x
guests="`find $NETWORK_DIR -maxdepth 1 -follow -name "*.conf" | sort -R | \
sed -re 's/^.*\/([^/]+).conf$/\1/'`"
{
echo "digraph {"
#echo "overlap=scale;"
#echo 'rankdir="LR"'
echo 'splines=true;'
echo 'nodesep=0.1;'
#echo '1 -> 2 [style="invis"]'
for i in $guests; do
#loadGuestVars $i
unset networks
set -a
GUEST_NAME=$i
GUEST_OWNER=$USER
. $NETWORK_DIR/$i.conf
set +a
networks="$NETWORKS"
#networks=$(vdn-config $i NETWORKS)
parse $i
done
for k in "${!connects[@]}"; do
echo "=> $k" >&2
echo "$k [ arrowhead=none ]"
done
echo "}"
} > $dot1
if [ $ASCII = 1 ]; then
# obsolete
#dot2=`mktemp`
#plain=`mktemp`
#gnuplot=`mktemp`
#cat $dot1 | sed -re 's/taillabel/label/g' \
# > $dot2
##graph-easy --renderer neato --as ascii $dot2
#graph-easy --as ascii $dot2
warning "ASCII = 1 is obsolete !"
#neato -Tplain $dot2 > $plain
#cat $plain | toGnuplot > $gnuplot
#gnuplot $gnuplot | sed -re 's/\.//g' | sed -re 's/[<>]/\./g' \
# | sed -re 's/\[/'`echo -e "\033"`'\[1m/g' \
# | sed -re 's/\]/'`echo -e "\033"`'\[22m/g'
#rm -f $dot2 $plain $gnuplot
else
neato -Tsvg $dot1 | iconv -t UTF-8 > $NETWORK_DIR/graph.svg
gzip $NETWORK_DIR/graph.svg
mv $NETWORK_DIR/graph.svg.gz $NETWORK_DIR/graph.svgz
echo "Done."
fi
#cp $dot1 /tmp/dot
rm -f $dot1