1
2
3 # a hack to display OLSR topology information
4 #
5 # copyleft 2004 Bruno Randolf <br1@subnet.at>
6 # licensed under the GPL
7
8 use IO::Socket;
9 use Getopt::Long;
10
11 $TOPPATH = "/tmp";
12 $NAME = "topology";
13 $FILENAME = "$TOPPATH/$NAME.dot";
14 $EXT = "png";
15 $SERVER = "localhost";
16 $PORT = "2004";
17 $FULLSCREEN = 0;
18 $HELP = 0;
19 $KEEP = 0;
20 $BGCOLOR = "black";
21 $STYLE = 1;
22 $SIZE = "8,8";
23 $ROOTWIN = 0;
24 $ONCE = 0;
25 $GRAPH = 1;
26 $SHOW = 1;
27 $FONTNAME = "Courier";
28 $FONTSIZE = 12;
29 $LINEWIDTH = 0;
30 $LINECOLOR = 0;
31 $RESOLV = 0;
32
33 GetOptions ("server=s" => \$SERVER,
34 "port=s" => \$PORT,
35 "fullscreen!" => \$FULLSCREEN,
36 "help!" => \$HELP,
37 "keep!" => \$KEEP,
38 "bgcolor=s" => \$BGCOLOR,
39 "fontname=s" => \$FONTNAME,
40 "fontsize=s" => \$FONTSIZE,
41 "style=i" => \$STYLE,
42 "size=s" => \$SIZE,
43 "rootwin!" => \$ROOTWIN,
44 "once!" => \$ONCE,
45 "graph!" => \$GRAPH,
46 "show!" => \$SHOW,
47 "linewidth!" => \$LINEWIDTH,
48 "linecolor!" => \$LINECOLOR,
49 "resolv" => \$RESOLV,
50 );
51
52 if ($HELP) {
53 print << "EOF";
54 usage: $0 [ --server SERVER ] [--port PORT] [--fullscreen] [--keep]
55
56 a hack to display OLSR topology information
57
58 options:
59 --server SERVER connect to OLSR node (default: localhost)
60 --port PORT connect to port (default: 2004)
61 --bgcolor background color (default: black)
62 --fontname font name (default: Courier)
63 --fontsize font size (default: 12)
64 --style drawing style 1, 2 or 3 (default:1)
65 --size X,Y image size in inches for graphviz (default: 8,8)
66 --[no]fullscreen display fullscreen (default: off)
67 --[no]rootwin display in root window (default: off)
68 --[no]graph genereate graphics (default: on)
69 --[no]show display the graphics (default: on)
70 --[no]once run only 1 time, then exit (default: run forever)
71 --[no]linewith change line width according to metric (default: off)
72 --[no]linecolor change line color according to metric (default: off)
73 --[no]resolv resolv hostnames (default: off)
74 --[no]keep keep the .dot files with timestamp in /tmp (default: off)
75
76 requires the "graphviz" and "imagemagick" packages installed
77 and the "olsrd_dot_draw" plugin configured on the olsr node
78
79 EOF
80 exit;
81 }
82
83
84 `touch $TOPPATH/$NAME.$EXT`;
85
86 $remote = IO::Socket::INET->new(
87 Proto => "tcp",
88 PeerAddr => $SERVER,
89 PeerPort => $PORT,
90 )
91 or die "cannot connect to port $PORT at $SERVER!\nis the olsrd_dot_draw plugin loaded and configured to allow connections from this host?\n";
92
93 $f;
94 $start = 1;
95
96 $FULLOPT = "-backdrop -background black" if $FULLSCREEN;
97
98 if ($STYLE == 1) {
99 $DOT_CMD = "neato -Tpng -Gsize=${SIZE} -Gbgcolor=${BGCOLOR} -Gsplines=true -Nstyle=filled -Nfontname=${FONTNAME} -Nfontsize=${FONTSIZE} -Efontname=${FONTNAME} -Efontsize=${FONTSIZE} -Ncolor=white -Nfillcolor=white -Ecolor=grey -Elen=4 -Earrowsize=2 -Efontcolor=white $FILENAME -o $TOPPATH/$NAME.new";
100 }
101 elsif ($STYLE == 2) {
102 $BGCOLOR = "grey";
103 $DOT_CMD = "dot -Tpng -Gsize=${SIZE} -Elen=2 -Ncolor=grey -Nstyle=filled -Nfillcolor=white -Nfontname=${FONTNAME} -Nfontsize=${FONTSIZE} -Efontname=${FONTNAME} -Efontsize=${FONTSIZE} -Nfontcolor=red -Nwidth=1 -Gbgcolor=$BGCOLOR $FILENAME -o $TOPPATH/$NAME.new";
104 }
105 elsif ($STYLE == 3) {
106 $BGCOLOR = "\#ff6600";
107 $DOT_CMD = "neato -Tpng -Gsize=10,9 -Gbgcolor=${BGCOLOR} -Gsplines=true -Nstyle=filled -Nfontname=${FONTNAME} -Nfontsize=${FONTSIZE} -Efontname=${FONTNAME} -Efontsize=${FONTSIZE} -Nheight=1.3 -Nwidth=1.3 -Gsplines=true -Ncolor=darkslategrey -Nfillcolor=darkslategrey -Ecolor=black -Elen=4 -Earrowsize=3 $FILENAME -o $TOPPATH/$NAME.new";
108 }
109
110
111 while ( <$remote> )
112 {
113 $line = $_;
114 if ($RESOLV) {
115 $line = resolv( $line );
116 }
117 if ($LINEWIDTH || $LINECOLOR) {
118 $line = draw_thicker_metric_lines( $line );
119 }
120 $f = $f . $line;
121
122 # end of one graph
123 if ( $line =~ /}/i ) {
124 #print "* ";
125 open DOTFILE, ">$FILENAME";
126 print DOTFILE $f;
127 close DOTFILE;
128 $f = "";
129
130 if ($GRAPH) {
131 `$DOT_CMD`;
132 `mv $TOPPATH/$NAME.new $TOPPATH/$NAME.$EXT`;
133 }
134 if ($KEEP) {
135 `cp $TOPPATH/$NAME.dot $TOPPATH/$NAME-\$(date +'%Y-%m-%d-%H-%M-%S').dot`;
136 }
137 if ($SHOW) {
138 if ($ROOTWIN) {
139 system "display -window root -backdrop $TOPPATH/$NAME.$EXT &";
140 }
141 elsif ($start) {
142 system "display $FULLOPT -update 5 $TOPPATH/$NAME.$EXT &";
143 $start = 0;
144 }
145 }
146 exit if $ONCE;
147 }
148 }
149
150 print "connection closed\n";
151
152
153 sub resolv {
154 my $l = shift;
155 if ( $l =~ /\"(.*)\" -> "([^[]*)"(.*)/) {
156 my $from = $1;
157 my $to = $2;
158 my $rest = $3;
159 $from =~ s/\"//g;
160 $to =~ s/\"//g;
161 my $iaddrf = inet_aton($from);
162 my $fromn = gethostbyaddr($iaddrf, AF_INET);
163 my $iaddrt = inet_aton($to);
164 my $ton = gethostbyaddr($iaddrt, AF_INET);
165 $fromn = $from if ! $fromn;
166 $ton = $to if ! $ton;
167 $l = "\"$fromn\" -> \"$ton\"$rest\n";
168 }
169 return $l;
170 }
171
172
173 sub draw_thicker_metric_lines {
174 # sla 04.04.2005
175 # a hack to draws thicker lines for better metrics (the better the thicker).
176 # colorize the metric lines.
177 #
178 my $l = shift;
179 if ($l =~ /.*label="[0-9].*/){ # recognizion pattern
180 @n=split (/"/,$l); # split the string to get the metric value
181 if ($LINECOLOR) {
182 if ( $n[5]>2 ) { # colorize metrics greater than 2 gray
183 $c="888888";
184 }
185 else { # colorize metrics less than 2 black
186 $c="000000";
187 }
188 $setcol = "color=\"#$c\",";
189 }
190 if ($LINEWIDTH) {
191 if ($n[5]>0 && $n[5]<10) { # thicker lines if 10 > metric > 0
192 $lt=6/$n[5];
193 $at=$lt/2;
194 }
195 else { # at least draw a thin line
196 $lt=1;
197 $at=$lt;
198 }
199 $setwidth = "style=\"setlinewidth($lt)\", arrowsize=$at";
200 }
201 $l =~ s/\]/, $setcol $setwidth]/g; # replace pattern
202 }
203 return $l;
204 }
205
206 __END__
, come mostrato qui sotto nell'elenco degli allegati.
, potrebbe cambiare in futuro.