Info/CiscoStack.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 185 CC 14 SCAR 28.4
CC
14
Cov
0%
i_duplex
30.0
i_duplex_admin
30.0
serial
12.0
CRAP 17.1
1 # SNMP::Info::CiscoStack
2 #
3 # Copyright (c) 2008 Max Baker
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::CiscoStack;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35  
36 @SNMP::Info::CiscoStack::ISA = qw/Exporter/;
37 @SNMP::Info::CiscoStack::EXPORT_OK = qw//;
38  
39 our ($VERSION, %MIBS, %FUNCS, %GLOBALS, %MUNGE, %PORTSTAT);
40  
41 $VERSION = '3.985003';
42  
43 %MIBS = ( 'CISCO-STACK-MIB' => 'ciscoStackMIB', );
44  
45 %GLOBALS = (
46 'sysip' => 'sysIpAddr',
47 'netmask' => 'sysNetMask',
48 'broadcast' => 'sysBroadcast',
49 'serial1' => 'chassisSerialNumber',
50 'serial2' => 'chassisSerialNumberString',
51 'model1' => 'chassisModel',
52 'ps1_type' => 'chassisPs1Type',
53 'ps1_status' => 'chassisPs1Status',
54 'ps2_type' => 'chassisPs2Type',
55 'ps2_status' => 'chassisPs2Status',
56 'slots' => 'chassisNumSlots',
57 'fan' => 'chassisFanStatus',
58 );
59  
60 %FUNCS = (
61  
62 # CISCO-STACK-MIB::moduleEntry
63 # These are blades in a catalyst device
64 'm_type' => 'moduleType',
65 'm_model' => 'moduleModel',
66 'm_serial' => 'moduleSerialNumber',
67 'm_serial_string' => 'moduleSerialNumberString',
68 'm_status' => 'moduleStatus',
69 'm_name' => 'moduleName',
70 'm_ports' => 'moduleNumPorts',
71 'm_ports_status' => 'modulePortStatus',
72 'm_hwver' => 'moduleHwVersion',
73 'm_fwver' => 'moduleFwVersion',
74 'm_swver' => 'moduleSwVersion',
75  
76 # Router Blades :
77 'm_ip' => 'moduleIPAddress',
78 'm_sub1' => 'moduleSubType',
79 'm_sub2' => 'moduleSubType2',
80  
81 # CISCO-STACK-MIB::portEntry
82 'p_name' => 'portName',
83 'p_type' => 'portType',
84 'p_status' => 'portOperStatus',
85 'p_status2' => 'portAdditionalStatus',
86 'p_speed' => 'portAdminSpeed',
87 'p_duplex' => 'portDuplex',
88 'p_port' => 'portIfIndex',
89 'p_rx_flow_control' => 'portOperRxFlowControl',
90 'p_tx_flow_control' => 'portOperTxFlowControl',
91 'p_rx_flow_control_admin' => 'portAdminRxFlowControl',
92 'p_tx_flow_control_admin' => 'portAdminTxFlowControl',
93 'p_oidx' => 'portCrossIndex',
94  
95 # CISCO-STACK-MIB::PortCpbEntry
96 'p_speed_admin' => 'portCpbSpeed',
97 'p_duplex_admin' => 'portCpbDuplex',
98 );
99  
100 %MUNGE = (
101 'm_ports_status' => \&munge_port_status,
102 'p_duplex_admin' => \&SNMP::Info::munge_bits,
103 );
104  
105 %PORTSTAT = (
106 1 => 'other',
107 2 => 'ok',
108 3 => 'minorFault',
109 4 => 'majorFault',
110 );
111  
112 # Changes binary byte describing each port into ascii, and returns
113 # an ascii list separated by spaces.
114 sub munge_port_status {
115 my $status = shift;
116 my @vals = map( $PORTSTAT{$_}, unpack( 'C*', $status ) );
117 return join( ' ', @vals );
118 }
119  
120 sub serial {
121 my $stack = shift;
122 my $serial1 = $stack->serial1();
123 my $serial2 = $stack->serial2();
124  
125 return $serial1 if defined $serial1;
126 return $serial2 if defined $serial2;
127 return;
128 }
129  
130 # Rules for older CatOS devices using CiscoStack
131 #
132 # You can configure Ethernet and Fast Ethernet interfaces to either full
133 # duplex or half duplex.
134 #
135 # You cannot configure the duplex mode on Gigabit Ethernet ports (they are
136 # always in full-duplex mode).
137 #
138 # If you set the port speed to auto, duplex mode is automatically set to auto.
139 #
140 # For operational duplex if portCpbDuplex is all zeros the port is a gigabit
141 # port and duplex is always full. If the port is not operational and auto
142 # return value will be undef since we don't know the operational status.
143 #
144 # Newer devices use ETHERLIKE-MIB to report operational duplex, this will be
145 # checked in the device class.
146  
147 sub i_duplex {
148 my $stack = shift;
149 my $partial = shift;
150  
151 my $p_port = $stack->p_port() || {};
152 my $p_duplex = $stack->p_duplex() || {};
153 my $p_duplex_cap = $stack->p_duplex_admin() || {};
154  
155 my $i_duplex = {};
156 foreach my $port ( keys %$p_duplex ) {
157 my $iid = $p_port->{$port};
158 next unless defined $iid;
159 next if ( defined $partial and $iid !~ /^$partial$/ );
160  
161 # Test for gigabit
162 if ( defined $p_duplex_cap->{$port} and $p_duplex_cap->{$port} == 0 ) {
163 $i_duplex->{$iid} = 'full';
164 }
165  
166 # Auto is not a valid operational state
167 elsif ( $p_duplex->{$port} eq 'auto' ) {
168 next;
169 }
170 else {
171 $i_duplex->{$iid} = $p_duplex->{$port};
172 }
173 }
174 return $i_duplex;
175 }
176  
177 # For administrative duplex if portCpbDuplex is all zeros the port is a gigabit
178 # port and duplex is always full. If portAdminSpeed is set to auto then the
179 # duplex will be auto, otherwise use portDuplex.
180  
181 sub i_duplex_admin {
182 my $stack = shift;
183 my $partial = shift;
184  
185 my $p_port = $stack->p_port() || {};
186 my $p_duplex = $stack->p_duplex() || {};
187 my $p_duplex_cap = $stack->p_duplex_admin() || {};
188 my $p_speed = $stack->p_speed() || {};
189  
190 my $i_duplex_admin = {};
191 foreach my $port ( keys %$p_duplex ) {
192 my $iid = $p_port->{$port};
193 next unless defined $iid;
194 next if ( defined $partial and $iid !~ /^$partial$/ );
195  
196 # Test for gigabit
197 if ( defined $p_duplex_cap->{$port} and $p_duplex_cap->{$port} == 0 ) {
198 $i_duplex_admin->{$iid} = 'full';
199 }
200  
201 # Check admin speed for auto
202 elsif ( $p_speed->{$port} =~ /auto/ ) {
203 $i_duplex_admin->{$iid} = 'auto';
204 }
205 else {
206 $i_duplex_admin->{$iid} = $p_duplex->{$port};
207 }
208 }
209 return $i_duplex_admin;
210 }
211  
212 sub i_speed_admin {
213 my $stack = shift;
214 my $partial = shift;
215  
216 my %i_speed_admin;
217 my $p_port = $stack->p_port() || {};
218 my $p_speed = $stack->p_speed() || {};
219  
220 my %speeds = (
221 'autoDetect' => 'auto',
222 'autoDetect10100' => 'auto',
223 's10000000' => '10 Mbps',
224 's100000000' => '100 Mbps',
225 's1000000000' => '1.0 Gbps',
226 's10G' => '10 Gbps',
227 );
228  
229 %i_speed_admin
230 = map { $p_port->{$_} => $speeds{ $p_speed->{$_} } } keys %$p_port;
231  
232 return \%i_speed_admin;
233 }
234  
235 sub set_i_speed_admin {
236  
237 # map speeds to those the switch will understand
238 my %speeds = qw/auto 1 10 10000000 100 100000000 1000 1000000000/;
239  
240 my $stack = shift;
241 my ( $speed, $iid ) = @_;
242 my $p_port = $stack->p_port() || {};
243 my %reverse_p_port = reverse %$p_port;
244  
245 $speed = lc($speed);
246  
247 return 0 unless defined $speeds{$speed};
248  
249 $iid = $reverse_p_port{$iid};
250  
251 return $stack->set_p_speed( $speeds{$speed}, $iid );
252 }
253  
254 sub set_i_duplex_admin {
255  
256 # map a textual duplex to an integer one the switch understands
257 my %duplexes = qw/half 1 full 2 auto 4/;
258  
259 my $stack = shift;
260 my ( $duplex, $iid ) = @_;
261 if ( $duplex eq 'auto' ) {
262 $stack->error_throw(
263 "Software doesn't support setting auto duplex with
264 set_i_duplex_admin() you must use
265 set_i_speed_admin() and set both speed and duplex
266 to auto"
267 );
268 return 0;
269 }
270  
271 my $p_port = $stack->p_port() || {};
272 my %reverse_p_port = reverse %$p_port;
273  
274 $duplex = lc($duplex);
275  
276 return 0 unless defined $duplexes{$duplex};
277  
278 $iid = $reverse_p_port{$iid};
279  
280 return $stack->set_p_duplex( $duplexes{$duplex}, $iid );
281 }
282  
283 1;
284  
285 __END__