blib/lib/SNMP/Map.pm untested

filter: Statementstmt 0.0% 0 / 714 Branchbran 0.0% 0 / 256 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 55 Podpod 0.0% 0 / 37 totaltotal 0.0% 0 / 1162 CC 129 SCAR 47.7
CC
129
Cov
0%
get_output
506.0
getdata
306.0
printedges
132.0
CRAP 117.8
1 =head1 NAME
2  
3 SNMP::Map - Tool for drawing network map
4  
5 =head1 SYNOPSIS
6
7 my $map = new SNMP::Map;
8 $map->get_data(username=> 'user',
9 password =>'pass',
10 ipv3 => ['10.8.255.238','10.8.255.239','10.8.255.244','10.8.255.248'],
11 vlan=>'vlan-224'
12 );
13 $map->get_output(file => 'map.jpg');
14  
15 =head1 DESCRIPTION
16  
17 The SNMP::Map module is used for drawing network map of hosts which support SNMP.
18 Spanning Tree root switch can be distinguished by its position and color. Module provides
19 switches and ports on the picture linked by each other.You can get various information
20 about switch and its link. Image map file is also available. So, it can be used to create
21 javascript for popuping additional info.
22  
23 =head1 METHODS
24  
25 =head2 new
26  
27 This is the constructor. It does not accept any attributes.
28  
29 my $map = new SNMP::Map;
30  
31 =head2 get_data
32  
33 C<get_data> method gets data from switches using SNMP protocol. It starts from the spanning tree root switch,
34 and makes recursive calls to all switches linked to root. By default, it uses SNMP v3, but for switches
35 listed in ipv2 array reference,it uses SNMP v2. If a failure occurs, method returns underfined value,
36 you can use 'error' method to get error. At least, one ip adress must be specified by B<ipv2> or B<ipv3> attribute.
37 It is recommended to get more info, list all switches by B<ipv2> and B<ipv3> attributes. It was tested on
38 'WS-C2950','WS-C2960','WS-3750' platforms. 'WS-C2950' switches must be listed in B<ipv2>.
39  
40  
41 Attributes:
42  
43 =over
44  
45 =item username
46  
47 username for SNMP v3.
48  
49 =item password
50  
51 password for SNMP v3.
52  
53 =item community
54  
55 community for SNMP v2.
56  
57 =item timeout
58  
59 timeout for SNMP.
60  
61 =item ipv3
62  
63 array reference consist of list of hosts which is supported SNMP v3.
64  
65 =item ipv2
66  
67 array reference consist of list of hosts which is supported SNMP v2.
68  
69 =item debug
70  
71 switch on/off debug.can be true or false. Debug information is printed to STDERR.
72  
73 =item vlan
74  
75 vlan to draw map.
76  
77 =back
78  
79  
80 $map->get_data(username=>'user',
81 community=>'comm',
82 password=>'pass',
83 ipv3=>['10.8.255.115','10.8.255.118','10.8.255.119','10.8.255.111'],
84 ipv2=>['10.8.255.145','10.8.255.133','10.8.255.151','10.8.255.149'],
85 vlan => 'vlan-192');
86  
87 =head2 get_output
88  
89 This method is used for saving image into file. Also you can get an image map file.
90 A color value used in attributes of the method may be "h,s,v" (hue, saturation, brightness)
91 floating point numbers between 0 and 1, or an X11 color name such as 'white', 'black',
92 'red', 'green', 'blue', 'yellow', 'magenta', 'cyan', or 'burlywood'.
93  
94 Attributes:
95  
96 =over
97  
98 =item root_color
99  
100 sets the outline color for Spanning Tree root switch, and the default fill color if
101 the 'style' is 'filled' and 'fillcolor' is not specified.
102  
103 =item root_fillcolor
104  
105 sets the fill color for Spanning Tree root switch when the style is 'filled'. If not
106 specified, the 'fillcolor' when the 'style' is 'filled' defaults to be the same as the
107 outline color.
108  
109 =item root_fontcolor
110  
111 sets the label text color for Spanning Tree root switch.
112  
113 =item color
114  
115 sets the outline color for hosts and the default fill color if the 'style' is 'filled'
116 and 'fillcolor' is not specified.
117  
118 =item fillcolor
119  
120 sets the fill color for hosts when the style is 'filled'. If not specified, the 'fillcolor'
121 when the 'style' is 'filled' defaults to be the same as the outline color.
122  
123 =item fontcolor
124  
125 sets the label text color for hosts.
126  
127 =item shapes
128  
129 sets hash reference for the host`s shape, the key - regexp for the device platform, value - shape.
130 shape could be: 'record', 'plaintext', 'ellipse', 'circle', 'egg', 'triangle', 'box', 'diamond',
131 'trapezium', 'parallelogram', 'house', 'hexagon', 'octagon'. B<unknown> key is for hosts for which
132 platform is unknown.
133  
134 =item edges_colors
135  
136 sets array reference for colors of the links between hosts. First element of array reference
137 is the color for the unknown state of link. Last element is the color for link in blocking state.
138 2-7 elements - colors for hosts depending of its cost. Formula to correspond link color and its
139 cost - C<log(1/cost)*100> .
140  
141 =item file
142  
143 file to output image.
144  
145 =item format
146  
147 format of image file. Could be: 'ps','hpgl','pcl','mif','pic','gd','gd2','gif','jpg','png','wbmp',
148 'vrml','vtx,'mp','fig','svg','svgz','plain'.
149  
150 =item layout
151  
152 The B<layout> attribute determines which layout algorithm GraphViz.pm will use. Possible values are:
153 'dot','neato','twopi','circo','fdp'. 'dot' and 'circo' are recommended.
154  
155 =item bgcolor
156  
157 background color of image.
158  
159 =item height, width
160  
161 minimal height/width of image in inches.
162  
163 =item fontsize
164  
165 host`s label font size.
166  
167 =item style
168  
169 host`s style. Could be: 'filled', 'solid', 'dashed', 'dotted', 'bold', 'invis'.
170  
171 =item edge_style
172  
173 style of link. Could be: 'filled', 'solid', 'dashed', 'dotted', 'bold', 'invis'.
174  
175 =item cmap,imap
176  
177 file to output client-side/server-side image file. B<href> parameter in the html tags
178 is the ip adress for hosts and number for links. You can change it to B<nohref> and use
179 'nodes_info' and 'edge_info' methods to create javascript pop up.
180  
181 =back
182  
183 $map->get_output(file=>'../map.jpg',
184 root_fillcolor=>'blue',
185 root_fontcolor=>'cyan',
186 fontcolor=>'red',
187 color=>'green',
188 shapes => {unknown => 'record','IP Phone' =>'ellipse'},
189 edges_colors=>['blue','0,0,0.9','0,0,0.8','0,0,0.7','0,0.5,0.6','0,0,0.5','0,0,0.4','red'],
190 height=>5,
191 width=>9,
192 edge_style=>'dashed',
193 cmap=>'map'
194 );
195  
196 =head2 error
197  
198 return last error message.
199  
200 =head2 nodes_info
201  
202 returns hash reference with information about host.To use this method you must call 'get_data'
203 method first. Keys of the hash reference are the ip address of the hosts.The following
204 information could be obtained:
205  
206 $info = $map->nodes_info();
207 print $info->{10.8.255.101}{deviceID};
208 print $info->{10.8.255.101}{priority};
209 print $info->{10.8.255.101}{mac};
210 print $info->{10.8.255.101}{platform};
211 print $info->{10.8.255.101}{interfaces}{Gi0/1}{cost}; #cost for Gi0/1 port
212 print $info->{10.8.255.101}{interfaces}{Gi0/1}{cost}; #state for Gi0/1 port
213  
214 =head2 edge_info
215  
216 take the number of the link as first parameter. To use this method you must call 'get_data'
217 method first.Number correspond to B<href> parameter of the image map file of link. The
218 following information could be obtained:
219  
220 $info = $map->edge_info(5);
221 print $info->{from_ip};
222 print $info->{from_interface}; #name of the port
223 print $info->{from_virtual}; #name of the virtual port
224 print $info->{to_ip};
225 print $info->{to_interface}; #name of the port
226 print $info->{to_virtual}; #name of the virtual port
227  
228  
229 =head1 NOTES
230  
231 Module was tested on 'WS-C2950','WS-C2960','WS-3750' platforms. B<ipv2> option in 'get_data'
232 method is used for WS-C2950.
233  
234 =head1 AUTHOR
235  
236 Krol Alexander <kasha@bigmir.net>
237  
238 =head1 LICENSE
239  
240 This module is free software; you can redistribute it or modify it under the same terms as Perl itself.
241  
242 =cut
243  
244  
245 package SNMP::Map;
246 use strict;
247 use warnings;
248 use Net::SNMP;
249 use GraphViz;
250  
251 our $VERSION = '1.01';
252  
253 my $ID1='1.3.6.1.2.1.17.2.5.0'; #to get RootID(priority and Mac Adress)
254 my $ID2='1.3.6.1.2.1.2.2.1.6'; #to get STP Mac ID 2 IP
255 my $ID3='1.3.6.1.2.1.17.1.1.0'; #to get MAC for switch
256 my $ID4='1.3.6.1.2.1.17.2.2.0'; #to get priority for switch
257 my $ID5='1.3.6.1.2.1.17.2.15.1.3'; #port`s state
258 my $ID6='1.3.6.1.2.1.17.2.15.1.5'; #port`s cost
259 my $ID7='1.3.6.1.2.1.17.1.4.1.2'; #index correspondence
260 my $ID8='1.3.6.1.2.1.2.2.1.2'; #to get interfaces
261 my $ID9='1.3.6.1.2.1.2.2.1.1'; #to get port`s indexes
262 my $ID10='1.3.6.1.4.1.9.9.46.1.6.1.1.14'; #to get port`s status
263 my $ID11='1.3.6.1.4.1.9.9.23.1.2.1.1.6'; #to get DeviceID
264 my $ID12='1.3.6.1.4.1.9.9.23.1.2.1.1.7'; #to which port device is connected
265 my $ID13='1.3.6.1.4.1.9.9.23.1.2.1.1.4'; #to which ip device is connected
266 my $ID14='1.3.6.1.4.1.9.9.23.1.2.1.1.8'; #to get DevicePlatfrom
267 my $ID15='1.3.6.1.2.1.4.20.1.1'; #to get aliases
268 my $ID16='1.3.6.1.4.1.9.9.98.1.1.1.1.8'; #to get link between port index and virtual port index
269 my $ID17='1.3.6.1.4.1.9.9.98.1.1.1.1.1'; #to get phys link state
270 my $ID18='1.3.6.1.2.1.2.2.1.3'; #to get virtual link state
271  
272  
273 sub new
274 {
275 my $invocant= shift;
276 my $class = $invocant;
277 my $data={};
278 return bless $data,$class;
279 }
280  
281 sub get_data
282 {
283 my $self = shift;
284 my (@sessions,$ip,$RootMac,%options);
285  
286 %options = (@_);
287
288 $self->{result} = {};
289 $self->{mac2ip} = {};
290 $self->{aliases} = {};
291 $self->{correspondence} = {};
292 $self->{index} = {};
293 $self->{labels} = {};
294 $self->{draw2} = [];
295 $self->{info_edge} = [];
296 $self->{info_node} = {};
297 $self->{virtual} = {};
298 $self->{platforms} = {};
299 $self->{checked} = [];
300 $self->{shapes} = {};
301 $self->{GOT} = 0;
302  
303 $self->{USER} = $options{username};
304 $self->{PASS} = $options{password};
305 $self->{COMMUNITY} = $options{community};
306 $self->{TIMEOUT} = $options{timeout} || 1;
307 $self->{IP_CONF} = $options{ipv3} || [];
308 $self->{IPV2_CONF} = $options{ipv2} || [];
309 $self->{DEBUG} = $options{debug};
310 $self->{VLAN} = $options{vlan} || die "Vlan must be specified\n";
311  
312  
313  
314  
315 $self->getaliases();
316 $RootMac = $self->getrootid();
317 unless (defined($RootMac))
318 {
319 $self->{error} = 'Unable to determinate root mac for spanning tree';
320 return undef;
321 }
322 map{push(@sessions,$self->getmac2ip($_))} (@{$self->{IP_CONF}},@{$self->{IPV2_CONF}});
323 snmp_dispatcher();
324 map{$_->close()}@sessions;
325  
326  
327 $self->{root_ip} = $ip = $self->{mac2ip}{$RootMac};
328
329 unless (defined($ip))
330 {
331 $self->{error} = 'Unable to determinate root ip for spanning tree';
332 return undef;
333 }
334 my $version= grep($ip eq $_,@{$self->{IPV2_CONF}})? 'v2':'v3';
335 $self->{result}{$ip} = {};
336 $self->{checked}[0]=$ip;
337 $self->getdata($self->{result}{$ip},$ip,$version);
338 $self->getdeviceplatform($self->{result},$ip);
339 $self->{GOT} = 1;
340 return 1;
341 }
342 sub error
343 {
344 my $self = shift;
345 return $self->{error};
346 }
347 sub get_output
348 {
349 my $self = shift;
350 my $ip = $self->{root_ip};
351 my %options = (@_);
352 die "No data to output" unless($self->{GOT});
353  
354 $self->{root_color} = $options{root_color} || '0.85,0.58,0.8';
355 $self->{color} = $options{color} || '0.54,0.34,0.67';
356 $self->{root_fillcolor} = $options{root_fillcolor} || '0.70,0.70,0.90';
357 $self->{fillcolor} = $options{fillcolor} || '0.72,0.96,0.17';
358 $self->{root_fontcolor} = $options{root_fontcolor} || '0.85,0.58,0.8';
359 $self->{fontcolor} = $options{fontcolor} || '0.54,0.34,0.67';
360  
361  
362 $self->{shapes} = $options{shapes} || {unknown => 'ellipse','IP Phone' => 'diamond'};
363 $self->{edges_colors} = $options{edges_colors} || ['0.4,0.3,0.8','0,0,0.9','0,0,0.8','0,0,0.6','0,0,0.4','0,0,0.2',
364 '0,0,0','1,1,1'];
365  
366 my $file = $options{file};
367 my $format = $options{format} || 'jpg';
368  
369 $options{layout} ||= 'dot';
370 $options{bgcolor} ||= 'white';
371 $options{height} ||= 5;
372 $options{width} ||= 5;
373 $options{style} ||= 'filled';
374 $options{fontsize} ||= 10;
375 $options{edge_style} ||= 'filled';
376  
377 my $picture = GraphViz->new(directed=>0,layout=>$options{layout},rankdir=>$options{rankdir},
378 bgcolor=>$options{bgcolor},height=>$options{height},width=>$options{width},
379 overlap=>'false',name=>'map',node=>{style=>$options{style},fontsize=>$options{fontsize},
380 fontname=>$options{fontname}},edge=>{style=>$options{edge_style}});
381 $self->output_nodes($self->{result},$picture,$ip);
382 $self->output_edges($self->{result},$ip);
383 $self->printedges($picture);
384  
385  
386
387 if ($format eq 'ps'){$picture->as_ps($file);}
388 elsif ($format eq 'hpgl') {$picture->as_hpgl($file);}
389 elsif ($format eq 'pcl') {$picture->as_pcl($file);}
390 elsif ($format eq 'mif') {$picture->as_mif($file);}
391 elsif ($format eq 'pic') {$picture->as_pic($file);}
392 elsif ($format eq 'gd') {$picture->as_gd($file);}
393 elsif ($format eq 'gd2') {$picture->as_gd2($file);}
394 elsif ($format eq 'gif') {$picture->as_gif($file);}
395 elsif ($format eq 'jpg') {$picture->as_jpeg($file);}
396 elsif ($format eq 'png') {$picture->as_png($file);}
397 elsif ($format eq 'wbmp') {$picture->as_wbmp($file);}
398 elsif ($format eq 'vrml') {$picture->as_vrml($file);}
399 elsif ($format eq 'vtx') {$picture->as_vtx($file);}
400 elsif ($format eq 'mp') {$picture->as_mp($file);}
401 elsif ($format eq 'fig') {$picture->as_fig($file);}
402 elsif ($format eq 'svg') {$picture->as_svg($file);}
403 elsif ($format eq 'svgz') {$picture->as_svgz($file);}
404 elsif ($format eq 'plain') {$picture->as_plain($file);}
405 else {die "Unknown format - $format for get_output \n";}
406
407 $picture->as_cmapx($options{cmap}) if (defined($options{cmap}));
408 $picture->as_ismap($options{imap}) if (defined($options{imap}));
409 }
410  
411 sub getdeviceplatform
412 {
413 my ($self,$result,$root_ip)= @_;
414 my $platforms = $self->{platforms};
415 my ($ipt,$DevicePlatform);
416 local $_;
417  
418
419
420 foreach (keys %{$result->{$root_ip}{devices}})
421 {
422 ($ipt) = grep(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,keys %{$result->{$root_ip}{devices}{$_}});
423 $platforms->{$ipt} = $result->{$root_ip}{devices}{$_}{platform};
424 next unless (keys %{$result->{$root_ip}{devices}{$_}{$ipt}{devices}});
425 $self->getdeviceplatform($result->{$root_ip}{devices}{$_},$ipt);
426 }
427 }
428  
429 sub getdeviceid
430 {
431 my ($self,$result,$root_ip,$ip)= @_;
432 my ($ipt,$DeviceID);
433 local $_;
434  
435
436
437 foreach (keys %{$result->{$root_ip}{devices}})
438 {
439 ($ipt) = grep(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,keys %{$result->{$root_ip}{devices}{$_}});
440 return $result->{$root_ip}{devices}{$_}{DeviceID} if ($ip eq $ipt);
441 next unless (keys %{$result->{$root_ip}{devices}{$_}{$ipt}{devices}});
442 $DeviceID = $self->getdeviceid($result->{$root_ip}{devices}{$_},$ipt,$ip);
443 return $DeviceID if (defined($DeviceID));
444 }
445 return undef;
446 }
447  
448 sub getaliases
449 {
450 my ($self) = shift;
451 my ($aliases,$virtual) = ($self->{aliases},$self->{virtual});
452 my ($session,$error,$ip,$status,@sessions);
453 my $VirtState={};
454 my @ips = (@{$self->{IP_CONF}},@{$self->{IPV2_CONF}});
455 foreach $ip (@ips)
456 {
457 ($session,$error)= Net::SNMP->session( -hostname => $ip,
458 -version => 'snmpv3',
459 -timeout => $self->{TIMEOUT},
460 -username => $self->{USER},
461 -authpassword => $self->{PASS},
462 -nonblocking => 1,
463 -maxmsgsize => 65535);
464  
465 die "Error while creating SNMP object:$error\n" if (!defined($session));
466
467 $status = $session->get_table(-baseoid => $ID15,
468 -callback => [\&push2aliases,$self]);
469  
470 $self->debug("Error while sending a SNMP get-request $ID15:",$session->error()) if (!defined($status));
471  
472 $status = $session->get_table(-baseoid => $ID16,
473 -callback => [\&pushphyslink2virtlink,$self]);
474
475 $self->debug("Error while sending a SNMP get-request $ID16:",$session->error()) if (!defined($status));
476
477 $status = $session->get_table(-baseoid => $ID17,
478 -callback => [\&pushphyslinkstate,$self]);
479
480 $self->debug("Error while sending a SNMP get-request $ID17:",$session->error()) if (!defined($status));
481  
482 $status = $session->get_table(-baseoid => $ID18,
483 -callback => [\&pushvirtlinkstate,$self,$VirtState]);
484
485 $self->debug("Error while sending a SNMP get-request $ID18:",$session->error()) if (!defined($status));
486  
487 push(@sessions,$session);
488 }
489 snmp_dispatcher();
490 map($_->close(),@sessions);
491  
492 foreach $ip (@ips)
493 {
494 while (my ($key,$val) = each %{$virtual->{$ip}})
495 {
496 delete $virtual->{$ip}{$key} if ($val == -1 or !defined($VirtState->{$ip}{$val}));
497 }
498 }
499  
500 }
501 sub pushvirtlinkstate
502 {
503 my ($session,$self,$VirtState) = @_;
504 my $ip = $session->hostname;
505 my $lists = $session->var_bind_list;
506 local $_;
507  
508
509 if (!defined($lists))
510 {
511 $self->debug("Error while sending a SNMP get-request for $ID18:\t ".$session->hostname.':'.$session->error());
512 return;
513 }
514
515 while (my ($key,$val) = each %$lists)
516 {
517 $VirtState->{$ip}{substr($key,20)} = $val if ($val == 53);
518 }
519 }
520 sub pushphyslinkstate
521 {
522 my ($session,$self) = @_;
523 my $virtual = $self->{virtual};
524 my $ip = $session->hostname;
525 my $lists = $session->var_bind_list;
526 local $_;
527 my $index;
528
529 if (!defined($lists))
530 {
531 $self->debug("Error while sending a SNMP get-request for $ID17:\t ".$session->hostname.':'.$session->error());
532 return;
533 }
534
535 while (my ($key,$val) = each %$lists)
536 {
537 $index = substr($key,29);
538 $virtual->{$ip}{$index} = -1 if ($val == 1);
539 }
540 }
541 sub pushphyslink2virtlink
542 {
543 my ($session,$self) = @_;
544 my $virtual = $self->{virtual};
545 my $ip = $session->hostname;
546 my $lists = $session->var_bind_list;
547 local $_;
548 my $index;
549
550 if (!defined($lists))
551 {
552 $self->debug("Error while sending a SNMP get-request for $ID16:\t ".$session->hostname.':'.$session->error());
553 return;
554 }
555
556 while (my ($key,$val) = each %$lists)
557 {
558 $index = substr($key,29);
559 no warnings;
560 $virtual->{$ip}{$index} = $val if ($index ne $val and $val and $virtual->{$ip}{$index} != -1);
561 use warnings;
562 }
563
564 }
565 sub push2aliases
566 {
567 my ($session,$self) = @_;
568 my $aliases = $self->{aliases};
569 my $ip = $session->hostname;
570 my $lists = $session->var_bind_list;
571 local $_;
572  
573
574 if (!defined($lists))
575 {
576 $self->debug("Error while sending a SNMP get-request for $ID15:\t ".$session->hostname.':'.$session->error());
577 return;
578 }
579
580 foreach (values %$lists)
581 {
582 $aliases->{$_} = $ip if ($_ ne $ip);
583 }
584
585 }
586 sub getrootid
587 {
588 my ($self) = shift;
589 my ($session,$error,$ip,$lists,%settings,$version,$vlan_v2);
590 my @ips = (@{$self->{IP_CONF}},@{$self->{IPV2_CONF}});
591 $vlan_v2 = $self->{VLAN};
592 $vlan_v2 =~ s/vlan-(\d+)/$1/;
593
594
595
596 foreach $ip (@ips)
597 {
598 $version = grep($_ eq $ip,@{$self->{IPV2_CONF2}})? 'v2':'v3';
599 if ($version eq 'v3')
600 {
601 %settings = ( -hostname => $ip,
602 -version => 'snmpv3',
603 -timeout => $self->{TIMEOUT},
604 -username => $self->{USER},
605 -authpassword => $self->{PASS},
606 -maxmsgsize => 65535);
607 }
608 else
609 {
610 %settings = ( -hostname => $ip,
611 -version => 'snmpv2',
612 -community => $self->{COMMUNITY}.'@'.$vlan_v2,
613 -timeout => $self->{TIMEOUT},
614 -maxmsgsize => 65535);
615 }
616
617 ($session,$error) = Net::SNMP->session(%settings);
618 die "Error while creating SNMP object:$error\n" if (!defined($session));
619
620 if ($version eq 'v3')
621 {
622 $lists= $session->get_request(
623 -varbindlist => [$ID1],
624 -contextname => $self->{VLAN},
625 );
626 }
627 else
628 {
629 $lists= $session->get_request(
630 -varbindlist => [$ID1],
631 );
632 }
633
634  
635 if (!defined($lists))
636 {
637 $self->debug("Error while sending a SNMP get-request $ID1:",$session->error());
638 $session->close();
639 last;
640 }
641  
642 if (substr($lists->{$ID1},0,14) ne 'noSuchInstance')
643 {
644 use bigint;
645 no warnings;
646 my $RootMac = hex(substr($lists->{$ID1},6));
647 no bigint;
648 use warnings;
649 $session->close();
650 return $RootMac;
651 }
652 $session->close();
653 }
654 return undef;
655
656 }
657  
658  
659 sub getmac2ip
660 {
661 my ($self,$ip) = @_;
662  
663 my ($session,$error,$status);
664
665 ($session,$error) = Net::SNMP->session(
666 -hostname => $ip,
667 -version => 'snmpv3',
668 -timeout => $self->{TIMEOUT},
669 -username => $self->{USER},
670 -authpassword => $self->{PASS},
671 -nonblocking => 1,
672 -maxmsgsize => 65535,
673 );
674  
675
676 die "Error while creating SNMP object:$error\n" if (!defined($session));
677
678
679 $status= $session->get_table(
680 -baseoid => $ID2,
681 -callback => [\&push2mac2ip,$self],
682 );
683 $self->debug("Error while sending a SNMP get-request $ID2:",$session->error()) if (!defined($status));
684 return $session;
685 }
686  
687 sub push2mac2ip
688 {
689 my ($session,$self) = @_;
690 my $mac2ip = $self->{mac2ip};
691 my ($min,@ar,$binmin);
692 my $ip = $session->hostname;
693 my $lists = $session->var_bind_list;
694 local $_;
695
696  
697 if (!defined($lists))
698 {
699 $self->debug( "Error while sending a SNMP get-request for $ID2:$ip ".$session->error());
700 return;
701 }
702
703 @ar= sort { $a cmp $b } values %$lists;
704  
705 foreach (@ar)
706 {
707  
708 if (length == 14)
709 {
710 $min = $_;
711 last;
712 }
713
714 }
715 no warnings;
716 use bigint;
717 my $dec = hex($min);
718 $dec = $dec % 2? $dec-1:$dec;
719 $mac2ip->{$dec} = $ip;
720 no bigint;
721 use warnings;
722 }
723  
724  
725 sub getdata
726 {
727 my ($self,$result,$ip,$version) = @_;
728 my ($aliases,$correspondence,$index,$vlan) = ($self->{aliases},$self->{correspondence},$self->{index},$self->{VLAN});
729 my ($vlan_v2,%settings,$error,$session,$status,$session2,@ToCheck);
730 local $_;
731  
732  
733  
734  
735 $vlan_v2 = $vlan;
736 $vlan_v2 =~ s/vlan-(\d+)/$1/;
737 if ($version eq 'v3')
738 {
739 %settings = ( -hostname => $ip,
740 -version => 'snmpv3',
741 -timeout => $self->{TIMEOUT},
742 -username => $self->{USER},
743 -authpassword => $self->{PASS},
744 -nonblocking => 1,
745 -maxmsgsize => 65535);
746 }
747 else
748 {
749 %settings = ( -hostname => $ip,
750 -version => 'snmpv2',
751 -community => $self->{COMMUNITY}.'@'.$vlan_v2,
752 -timeout => $self->{TIMEOUT},
753 -nonblocking => 1,
754 -maxmsgsize => 65535);
755 }
756 ($session,$error) = Net::SNMP->session(%settings);
757  
758 die("Error while creating SNMP object:$error\n") if (!defined($session));
759  
760
761 $self->getsettings(\%settings,$ID3,\&pushbridgemac,$result,$version,'request');
762 $status= $session->get_request(%settings);
763 $self->debug("Error while sending a SNMP get-request $ID3:".$session->error()) if (!defined($status));
764
765
766
767 $self->getsettings(\%settings,$ID4,\&pushbridgepri,$result,$version,'request');
768 $status= $session->get_request(%settings);
769 $self->debug("Error while sending a SNMP get-request $ID4:".$session->error()) if (!defined($status));
770
771
772 $self->getsettings(\%settings,$ID5,\&pushstate,$result,$version,'table');
773 $status= $session->get_table(%settings);
774 $self->debug("Error while sending a SNMP get-request $ID5:".$session->error()) if (!defined($status));
775
776 $self->getsettings(\%settings,$ID6,\&pushcost,$result,$version,'table');
777 $status= $session->get_table(%settings);
778 $self->debug("Error while sending a SNMP get-request $ID6:".$session->error()) if (!defined($status));
779  
780
781 $self->getsettings(\%settings,$ID7,\&pushcorrespondence,$correspondence,$version,'table');
782 $status= $session->get_table(%settings);
783 $self->debug("Error while sending a SNMP get-request $ID7:".$session->error()) if (!defined($status));
784
785 ($session2,$error) = Net::SNMP->session( -hostname => $ip,
786 -version => 'snmpv3',
787 -timeout => $self->{TIMEOUT},
788 -username => $self->{USER},
789 -authpassword => $self->{PASS},
790 -nonblocking => 1,
791 -maxmsgsize => 65535);
792
793 die("Error while creating SNMP object:$error\n") if (!defined($session2));
794  
795 $status= $session2->get_table(
796 -baseoid => $ID8,
797 -callback => [\&pushinterfaces,$self,$result],
798 );
799 $self->debug("Error while sending a SNMP get-request $ID8:".$session2->error()) if (!defined($status));
800  
801 $status= $session2->get_table(
802 -baseoid => $ID9,
803 -callback => [\&pushindexes,$self],
804 );
805 $self->debug("Error while sending a SNMP get-request $ID9:".$session2->error()) if (!defined($status));
806
807 $status= $session2->get_table(
808 -baseoid => $ID10,
809 -callback => [\&pushstatus,$self],
810 );
811 $self->debug("Error while sending a SNMP get-request $ID10:".$session2->error()) if (!defined($status));
812
813 $status= $session2->get_table(
814 -baseoid => $ID11,
815 -callback => [\&pushdeviceid,$self,$result],
816 );
817 $self->debug("Error while sending a SNMP get-request $ID11:".$session2->error()) if (!defined($status));
818  
819 $status= $session2->get_table(
820 -baseoid => $ID12,
821 -callback => [\&pushdeviceport,$self,$result],
822 );
823 $self->debug("Error while sending a SNMP get-request $ID12:".$session2->error()) if (!defined($status));
824  
825 $status= $session2->get_table(
826 -baseoid => $ID13,
827 -callback => [\&pushdeviceip,$self,$result,\@ToCheck],
828 );
829 $self->debug("Error while sending a SNMP get-request $ID13:".$session2->error()) if (!defined($status));
830
831
832 $status= $session2->get_table(
833 -baseoid => $ID14,
834 -callback => [\&pushdeviceplatform,$self,$result],
835 );
836 $self->debug("Error while sending a SNMP get-request $ID14:".$session2->error()) if (!defined($status));
837  
838 snmp_dispatcher();
839 $session->close();
840 $session2->close();
841  
842
843
844  
845  
846 for (my $i = 0;$i <= $#ToCheck;$i++)
847 {
848 $ip = $ToCheck[$i][0];
849 $version= grep($ip eq $_,@{$self->{IPV2_CONF}})? 'v2':'v3';
850 $self->getdata($ToCheck[$i][1],$ip,$version);
851 }
852
853 @ToCheck = ();
854 }
855  
856  
857 sub getsettings
858 {
859 my ($self,$settings,$ID,$func,$result,$version,$type) = @_;
860 my $vlan = $self->{VLAN};
861  
862 if ($type eq 'table')
863 {
864 if ($version eq 'v3')
865 {
866 %$settings=(-baseoid => $ID,
867 -contextname => $vlan,
868 -callback => [$func,$self,$result]);
869 }
870 elsif ($version eq 'v2')
871 {
872 %$settings=(-baseoid => $ID,
873 -callback => [$func,$self,$result]);
874 }
875 else
876 {
877 die "Unknown version - $version in func getsettings\n";
878 }
879 }
880 elsif ($type eq 'request')
881 {
882 if ($version eq 'v3')
883 {
884 %$settings=(-varbindlist => [$ID],
885 -contextname => $vlan,
886 -callback => [$func,$self,$result]);
887 }
888 elsif ($version eq 'v2')
889 {
890 %$settings=(-varbindlist => [$ID],
891 -callback => [$func,$self,$result]);
892 }
893 else
894 {
895 die "Unknown version - $version in func getsettings \n";
896 }
897 }
898 else
899 {
900 die "Unknown type operation - $type in func getSettings \n";
901 }
902 }
903  
904  
905  
906 sub pushbridgemac
907 {
908 my ($session,$self,$result) = @_;
909 my $ip = $session->hostname;
910 my $lists = $session->var_bind_list;
911  
912
913 if (!defined($lists))
914 {
915 $self->debug("Error while sending a SNMP get-request for $ID2:\t ".$session->hostname.':'.$session->error());
916 return;
917 }
918
919 $result->{data}{mac} = substr($lists->{$ID3},2);
920  
921 }
922  
923  
924 sub pushbridgepri
925 {
926 my ($session,$self,$result) = @_;
927 my $ip = $session->hostname;
928 my $lists = $session->var_bind_list;
929  
930  
931
932 if (!defined($lists))
933 {
934 $self->debug("Error while sending a SNMP get-request for $ID3:\t ".$session->hostname.':'.$session->error());
935 return;
936 }
937  
938
939 $result->{data}{priority} = $lists->{$ID4};
940 }
941  
942 sub pushstate
943 {
944 my ($session,$self,$result) = @_;
945 my $ip = $session->hostname;
946 my $lists = $session->var_bind_list;
947 my @state;
948 @state[1..6]=('disabled','blocking','listening','learning','forwarding','broken');
949 my ($key,$value);
950
951 if (!defined($lists))
952 {
953 $self->debug("Error while sending a SNMP get-request for $ID5:\t ".$session->hostname.':'.$session->error());
954 return;
955 }
956
957 while (($key,$value) = each %$lists)
958 {
959 $result->{data}{state}{substr($key,24)} = $state[$value];
960 }
961 }
962  
963 sub pushcost
964 {
965 my ($session,$self,$result) = @_;
966 my $ip = $session->hostname;
967 my $lists = $session->var_bind_list;
968 my ($key,$value);
969
970 if (!defined($lists))
971 {
972 $self->debug("Error while sending a SNMP get-request for $ID6:\t ".$session->hostname.':'.$session->error());
973 return;
974 }
975
976 while (($key,$value) = each %$lists)
977 {
978 $result->{data}{cost}{substr($key,24)} = $value;
979 }
980 }
981  
982  
983 sub pushcorrespondence
984 {
985  
986 my ($session,$self,$correspondence) = @_;
987 my $ip = $session->hostname;
988 my $lists = $session->var_bind_list;
989 my ($key,$value);
990  
991
992 if (!defined($lists))
993 {
994 $self->debug("Error while sending a SNMP get-request for $ID7:\t ".$session->hostname.':'.$session->error());
995 return;
996 }
997
998 while (($key,$value) = each %$lists)
999 {
1000 $correspondence->{$ip}{substr($key,23)} = $value;
1001 }
1002 %{$correspondence->{$ip}} = reverse %{$correspondence->{$ip}};
1003 }
1004  
1005  
1006 sub debug
1007 {
1008 my ($self,$str) = @_;
1009  
1010 print STDERR $str."\n" if ($self->{DEBUG});
1011 }
1012  
1013  
1014 sub pushinterfaces
1015 {
1016 my ($session,$self,$result) = @_;
1017 my $ip = $session->hostname;
1018 my $lists = $session->var_bind_list;
1019 my ($key,$value);
1020  
1021 if (!defined($lists))
1022 {
1023 $self->debug("Error while sending a SNMP get-request for $ID8:\t ".$session->hostname.':'.$session->error());
1024 return;
1025 }
1026  
1027 while (($key,$value) = each %$lists)
1028 {
1029 $value =~ s/^(.{2}).*?(\d.*)$/$1$2/;
1030 $result->{data}{interface}{substr($key,20)} = $value;
1031 }
1032 }
1033  
1034  
1035  
1036 sub pushindexes
1037 {
1038 my ($session,$self) = @_;
1039 my $index = $self->{index};
1040 my $ip = $session->hostname;
1041 my $lists = $session->var_bind_list;
1042 my ($key,$value);
1043  
1044  
1045 if (!defined($lists))
1046 {
1047 $self->debug("Error while sending a SNMP get-request for $ID9:$ip\t ".$session->hostname.':'.$session->error());
1048 return;
1049 }
1050  
1051 while (($key,$value) = each %$lists)
1052 {
1053 no warnings;
1054 $index->{$ip}{substr($key,20)}=$value unless($index->{$ip}{substr($key,20)} == -1);
1055 use warnings;
1056 }
1057  
1058  
1059 }
1060  
1061 sub pushstatus
1062 {
1063 my ($session,$self) = @_;
1064 my $index = $self->{index};
1065 my $ip = $session->hostname;
1066 my $lists = $session->var_bind_list;
1067 my ($key,$value);
1068
1069  
1070 if (!defined($lists))
1071 {
1072 $self->debug("Error while sending a SNMP get-request for $ID10:\t ".$session->hostname.':'.$session->error());
1073 return;
1074 }
1075  
1076 while (($key,$value) = each %$lists)
1077 {
1078 $index->{$ip}{substr($key,30)}=-1 if ($value == 2);
1079 }
1080
1081  
1082 }
1083  
1084 sub pushdeviceid
1085 {
1086 my ($session,$self,$result) = @_;
1087 my $ip = $session->hostname;
1088 my $lists = $session->var_bind_list;
1089 my ($key,$val,$id);
1090
1091  
1092 if (!defined($lists))
1093 {
1094 $self->debug("Error while sending a SNMP get-request for $ID11:\t ".$session->hostname.':'.$session->error());
1095 $result->{data}{shape}='ellipse';
1096 return;
1097 }
1098
1099 while (($key,$val) = each %$lists)
1100 {
1101 ($id) = $key =~ /\.(\d+)\.\d+$/;
1102 $result->{devices}{$id}{DeviceID}=$val;
1103 }
1104  
1105 }
1106  
1107  
1108 sub pushdeviceport
1109 {
1110 my ($session,$self,$result) = @_;
1111 my $ip = $session->hostname;
1112 my $lists = $session->var_bind_list;
1113 my ($key,$val,$id);
1114
1115  
1116 if (!defined($lists))
1117 {
1118 $self->debug("Error while sending a SNMP get-request for $ID12:\t ".$session->hostname.':'.$session->error());
1119 return;
1120 }
1121  
1122 while (($key,$val) = each %$lists)
1123 {
1124 ($id) = $key =~ /\.(\d+)\.\d+$/;
1125 $val =~ s/^(.{2}).*?(\d.*)$/$1$2/;
1126 $result->{devices}{$id}{interface}=$val;
1127 }
1128  
1129 }
1130  
1131 sub pushdeviceip
1132 {
1133 my ($session,$self,$result,$ToCheck) = @_;
1134 my ($checked,$aliases) = ($self->{checked},$self->{aliases});
1135 my $ip = $session->hostname;
1136 my $lists = $session->var_bind_list;
1137 my ($key,$val,$id);
1138
1139  
1140 if (!defined($lists))
1141 {
1142 $self->debug("Error while sending a SNMP get-request for $ID13:\t ".$session->hostname.':'.$session->error());
1143 return;
1144 }
1145  
1146 while (($key,$val) = each %$lists)
1147 {
1148 ($id) = $key =~ /\.(\d+)\.\d+$/;
1149  
1150 $val =~ s/0x([0-f]{2})([0-f]{2})([0-f]{2})([0-f]{2})/sprintf("%d.%d.%d.%d",hex($1),hex($2),hex($3),hex($4))/e;
1151 $val = $aliases->{$val} if (defined($aliases->{$val}));
1152  
1153 $result->{devices}{$id}{$val}{data} = undef;
1154 unless (grep($_ eq $val,@$checked))
1155 {
1156 push(@$ToCheck,[$val,$result->{devices}{$id}{$val}]);
1157 push(@$checked,$val);
1158 }
1159 }
1160  
1161 }
1162  
1163  
1164 sub pushdeviceplatform
1165 {
1166 my ($session,$self,$result) = @_;
1167 my $ip = $session->hostname;
1168 my $lists = $session->var_bind_list;
1169 my ($key,$val,$id);
1170
1171  
1172 if (!defined($lists))
1173 {
1174 $self->debug("Error while sending a SNMP get-request for $ID14:\t ".$session->hostname.':'.$session->error());
1175 return;
1176 }
1177  
1178 while (($key,$val) = each %$lists)
1179 {
1180 ($id) = $key =~ /\.(\d+)\.\d+$/;
1181 $result->{devices}{$id}{platform}=$val;
1182 }
1183  
1184 }
1185  
1186 sub output_nodes
1187 {
1188 my ($self,$result,$picture,$root_ip) = @_;
1189 my ($correspondence,$index,$labels,$platforms,$shapes) = ($self->{correspondence},$self->{index},
1190 $self->{labels},
1191 $self->{platforms},$self->{shapes});
1192 my ($val,$ip,$color,$shape,$label,$fillcolor,$fontcolor);
1193  
1194  
1195 ($ip) = grep(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,keys %$result);
1196 $color = $root_ip eq $ip? $self->{root_color}:$self->{color};
1197 $fillcolor = $root_ip eq $ip?$self->{root_fillcolor}:$self->{fillcolor};
1198 $fontcolor = $root_ip eq $ip?$self->{root_fontcolor}:$self->{fontcolor};
1199  
1200  
1201 $labels->{$ip}[0] = $ip;
1202  
1203 foreach $val (keys %{$result->{$ip}{devices}})
1204 {
1205 push(@{$labels->{$ip}},$result->{$ip}{data}{interface}{$val});
1206 $self->output_nodes($result->{$ip}{devices}{$val},$picture,$root_ip);
1207 }
1208  
1209 if (defined($platforms->{$ip}))
1210 {
1211 while (my ($key,$val) = each %$shapes)
1212 {
1213 if ($platforms->{$ip} =~ /$key/)
1214 {
1215 $shape = $val;
1216 last;
1217 }
1218 }
1219 $shape ||= 'box';
1220 }
1221 else
1222 {
1223 $shape = $shapes->{unknown};
1224 }
1225 $label = $#{$labels->{$ip}} == 0? $labels->{$ip}[0]:$labels->{$ip};
1226 $picture->add_node($ip,shape=>$shape,label=>$label,fontcolor=>$fontcolor,color=>$color,fillcolor=>$fillcolor,URL=>$ip);
1227 }
1228 sub nodes_info
1229 {
1230 my $self = shift;
1231
1232 die "No data for info" unless ($self->{GOT});
1233 $self->nodes_info_($self->{result});
1234 return %{$self->{info_node}};
1235 }
1236 sub nodes_info_
1237 {
1238 my ($self,$result) = @_;
1239 my ($info_node,$platforms,$index,$correspondence) = ($self->{info_node},$self->{platforms},$self->{index},
1240 $self->{correspondence});
1241 my $ip;
1242  
1243 ($ip) = grep(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,keys %$result);
1244 $info_node->{$ip}{deviceID} = $result->{DeviceID} if (defined($result->{DeviceID}));
1245 $info_node->{$ip}{priority}= $result->{$ip}{data}{priority} if (defined($result->{$ip}{data}{priority}));
1246 $info_node->{$ip}{mac} = $result->{$ip}{data}{mac} if (defined($result->{$ip}{data}{mac}));
1247 $info_node->{$ip}{platform} = $platforms->{$ip} if (defined($platforms->{$ip}));
1248 if (defined($correspondence->{$ip}))
1249 {
1250 $info_node->{$ip}{interfaces} = {};
1251
1252 foreach my $value (values %{$index->{$ip}})
1253 {
1254 my $key = $correspondence->{$ip}{$value};
1255 next if ($value == -1 or !defined($key));
1256 next unless (defined($result->{$ip}{data}{interface}{$value}));
1257 my $inf_key = $result->{$ip}{data}{interface}{$value};
1258 $info_node->{$ip}{interfaces}{$inf_key}{state} = $result->{$ip}{data}{state}{$key}
1259 if (defined($result->{$ip}{data}{state}{$key}));
1260 $info_node->{$ip}{interfaces}{$inf_key}{cost} = $result->{$ip}{data}{cost}{$key}
1261 if (defined($result->{$ip}{data}{cost}{$key}));
1262 }
1263
1264 }
1265 foreach my $val (keys %{$result->{$ip}{devices}})
1266 {
1267 $self->nodes_info_($result->{$ip}{devices}{$val});
1268 }
1269 }
1270  
1271 sub output_edges
1272 {
1273 my ($self,$result,$root_ip) = @_;
1274 my ($correspondence,$virtual,$info_edge,$draw2) = ($self->{correspondence},$self->{virtual},
1275 $self->{info_edge},$self->{draw2});
1276 my ($interface_from,$from_port,$interface_to,$to_port,$ip,$ip2,$val,$color,$ifvirtualfrom,$ifvirtualto);
1277 my @edges_colors=@{$self->{edges_colors}};
1278  
1279  
1280 $ifvirtualto=$ifvirtualfrom = 0;
1281  
1282 ($ip) = grep(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,keys %$result);
1283
1284 foreach $val (keys %{$result->{$ip}{devices}})
1285 {
1286 ($ip2) = grep(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,keys %{$result->{$ip}{devices}{$val}});
1287 $interface_from = $result->{$ip}{data}{interface}{$val};
1288 $from_port = $self->getport($ip,$interface_from);
1289 $interface_to = $result->{$ip}{devices}{$val}{interface};
1290 $to_port = $self->getport($ip2,$interface_to);
1291 if (defined($correspondence->{$ip}{$val}))
1292 {
1293 my $ind = $correspondence->{$ip}{$val};
1294 if (defined($result->{$ip}{data}{cost}{$ind}))
1295 {
1296 my $cost = $result->{$ip}{data}{cost}{$ind};
1297 my $value = sprintf("%1.0f",log((1/$cost)*100));
1298 $color = $edges_colors[$value];
1299 }
1300 else
1301 {
1302 $color=$edges_colors[0];
1303 }
1304 $color = $edges_colors[$#edges_colors] if ($result->{$ip}{data}{state}{$ind} eq 'blocking');
1305 }
1306 else
1307 {
1308 $color = $edges_colors[0];
1309 }
1310
1311 if (defined($virtual->{$ip}{$val}))
1312 {
1313 my $val_virt = $virtual->{$ip}{$val};
1314 $ifvirtualfrom = $result->{$ip}{data}{interface}{$val_virt};
1315 $ifvirtualto = $self->getvirtualto($self->{result},$root_ip,$interface_to,$ip2);
1316 my $ind_virt = $correspondence->{$ip}{$val_virt};
1317 $color = $edges_colors[$#edges_colors] if ($result->{$ip}{data}{state}{$ind_virt} eq 'blocking');
1318 }
1319
1320 my $n = $#$info_edge+1;
1321
1322 $info_edge->[$n] = {};
1323 $info_edge->[$n]{from_ip} = $ip;
1324 $info_edge->[$n]{from_interface} = $interface_from;
1325 $info_edge->[$n]{from_virtual} = $ifvirtualfrom;
1326 $info_edge->[$n]{to_ip} = $ip2;
1327 $info_edge->[$n]{to_interface} = $interface_to;
1328 $info_edge->[$n]{to_virtual} = $ifvirtualto;
1329
1330
1331 push(@$draw2,"$ip $ip2 $from_port $to_port $color $#$info_edge");
1332
1333
1334
1335 $self->output_edges($result->{$ip}{devices}{$val},$root_ip);
1336
1337 }
1338 }
1339 sub edge_info
1340 {
1341 my ($self,$n) = @_;
1342 die "No data for info" unless ($self->{GOT});
1343 return %{$self->{info_edge}[$n]};
1344 }
1345 sub getvirtualto
1346 {
1347 my ($self,$result,$root_ip,$interface_to,$ip) = @_;
1348 my $virtual = $self->{virtual};
1349 my ($ipt,$val2,$val2_virt,$VirtualTo);
1350 local $_;
1351
1352 foreach (keys %{$result->{$root_ip}{devices}})
1353 {
1354 ($ipt) = grep(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,keys %{$result->{$root_ip}{devices}{$_}});
1355 if ($ip eq $ipt and keys %{$result->{$root_ip}{devices}{$_}{$ipt}{devices}})
1356 {
1357 foreach my $ind (%{$result->{$root_ip}{devices}{$_}{$ipt}{data}{interface}})
1358 {
1359 no warnings;
1360 if ($result->{$root_ip}{devices}{$_}{$ipt}{data}{interface}{$ind} eq $interface_to)
1361 {
1362 $val2_virt= $virtual->{$ip}{$ind};
1363 return $result->{$root_ip}{devices}{$_}{$ipt}{data}{interface}{$val2_virt};
1364 }
1365 use warnings;
1366 }
1367 return undef;
1368 }
1369 $VirtualTo = $self->getvirtualto($result->{$root_ip}{devices}{$_},$ipt,$interface_to,$ip);
1370 return $VirtualTo if (defined($VirtualTo));
1371 }
1372 return undef;
1373 }
1374 sub printedges
1375 {
1376 my ($self,$picture) = @_;
1377 my $draw2 = $self->{draw2};
1378 my @edges_colors = @{$self->{edges_colors}};
1379 my $labels = $self->{labels};
1380 local $_;
1381 my ($ip,$ip2,$port,$port2,$color,$number,$_ip,$_ip2,$_port,$_port2,$_color,$_number,$colorf);
1382  
1383 my $j = 0;
1384  
1385 NEXT:while ($j <= $#$draw2)
1386 {
1387 unless (defined($draw2->[$j]))
1388 {
1389 $j++;
1390 next;
1391 }
1392 ($ip,$ip2,$port,$port2,$color,$number) = split / /,$draw2->[$j];
1393 for (my $i = $j+1;$i <= $#$draw2;$i++)
1394 {
1395 next unless (defined($draw2->[$i]));
1396 ($_ip,$_ip2,$_port,$_port2,$_color,$_number) = split / /,$draw2->[$i];
1397 if (($ip eq $_ip2) and ($ip2 eq $_ip) and ($port eq $_port2) and ($port2 eq $_port))
1398 {
1399 $colorf = $color ge $_color? $color:$_color;
1400 $colorf = $color if ($_color eq $edges_colors[0]);
1401 $colorf = $_color if ($color eq $edges_colors[0]);
1402 my %ports;
1403 $ports{from_port} = $port if ($#{$labels->{$ip}} > 0);
1404 $ports{to_port} = $port2 if ($#{$labels->{$ip2}} > 0);
1405 $picture->add_edge($ip => $ip2,%ports,color=>$colorf,URL=>$number);
1406 delete $draw2->[$i];
1407 delete $draw2->[$j];
1408 $j++;
1409 next NEXT;
1410 }
1411 }
1412 my %ports;
1413 $ports{from_port} = $port if ($#{$labels->{$ip}} > 0);
1414 $ports{to_port} = $port2 if ($#{$labels->{$ip2}} > 0);
1415 $picture->add_edge($ip => $ip2,%ports,color=>$color,URL=>$number);
1416 delete $draw2->[$j];
1417 $j++;
1418 }
1419 }
1420 sub getport
1421 {
1422 my ($self,$ip,$interface) = @_;
1423 my $labels = $self->{labels};
1424 for (my $i = 0; $i <= $#{$labels->{$ip}}; $i++)
1425 {
1426 return $i if ($labels->{$ip}[$i] eq $interface);
1427 }
1428 return 0;
1429 }
1430 1;