Info/Layer3/IBMGbTor.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 269 CC 21 SCAR 23.7
CC
21
Cov
0%
lldp_ip
30.0
lldp_if
30.0
hasLLDP
12.0
CRAP 10.7
1 # SNMP::Info::Layer3::IBMGbTor - SNMP Interface to IBM Rackswitch devices
2 #
3 # Copyright (c) 2013 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::IBMGbTor;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info::Layer3;
36  
37 @SNMP::Info::Layer3::IBMGbTor::ISA
38 = qw/SNMP::Info::LLDP SNMP::Info::Layer3 Exporter/;
39 @SNMP::Info::Layer3::IBMGbTor::EXPORT_OK = qw//;
40  
41 our ($VERSION, %GLOBALS, %FUNCS, %MIBS, %MUNGE);
42  
43 $VERSION = '3.985003';
44  
45 %MIBS = (
46 %SNMP::Info::Layer3::MIBS,
47  
48 # Standard LLDP-MIB comes from Layer3; this table is the fallback
49 'IBM-GbTOR-10G-L2L3-MIB' => 'lldpInfoRemoteDevicesLocalPort',
50 );
51  
52 %GLOBALS = (
53 %SNMP::Info::Layer3::GLOBALS,
54 %SNMP::Info::LLDP::GLOBALS,
55 'temp' => 'hwTempSensors',
56 'fan' => 'hwFanSpeed',
57  
58 # Can't find the equivalent in IBM-GbTOR-10G-L2L3-MIB
59 # use a different strategy for lldp_sys_cap in hasLLDP()
60 #'lldp_sysname' => 'lldpLocSysName',
61 #'lldp_sysdesc' => 'lldpLocSysDesc',
62 #'lldp_sys_cap' => 'lldpLocSysCapEnabled',
63 );
64  
65 %FUNCS = (
66 %SNMP::Info::Layer3::FUNCS,
67 %SNMP::Info::LLDP::FUNCS,
68  
69 # IBM-GbTOR-10G-L2L3-MIB::portInfoTable
70 'sw_duplex' => 'portInfoMode',
71  
72 # Can't find the equivalent in IBM-GbTOR-10G-L2L3-MIB
73 # not currently used in LLDP class
74 #'lldp_lman_addr' => 'lldpLocManAddrIfId',
75  
76 # IBM-GbTOR-10G-L2L3-MIB::lldpInfoPortTable
77 'lldp_port_status' => 'lldpInfoPortAdminStatus',
78  
79 # IBM-GbTOR-10G-L2L3-MIB::lldpInfoRemoteDevicesTable
80 'ibm_lldp_rem_id_type' => 'lldpInfoRemoteDevicesChassisSubtype',
81 'ibm_lldp_rem_id' => 'lldpInfoRemoteDevicesSystemName',
82 'ibm_lldp_rem_pid_type' => 'lldpInfoRemoteDevicesPortSubtype',
83 'ibm_lldp_rem_pid' => 'lldpInfoRemoteDevicesPortId',
84 'ibm_lldp_rem_desc' => 'lldpInfoRemoteDevicesPortDescription',
85 'ibm_lldp_rem_sysname' => 'lldpInfoRemoteDevicesSystemName',
86 'ibm_lldp_rem_sysdesc' => 'lldpInfoRemoteDevicesSystemDescription',
87 'ibm_lldp_rem_sys_cap' => 'lldpInfoRemoteDevicesSystemCapEnabled',
88  
89 # IBM-GbTOR-10G-L2L3-MIB::lldpInfoRemoteDevicesManAddrTable
90 'lldp_rman_type' => 'lldpInfoRemoteDevicesManAddrSubtype',
91 'ibm_lldp_rman_addr' => 'lldpInfoRemoteDevicesManAddr',
92 );
93  
94 %MUNGE = (
95 %SNMP::Info::Layer3::MUNGE,
96 %SNMP::Info::LLDP::MUNGE,
97 'ibm_lldp_rem_sysname' => \&SNMP::Info::munge_null,
98 'ibm_lldp_rem_sysdesc' => \&SNMP::Info::munge_null,
99 'ibm_lldp_rem_sys_cap' => \&SNMP::Info::munge_bits,
100 );
101  
102 sub hasLLDP {
103 my $ibm = shift;
104  
105 return 1 if $ibm->SUPER::hasLLDP();
106  
107 # We may be have LLDP, but nothing in lldpRemoteSystemsData Tables
108 # Look to see if LLDP Rx enabled on any port
109 my $lldp_cap = $ibm->lldp_port_status() || {};
110  
111 foreach my $if ( keys %$lldp_cap ) {
112 if ( $lldp_cap->{$if} =~ /enabledRx/i ) {
113 return 1;
114 }
115 }
116 return;
117 }
118  
119 # Decided once per device so neighbor keys never mix. Only the G8124 serves
120 # the IBM-GbTOR-10G-L2L3-MIB neighbor table, so LLDP-MIB rows take precedence.
121 # Memoized because an empty walk is not cached; clear_cache resets it.
122 sub _ibm_lldp_standard {
123 my $ibm = shift;
124  
125 return $ibm->{_ibm_lldp_standard} if exists $ibm->{_ibm_lldp_standard};
126 my $rem_id = $ibm->SUPER::lldp_rem_id() || {};
127 return $ibm->{_ibm_lldp_standard} = scalar keys %$rem_id ? 1 : 0;
128 }
129  
130 sub _ibm_lldp_column {
131 my ( $ibm, $column, $partial ) = @_;
132  
133 my $method = $ibm->_ibm_lldp_standard ? "SUPER::$column" : "ibm_$column";
134 return $ibm->$method($partial);
135 }
136  
137 sub lldp_rem_id_type {
138 my ( $ibm, $partial ) = @_;
139 return $ibm->_ibm_lldp_column( 'lldp_rem_id_type', $partial );
140 }
141  
142 sub lldp_rem_id {
143 my ( $ibm, $partial ) = @_;
144 return $ibm->_ibm_lldp_column( 'lldp_rem_id', $partial );
145 }
146  
147 sub lldp_rem_pid_type {
148 my ( $ibm, $partial ) = @_;
149 return $ibm->_ibm_lldp_column( 'lldp_rem_pid_type', $partial );
150 }
151  
152 sub lldp_rem_pid {
153 my ( $ibm, $partial ) = @_;
154 return $ibm->_ibm_lldp_column( 'lldp_rem_pid', $partial );
155 }
156  
157 sub lldp_rem_desc {
158 my ( $ibm, $partial ) = @_;
159 return $ibm->_ibm_lldp_column( 'lldp_rem_desc', $partial );
160 }
161  
162 sub lldp_rem_sysname {
163 my ( $ibm, $partial ) = @_;
164 return $ibm->_ibm_lldp_column( 'lldp_rem_sysname', $partial );
165 }
166  
167 sub lldp_rem_sysdesc {
168 my ( $ibm, $partial ) = @_;
169 return $ibm->_ibm_lldp_column( 'lldp_rem_sysdesc', $partial );
170 }
171  
172 sub lldp_rem_sys_cap {
173 my ( $ibm, $partial ) = @_;
174 return $ibm->_ibm_lldp_column( 'lldp_rem_sys_cap', $partial );
175 }
176  
177 sub lldp_rman_addr {
178 my ( $ibm, $partial ) = @_;
179 return $ibm->_ibm_lldp_column( 'lldp_rman_addr', $partial );
180 }
181  
182 sub lldp_ip {
183 my $ibm = shift;
184 my $partial = shift;
185  
186 return $ibm->SUPER::lldp_ip($partial) if $ibm->_ibm_lldp_standard;
187  
188 my $rman_type = $ibm->lldp_rman_type($partial) || {};
189 my $rman_addr = $ibm->lldp_rman_addr($partial) || {};
190  
191 my %lldp_ip;
192 foreach my $key ( keys %$rman_addr ) {
193 my $type = $rman_type->{$key};
194 next unless defined $type;
195 next unless $type eq 'ipV4';
196 if ( $key =~ /^(\d+)\./ ) {
197 $lldp_ip{$1} = $rman_addr->{$key};
198 }
199 }
200 return \%lldp_ip;
201 }
202  
203 sub lldp_addrs {
204 my $ibm = shift;
205 my $partial = shift;
206  
207 return $ibm->SUPER::lldp_addrs($partial) if $ibm->_ibm_lldp_standard;
208 return $ibm->_lldp_addrs_from_families($partial);
209 }
210  
211 # The LLDP-MIB local port number is the bridge port, and lldpLocPortDesc
212 # holds the port alias (XGE3), which matches neither ifDescr nor ifAlias.
213 sub _ibm_lldp_std_if {
214 my ( $ibm, $partial ) = @_;
215  
216 my $inherited = $ibm->SUPER::lldp_if($partial) || {};
217 my $bp_index = $ibm->bp_index() || {};
218  
219 my %lldp_if = %$inherited;
220 foreach my $key ( keys %lldp_if ) {
221 my $local_port = ( split /\./, $key )[1];
222 $lldp_if{$key} = $bp_index->{$local_port}
223 if defined $local_port and exists $bp_index->{$local_port};
224 }
225 return \%lldp_if;
226 }
227  
228 sub lldp_if {
229 my $lldp = shift;
230 my $partial = shift;
231  
232 return $lldp->_ibm_lldp_std_if($partial) if $lldp->_ibm_lldp_standard;
233  
234 my $lldp_desc = $lldp->lldpInfoRemoteDevicesLocalPort($partial) || {};
235 my $i_descr = $lldp->i_description() || {};
236 my $i_alias = $lldp->i_alias() || {};
237 my %r_i_descr = reverse %$i_descr;
238 my %r_i_alias = reverse %$i_alias;
239  
240 my %lldp_if;
241 foreach my $key ( keys %$lldp_desc ) {
242  
243 # Cross reference lldpLocPortDesc with ifDescr and ifAlias to get ifIndex,
244 # prefer ifAlias over ifDescr since MIB says 'alias'.
245 my $desc = $lldp_desc->{$key};
246 next unless $desc;
247 my $port = $desc;
248  
249 # If cross reference is successful use it, otherwise stick with
250 # lldpRemLocalPortNum
251 if ( exists $r_i_alias{$desc} ) {
252 $port = $r_i_alias{$desc};
253 }
254 elsif ( exists $r_i_descr{$desc} ) {
255 $port = $r_i_descr{$desc};
256 }
257  
258 $lldp_if{$key} = $port;
259 }
260 return \%lldp_if;
261 }
262  
263 sub lldp_platform {
264 my $ibm = shift;
265 my $partial = shift;
266  
267 return $ibm->SUPER::lldp_platform($partial) if $ibm->_ibm_lldp_standard;
268 return $ibm->lldpInfoRemoteDevicesSystemDescription($partial);
269 }
270  
271 sub i_ignore {
272 my $ibm = shift;
273 my $partial = shift;
274  
275 my $interfaces = $ibm->interfaces($partial) || {};
276  
277 my %i_ignore;
278 foreach my $if ( keys %$interfaces ) {
279 if ( $interfaces->{$if} =~ /(tunnel|loopback|\blo\b|lb|null)/i ) {
280 $i_ignore{$if}++;
281 }
282 }
283 return \%i_ignore;
284 }
285  
286 sub i_duplex {
287 my $ibm = shift;
288 my $partial = shift;
289  
290 return $ibm->sw_duplex($partial);
291 }
292  
293 sub model {
294 my $ibm = shift;
295 my $id = $ibm->id();
296 my $descr = $ibm->description();
297 my $model = &SNMP::translateObj($id);
298  
299 if ( $descr =~ /RackSwitch\s(.*)/ ) {
300 return $1;
301 }
302  
303 return $model || $id;
304 }
305  
306 sub os {
307 return 'ibm';
308 }
309  
310 # TODO: from os version 8 this should return lenovo
311 sub vendor {
312 return 'ibm';
313 }
314  
315 sub os_ver {
316 my $ibm = shift;
317  
318 return $ibm->agSoftwareVersion();
319 }
320  
321 sub interfaces {
322 my $ibm = shift;
323 my $partial = shift;
324  
325 my $i_descr = $ibm->i_description($partial) || {};
326 my $i_name = $ibm->i_name($partial) || {};
327  
328 foreach my $iid ( keys %$i_name ) {
329 my $name = $i_name->{$iid};
330 next unless defined $name;
331 $i_descr->{$iid} = $name
332 if $name =~ /^port\d+/i;
333 }
334  
335 return $i_descr;
336 }
337  
338 1;
339 __END__