Info/Layer3/Netscreen.pm untested

filter: Statementstmt 0.0% 0 / 221 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 87 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 450 CC 15 SCAR 17.4
CC
15
Cov
0%
model
20.0
serial
12.0
ip_index
12.0
CRAP 5.7
1 # SNMP::Info::Layer3::Netscreen
2 #
3 # Copyright (c) 2012 Eric Miller
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the University of California, Santa Cruz nor the
15 # names of its contributors may be used to endorse or promote products
16 # derived from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29  
30 package SNMP::Info::Layer3::Netscreen;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info::Layer3;
36 use SNMP::Info::IEEE802dot11;
37  
38 @SNMP::Info::Layer3::Netscreen::ISA
39 = qw/SNMP::Info::Layer3 SNMP::Info::IEEE802dot11 Exporter/;
40 @SNMP::Info::Layer3::Netscreen::EXPORT_OK = qw//;
41  
42 our ($VERSION, %FUNCS, %GLOBALS, %MIBS, %MUNGE);
43  
44 $VERSION = '3.985003';
45  
46 %MIBS = (
47 %SNMP::Info::Layer3::MIBS,
48 %SNMP::Info::IEEE802dot11::MIBS,
49 'NETSCREEN-SMI' => 'netscreenSetting',
50 'NETSCREEN-PRODUCTS-MIB' => 'netscreenGeneric',
51 'NETSCREEN-INTERFACE-MIB' => 'nsIfIndex',
52 'NETSCREEN-SET-GEN-MIB' => 'nsSetGenSwVer',
53 'NETSCREEN-IP-ARP-MIB' => 'nsIpArpAOD',
54 );
55  
56 %GLOBALS = (
57 %SNMP::Info::Layer3::GLOBALS,
58 %SNMP::Info::IEEE802dot11::GLOBALS,
59 'os_version' => 'nsSetGenSwVer',
60 );
61  
62 %FUNCS = (
63 %SNMP::Info::Layer3::FUNCS,
64 %SNMP::Info::IEEE802dot11::FUNCS,
65  
66 ns_i_index => 'nsIfIndex',
67 ns_i_name => 'nsIfName',
68 ns_i_description => 'nsIfDescr',
69 ns_i_mac => 'nsIfMAC',
70 ns_i_up => 'nsIfStatus',
71 ns_ip_table => 'nsIfIp',
72 ns_ip_netmask => 'nsIfNetmask',
73 bp_index => 'nsIfInfo',
74 std_at_paddr => 'ipNetToMediaPhysAddress',
75 ns_at_paddr => 'nsIpArpMac',
76 );
77  
78 %MUNGE = (
79 %SNMP::Info::Layer3::MUNGE,
80 %SNMP::Info::IEEE802dot11::MUNGE,
81 'ns_i_mac' => \&SNMP::Info::munge_mac,
82 'ns_at_paddr' => \&SNMP::Info::munge_mac,
83 'std_at_paddr' => \&SNMP::Info::munge_mac,
84 );
85  
86 sub layers {
87 return '01001110';
88 }
89  
90 sub vendor {
91 return 'juniper';
92 }
93  
94 sub os {
95 return 'screenos';
96 }
97  
98 sub os_ver {
99 my $netscreen = shift;
100  
101 my $descr = $netscreen->description();
102 if ( $descr =~ m/version (\d\S*) \(SN: / ) {
103 return $1;
104 }
105 return;
106 }
107  
108 sub serial {
109 my $netscreen = shift;
110  
111 my $e_serial = $netscreen->e_serial() || {};
112  
113 my $serial = $e_serial->{1} || undef;
114  
115 return $1 if ( defined $serial and $serial =~ /(\d+)/ );
116 my $descr = $netscreen->description();
117 if ( $descr =~ m/version .*\(SN: (\d\S*),/ ) {
118 return $1;
119 }
120 return;
121 }
122  
123 sub model {
124 my $netscreen = shift;
125  
126 my $id = $netscreen->id();
127  
128 unless ( defined $id ) {
129 print
130 " SNMP::Info::Layer3::model() - Device does not support sysObjectID\n"
131 if $netscreen->debug();
132 return;
133 }
134  
135 my $model = &SNMP::translateObj($id);
136  
137 return $id unless defined $model;
138  
139 $model =~ s/^netscreen//i;
140 return $model;
141 }
142  
143 # provides mapping from IF-MIB to nsIf interfaces - many to 1 (!)
144 # - on WLAN devices wireless0/0(|-[ag]) -> wireless0/0 !!
145 sub _if_nsif_map {
146 my $netscreen = shift;
147 my $i_descr = $netscreen->SUPER::i_description;
148 my $ns_descr = $netscreen->i_description;
149 my %if_nsif_map = ();
150 my @ikeys = sort { $a <=> $b } keys %$i_descr;
151 my @nskeys = sort { $a <=> $b } keys %$ns_descr;
152 my $i = 0;
153 my $n = 0;
154  
155 # assumes descriptions are in the same order from both walks
156 while ( $i < @ikeys && $n < @nskeys ) {
157  
158 # find matching sub interfaces
159 while (
160 $i < @ikeys
161 && substr(
162 $i_descr->{ $ikeys[$i] },
163 0,
164 length $ns_descr->{ $nskeys[$n] }
165 ) eq $ns_descr->{ $nskeys[$n] }
166 )
167 {
168  
169 $if_nsif_map{ $ikeys[$i] } = $nskeys[$n];
170 $i++;
171 }
172  
173 $n++;
174  
175 # skip non-matching interfaces (e.g. tunnel.N)
176 while (
177 $i < @ikeys
178 && substr(
179 $i_descr->{ $ikeys[$i] },
180 0,
181 length $ns_descr->{ $nskeys[$n] }
182 ) ne $ns_descr->{ $nskeys[$n] }
183 && $n < @nskeys
184 )
185 {
186  
187 $if_nsif_map{ $ikeys[$i] } = 0; # no matching interface
188 $i++;
189 }
190 }
191  
192 return \%if_nsif_map;
193 }
194  
195 # Provides mapping from nsIf interfaces to IF-MIB interfaces - many to 1
196 # Example, tunnel.# interfaces are not present in IF-MIB. There exist no
197 # mapping of index IID's between the tables so create mapping based on names
198 sub _nsif_if_map {
199 my $netscreen = shift;
200  
201 my $i_descr = $netscreen->SUPER::i_description;
202 my $ns_descr = $netscreen->i_description;
203 my %rev_i_descr = reverse %$i_descr;
204 my %rev_ns_descr = reverse %$ns_descr;
205  
206 my %nsif_if_map = ();
207 foreach my $value ( values %$ns_descr ) {
208 if ( exists $rev_i_descr{$value} ) {
209 $nsif_if_map{ $rev_ns_descr{$value} } = $rev_i_descr{$value};
210 }
211 else {
212 $nsif_if_map{ $rev_ns_descr{$value} } = 0;
213 }
214 }
215 return \%nsif_if_map;
216 }
217  
218 sub interfaces {
219 my $netscreen = shift;
220 return $netscreen->i_description();
221 }
222  
223 sub i_index {
224 my $netscreen = shift;
225 return $netscreen->ns_i_index();
226 }
227  
228 sub i_name {
229 my $netscreen = shift;
230 return $netscreen->ns_i_name();
231 }
232  
233 sub i_description {
234 my $netscreen = shift;
235  
236 # Versions prior to 5.4 do not support nsIfDescr but do have nsIfName
237 return $netscreen->ns_i_description() || $netscreen->ns_i_name();
238 }
239  
240 sub i_mac {
241 my $netscreen = shift;
242  
243 my $ns_mac = $netscreen->ns_i_mac() || {};
244 my $if_i_mac = $netscreen->SUPER::i_mac() || {};
245 my $ns_i_map = $netscreen->_nsif_if_map();
246  
247 my %i_mac = ();
248 foreach my $iid ( keys %$ns_i_map ) {
249 $i_mac{$iid} = $ns_mac->{$iid} || $if_i_mac->{ $ns_i_map->{$iid} };
250 }
251  
252 return \%i_mac;
253 }
254  
255 sub i_lastchange {
256 my $netscreen = shift;
257  
258 my $if_i_lastchange = $netscreen->SUPER::i_lastchange() || {};
259 my $ns_i_map = $netscreen->_nsif_if_map();
260 my %i_lastchange;
261  
262 foreach my $iid ( keys %$ns_i_map ) {
263 $i_lastchange{$iid} = $if_i_lastchange->{ $ns_i_map->{$iid} };
264 }
265 return \%i_lastchange;
266 }
267  
268 sub i_up {
269 my $netscreen = shift;
270 return $netscreen->ns_i_up();
271 }
272  
273 sub i_up_admin {
274 my $netscreen = shift;
275 my $i_up = $netscreen->i_up();
276 my $i_up_admin = $netscreen->SUPER::i_up_admin();
277 my $ns_i_map = $netscreen->_nsif_if_map();
278 my %i_up_admin;
279  
280 foreach my $iid ( keys %$ns_i_map ) {
281 $i_up_admin{$iid}
282 = $i_up->{$iid} eq "up" && "up"
283 || $i_up_admin->{ $ns_i_map->{$iid} }
284 || 0;
285 }
286 return \%i_up_admin;
287 }
288  
289 sub i_type {
290 my $netscreen = shift;
291  
292 my $if_i_type = $netscreen->SUPER::i_type() || {};
293 my $ns_i_map = $netscreen->_nsif_if_map();
294 my %i_type;
295  
296 foreach my $iid ( keys %$ns_i_map ) {
297 $i_type{$iid} = $if_i_type->{ $ns_i_map->{$iid} } || "tunnel";
298 }
299 return \%i_type;
300 }
301  
302 sub i_mtu {
303 my $netscreen = shift;
304  
305 my $i_type = $netscreen->SUPER::i_mtu() || {};
306 my $ns_i_map = $netscreen->_nsif_if_map();
307 my %i_mtu;
308  
309 foreach my $iid ( keys %$ns_i_map ) {
310 $i_mtu{$iid} = $i_type->{ $ns_i_map->{$iid} };
311 }
312 return \%i_mtu;
313 }
314  
315 sub i_speed {
316 my $netscreen = shift;
317  
318 my $i_speed = $netscreen->SUPER::i_speed();
319 my $i_name = $netscreen->i_name();
320 my $ns_i_map = $netscreen->_nsif_if_map;
321 my %i_speed;
322  
323 foreach my $iid ( keys %$ns_i_map ) {
324 $i_speed{$iid}
325 = $i_speed->{ $ns_i_map->{$iid} }
326 || $i_name->{$iid} =~ /tunnel/ && "vpn"
327 || 0;
328 }
329 return \%i_speed;
330 }
331  
332 sub _mac_map {
333 my $netscreen = shift;
334  
335 my $arp_mac = $netscreen->nsIpArpMac() || {};
336  
337 my %mac_map = ();
338 foreach my $iid ( keys %$arp_mac ) {
339 my $oid = join( ".", ( unpack( "C6", $arp_mac->{$iid} ) ) );
340 $mac_map{$oid} = $iid;
341 }
342 return \%mac_map;
343 }
344  
345 # Interfaces can have two addresses, we want to capture both the network
346 # address and the management address
347 sub ip_index {
348 my $netscreen = shift;
349  
350 my $ns_ip = $netscreen->ns_ip_table() || {};
351 my $if_mng_ip = $netscreen->nsIfMngIp() || {};
352  
353 my %ip_index = ();
354 foreach my $iid ( keys %$ns_ip ) {
355 $ip_index{ $ns_ip->{$iid} } = $iid if $ns_ip->{$iid} ne "0.0.0.0";
356 }
357 foreach my $iid ( keys %$if_mng_ip ) {
358 $ip_index{ $if_mng_ip->{$iid} } = $iid
359 if $if_mng_ip->{$iid} ne "0.0.0.0";
360 }
361 return \%ip_index;
362 }
363  
364 sub ip_table {
365 my $netscreen = shift;
366  
367 my $ip_index = $netscreen->ip_index() || {};
368 my $if_mng_ip = $netscreen->nsIfMngIp() || {};
369  
370 my %ip_table = ();
371 foreach my $iid ( keys %$ip_index ) {
372 my $mgmt_ip = $if_mng_ip->{$iid};
373  
374 if ( defined $mgmt_ip && $mgmt_ip ne '0.0.0.0' ) {
375 $ip_table{$iid} = $mgmt_ip;
376 }
377 else {
378 $ip_table{$iid} = $iid;
379 }
380 }
381 return \%ip_table;
382 }
383  
384 # There is only one netmask for the interface both network and management
385 # addresses should have the same netmask
386 sub ip_netmask {
387 my $netscreen = shift;
388  
389 my $ip_index = $netscreen->ip_index() || {};
390 my $ns_netmask = $netscreen->ns_ip_netmask();
391  
392 my %ip_netmask = ();
393 foreach my $iid ( keys %$ip_index ) {
394 $ip_netmask{$iid} = $ns_netmask->{ $ip_index->{$iid} };
395 }
396 return \%ip_netmask;
397 }
398  
399 sub fw_index {
400 my $netscreen = shift;
401 my %fw_index = ();
402 my $arp_mac = $netscreen->nsIpArpMac() || {};
403  
404 foreach my $iid ( keys %$arp_mac ) {
405 my $oid = join( ".", ( unpack( "C6", $arp_mac->{$iid} ) ) );
406 $fw_index{$iid} = $oid;
407 }
408 return \%fw_index;
409 }
410  
411 sub fw_mac {
412 my $netscreen = shift;
413  
414 my $mac_map = $netscreen->_mac_map() || {};
415  
416 my %fw_mac = ();
417 foreach my $oid ( keys %$mac_map ) {
418 my $mac
419 = join( ":", ( map { sprintf "%lx", $_ } split( /\./, $oid ) ) );
420 $fw_mac{$oid} = $mac;
421 }
422 return \%fw_mac;
423 }
424  
425 sub bp_index {
426 my $netscreen = shift;
427  
428 my $if_info = $netscreen->nsIfInfo() || {};
429  
430 my %bp_index = ();
431 foreach my $iid ( keys %$if_info ) {
432 $bp_index{ $if_info->{$iid} } = $iid;
433 }
434 return \%bp_index;
435 }
436  
437 sub fw_port {
438 my $netscreen = shift;
439  
440 my $fw_index = $netscreen->fw_index();
441 my $arp_if = $netscreen->nsIpArpIfIdx() || {};
442  
443 my %fw_port;
444 foreach my $iid ( keys %$arp_if ) {
445 $fw_port{ $fw_index->{$iid} } = $arp_if->{$iid}
446 if defined $fw_index->{$iid};
447 }
448 return \%fw_port;
449 }
450  
451 # need to remap from IF-MIB index to nsIf index
452 sub i_ssidlist {
453 my $netscreen = shift;
454  
455 my $i_ssidlist = $netscreen->SUPER::i_ssidlist() || {};
456 my $ns_i_map = $netscreen->_if_nsif_map();
457  
458 my %i_ssidlist;
459 foreach my $iid ( keys %$i_ssidlist ) {
460 $i_ssidlist{ $ns_i_map->{$iid} } = $i_ssidlist->{$iid};
461 }
462 return \%i_ssidlist;
463 }
464  
465 sub i_80211channel {
466 my $netscreen = shift;
467  
468 my $i_80211channel = $netscreen->SUPER::i_80211channel() || {};
469 my $ns_i_map = $netscreen->_if_nsif_map();
470  
471 my %i_80211channel;
472 foreach my $iid ( keys %$i_80211channel ) {
473 $i_80211channel{ $ns_i_map->{$iid} } = $i_80211channel->{$iid};
474 }
475 return \%i_80211channel;
476 }
477  
478 sub at_index {
479 my $netscreen = shift;
480  
481 my $std = $netscreen->ipNetToMediaIfIndex();
482 return $std if (ref {} eq ref $std and scalar keys %$std);
483  
484 return $netscreen->nsIpArpIfIdx();
485 }
486  
487 sub at_paddr {
488 my $netscreen = shift;
489  
490 my $std = $netscreen->std_at_paddr();
491 return $std if (ref {} eq ref $std and scalar keys %$std);
492  
493 return $netscreen->ns_at_paddr();
494 }
495  
496 sub at_netaddr {
497 my $netscreen = shift;
498  
499 my $std = $netscreen->ipNetToMediaNetAddress();
500 return $std if (ref {} eq ref $std and scalar keys %$std);
501  
502 return $netscreen->nsIpArpIp();
503 }
504  
505 1;
506  
507 __END__