Info/Layer2.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 159 CC 8 SCAR 20.5
CC
8
Cov
0%
interfaces
20.0
vendor
12.0
serial
12.0
CRAP 7.7
1 # SNMP::Info::Layer2 - SNMP Interface to Layer2 Devices
2 #
3 # Copyright (c) 2008 Max Baker -- All changes from Version 0.7 on
4 #
5 # Copyright (c) 2002,2003 Regents of the University of California
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # * Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of the University of California, Santa Cruz nor the
17 # names of its contributors may be used to endorse or promote products
18 # derived from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31  
32 package SNMP::Info::Layer2;
33  
34 use strict;
35 use warnings;
36 use Exporter;
37 use SNMP::Info;
38 use SNMP::Info::Bridge;
39 use SNMP::Info::Entity;
40 use SNMP::Info::PortAccessEntity;
41 use SNMP::Info::PowerEthernet;
42 use SNMP::Info::LLDP;
43 use SNMP::Info::DocsisHE;
44 use SNMP::Info::IEEE802dot3ad;
45 use SNMP::Info::L2Service;
46  
47 @SNMP::Info::Layer2::ISA = qw/
48 SNMP::Info SNMP::Info::Bridge
49 SNMP::Info::Entity
50 SNMP::Info::PowerEthernet
51 SNMP::Info::LLDP
52 SNMP::Info::DocsisHE
53 SNMP::Info::PortAccessEntity
54 SNMP::Info::IEEE802dot3ad
55 SNMP::Info::L2Service
56 Exporter
57 /;
58  
59 @SNMP::Info::Layer2::EXPORT_OK = qw//;
60  
61 our ($VERSION, %GLOBALS, %MIBS, %FUNCS, %PORTSTAT, %MUNGE);
62  
63 $VERSION = '3.985003';
64  
65 %MIBS = (
66 %SNMP::Info::MIBS, %SNMP::Info::Bridge::MIBS,
67 %SNMP::Info::Entity::MIBS, %SNMP::Info::PowerEthernet::MIBS,
68 %SNMP::Info::LLDP::MIBS, %SNMP::Info::DocsisHE::MIBS,
69 %SNMP::Info::PortAccessEntity::MIBS,
70 %SNMP::Info::IEEE802dot3ad::MIBS,
71 %SNMP::Info::L2Service::MIBS
72 );
73  
74 %GLOBALS = (
75 %SNMP::Info::GLOBALS,
76 %SNMP::Info::Bridge::GLOBALS,
77 %SNMP::Info::DocsisHE::GLOBALS,
78 %SNMP::Info::Entity::GLOBALS,
79 %SNMP::Info::PowerEthernet::GLOBALS,
80 %SNMP::Info::LLDP::GLOBALS,
81 %SNMP::Info::PortAccessEntity::GLOBALS,
82 %SNMP::Info::IEEE802dot3ad::GLOBALS,
83 %SNMP::Info::L2Service::GLOBALS,
84 'serial1' =>
85 '.1.3.6.1.4.1.9.3.6.3.0', # OLD-CISCO-CHASSIS-MIB::chassisId.0
86 );
87  
88 %FUNCS = (
89 %SNMP::Info::FUNCS, %SNMP::Info::Bridge::FUNCS,
90 %SNMP::Info::Entity::FUNCS, %SNMP::Info::PowerEthernet::FUNCS,
91 %SNMP::Info::LLDP::FUNCS, %SNMP::Info::DocsisHE::FUNCS,
92 %SNMP::Info::PortAccessEntity::FUNCS,
93 %SNMP::Info::IEEE802dot3ad::FUNCS,
94 %SNMP::Info::L2Service::FUNCS
95 );
96  
97 %MUNGE = (
98  
99 # Inherit all the built in munging
100 %SNMP::Info::MUNGE,
101 %SNMP::Info::Bridge::MUNGE,
102 %SNMP::Info::DocsisHE::MUNGE,
103 %SNMP::Info::Entity::MUNGE,
104 %SNMP::Info::PowerEthernet::MUNGE,
105 %SNMP::Info::LLDP::MUNGE,
106 %SNMP::Info::PortAccessEntity::MUNGE,
107 %SNMP::Info::IEEE802dot3ad::MUNGE,
108 %SNMP::Info::L2Service::MUNGE,
109 );
110  
111 # Method OverRides
112  
113 # $l2->model() - Looks at sysObjectID which gives the oid of the system
114 # name, contained in a proprietary MIB.
115 sub model {
116 my $l2 = shift;
117 my $id = $l2->id();
118 my $model = &SNMP::translateObj($id) || $id || '';
119  
120 # HP
121 $model =~ s/^hpswitch//i;
122  
123 # Cisco
124 $model =~ s/sysid$//i;
125 $model =~ s/^(cisco|catalyst)//i;
126 $model =~ s/^cat//i;
127  
128 return $model;
129 }
130  
131 sub vendor {
132 my $l2 = shift;
133 my $model = $l2->model() || '';
134 my $descr = $l2->description() || '';
135  
136 if ( $model =~ /hp/i or $descr =~ /\bhp\b/i ) {
137 return 'hp';
138 }
139  
140 if ( $model =~ /catalyst/i or $descr =~ /(catalyst|cisco)/i ) {
141 return 'cisco';
142 }
143  
144 }
145  
146 sub serial {
147 my $l2 = shift;
148  
149 my $entity_serial = $l2->entity_derived_serial();
150 if ( defined $entity_serial and $entity_serial !~ /^\s*$/ ){
151 return $entity_serial;
152 }
153  
154 my $serial1 = $l2->serial1();
155 if ( defined $serial1 and $serial1 !~ /^\s*$/ ) {
156 return $serial1;
157 }
158  
159 return;
160  
161 }
162  
163 sub interfaces {
164 my $l2 = shift;
165 my $partial = shift;
166  
167 my $interfaces = $l2->i_index($partial) || {};
168 my $i_descr = $l2->i_description($partial) || {};
169  
170 # Replace the Index with the ifDescr field.
171 # Check for duplicates in ifDescr, if so uniquely identify by adding
172 # ifIndex to repeated values
173 my (%seen, %first_seen_as);
174 foreach my $iid ( sort keys %$i_descr ) {
175 my $port = $i_descr->{$iid};
176 next unless defined $port;
177  
178 $port = SNMP::Info::munge_null($port);
179 $port =~ s/^\s+//; $port =~ s/\s+$//;
180 next unless length $port;
181  
182 if ( $seen{$port}++ ) {
183 # (#320) also fixup the port this is a duplicate of
184 $interfaces->{ $first_seen_as{$port} }
185 = sprintf( "%s (%d)", $port, $first_seen_as{$port} );
186  
187 $interfaces->{$iid} = sprintf( "%s (%d)", $port, $iid );
188 }
189 else {
190 $interfaces->{$iid} = $port;
191 $first_seen_as{$port} = $iid;
192 }
193 }
194 return $interfaces;
195 }
196  
197 1;
198 __END__