Info/Layer3/Lenovo.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 106 CC 6 SCAR 24.7
CC
6
Cov
0%
agg_ports_cnos
30.0
i_speed_raw
6.0
i_speed
2.0
CRAP 11.9
1 # SNMP::Info::Layer3::Lenovo
2 #
3 # Copyright (c) 2019 nick nauwelaerts
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 # TODO
31 # fallback to super::i_speed needed?
32 # lag members (no ez way to map master<->slaves)
33 # psu & fan info should be possible
34 # spanning tree info is avail too
35 # modules list could use more work
36  
37 package SNMP::Info::Layer3::Lenovo;
38  
39 use strict;
40 use warnings;
41 use Exporter;
42 use SNMP::Info::Layer3;
43 use SNMP::Info::IEEE802dot3ad;
44  
45 @SNMP::Info::Layer3::Lenovo::ISA = qw/
46 SNMP::Info::Layer3
47 SNMP::Info::IEEE802dot3ad
48 Exporter
49 /;
50 @SNMP::Info::Layer3::Lenovo::EXPORT_OK = qw//;
51  
52 our ($VERSION, %GLOBALS, %MIBS, %FUNCS, %MUNGE);
53  
54 $VERSION = '3.985003';
55  
56 %MIBS = (
57 %SNMP::Info::Layer3::MIBS,
58 %SNMP::Info::IEEE802dot3ad::MIBS,
59 'LENOVO-ENV-MIB' => 'lenovoEnvMibPowerSupplyIndex',
60 'LENOVO-PRODUCTS-MIB' => 'tor',
61 );
62  
63 %GLOBALS = (
64 %SNMP::Info::Layer3::GLOBALS,
65 # no way to get os version and other device details
66 # ENTITY-MIB however can help out
67 'os_ver' => 'entPhysicalSoftwareRev.1',
68 'mac' => 'dot1dBaseBridgeAddress',
69 );
70  
71 %FUNCS = (
72 %SNMP::Info::Layer3::FUNCS,
73 %SNMP::Info::IEEE802dot3ad::FUNCS,
74 # perhaps we should honor what the device returns, but it's just
75 # the opposite of what most other's do, so overwrite
76 'i_name' => 'ifDescr',
77 'i_description' => 'ifName',
78 );
79  
80 %MUNGE = (
81 %SNMP::Info::Layer3::MUNGE,
82 %SNMP::Info::IEEE802dot3ad::MUNGE,
83 );
84  
85 # lenovo does not set ifSpeed to 4294967295 for highspeed links, instead
86 # it substracts 4294967296 from the value until the remainder fits, so
87 # 10gbit interfaces are presented as:
88 # 10000000000 - 4294967296 - 4294967296 = 1410065408
89 # so just always return if_speed_high
90 #
91 # forcing the use of ifhighspeed would be preferred but not possible atm
92 # so copy both functions from Info.pm & overwrite
93  
94 # is there any way to just overwrite the whole function?
95 # (overwrite i_speed with i_speed_high). would be more elegant.
96 sub i_speed {
97 my $cnos = shift;
98 my $partial = shift;
99  
100 return $cnos->orig_i_speed_high($partial);
101 }
102  
103 # also need to overwrite i_speed_raw, netdisco uses this in some
104 # instances
105 sub i_speed_raw {
106 my $info = shift;
107 my $partial = shift;
108  
109 # remove the speed formating
110 my $munge_i_speed = delete $info->{munge}{i_speed};
111 # also for highspeed interfaces e.g. TenGigabitEthernet
112 my $munge_i_speed_high = delete $info->{munge}{i_speed_high};
113  
114 my $i_speed_raw = $info->orig_i_speed($partial);
115 # my $i_speed_high = undef;
116  
117 # just overwrite if interface speed is over 2.5gbps
118 foreach my $i ( keys %$i_speed_raw ) {
119 my $i_speed_high = undef;
120  
121 $i_speed_high = $info->i_speed_high($partial);
122 if (defined($i_speed_high) and ($i_speed_high->{$i} > 2500)) {
123 $i_speed_raw->{$i} = ( $i_speed_high->{$i} * 1_000_000 );
124 }
125 }
126  
127 # restore the speed formating
128 $info->{munge}{i_speed} = $munge_i_speed;
129 $info->{munge}{i_speed_high} = $munge_i_speed_high;
130  
131 return $i_speed_raw;
132 }
133  
134 sub vendor {
135 return 'lenovo';
136 }
137  
138 sub os {
139 return 'cnos';
140 }
141  
142 # work in progress, there seems to be no standardized way to map
143 # lag members to the master.
144 sub agg_ports_cnos {
145 my $dev = shift;
146  
147 # TODO: implement partial
148 my $ports = $dev->ad_lag_ports();
149 my $index = $dev->bp_index() || {};
150  
151 return {} unless ref {} eq ref $ports and scalar keys %$ports;
152  
153 my $ret = {};
154 foreach my $m ( keys %$ports ) {
155 print "m $m\n";
156 my $idx = $m;
157 my $portlist = $ports->{$m};
158 printf "p %d\n", scalar(@$portlist);
159  
160 next unless $portlist;
161  
162 # While dot3adAggTable is indexed by ifIndex, the portlist is indexed
163 # with a dot1dBasePort, so we need to use dot1dBasePortIfIndex to map to
164 # the ifIndex. If we don't have dot1dBasePortIfIndex assume
165 # dot1dBasePort = ifIndex
166 for ( my $i = 0; $i <= scalar(@$portlist); $i++ ) {
167 my $ifindex = $i+1;
168 if ( exists($index->{$i+1}) and defined($index->{$i+1}) ) {
169 $ifindex = $index->{$i+1};
170 print "ifi $ifindex\n";
171 }
172 $ret->{$ifindex} = $idx if ( @$portlist[$i] );
173 }
174 }
175  
176 return $ret;
177 }
178  
179 #sub agg_ports { return agg_ports_cnos(@_) }
180  
181 1;
182  
183 __END__