Info/CiscoAgg.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 146 CC 9 SCAR 24.2
CC
9
Cov
0%
agg_ports_propvirtual
30.0
munge_port_ifindex
12.0
agg_ports_pagp
6.0
CRAP 11.3
1 # SNMP::Info::CiscoAgg
2 #
3 # Copyright (c) 2019 SNMP::Info Developers
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::CiscoAgg;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info::IEEE802dot3ad;
36 use SNMP::Info::Aggregate 'agg_ports_ifstack';
37  
38 @SNMP::Info::CiscoAgg::ISA = qw/
39 SNMP::Info::IEEE802dot3ad
40 Exporter
41 /;
42  
43 @SNMP::Info::CiscoAgg::EXPORT_OK = qw/
44 agg_ports
45 agg_ports_cisco
46 agg_ports_lag
47 agg_ports_pagp
48 /;
49  
50 our ($DEBUG, $VERSION, %MIBS, %FUNCS, %GLOBALS, %MUNGE);
51  
52 $VERSION = '3.985003';
53  
54 %MIBS = (
55 %SNMP::Info::IEEE802dot3ad::MIBS,
56 'CISCO-PAGP-MIB' => 'pagpGroupIfIndex',
57 'CISCO-LAG-MIB' => 'clagAggPortListPorts',
58 'CISCO-IF-EXTENSION-MIB' => 'cieIfLastInTime',
59 );
60  
61 %GLOBALS = ();
62  
63 %FUNCS = (
64 %SNMP::Info::IEEE802dot3ad::FUNCS,
65 'lag_ports' => 'clagAggPortListPorts',
66 'lag_members' => 'clagAggPortListInterfaceIndexList',
67 );
68  
69 %MUNGE = (
70 %SNMP::Info::IEEE802dot3ad::MUNGE,
71 'lag_ports' => \&SNMP::Info::munge_port_list,
72 'lag_members' => \&munge_port_ifindex,
73 );
74  
75 sub munge_port_ifindex {
76 my $plist = shift;
77 return unless defined $plist;
78 return unless length $plist;
79  
80 my $list = [ map {sprintf "%d", hex($_)} unpack( "(A8)*", join( '' , map { sprintf "%02x", $_} unpack( "(C4)*", $plist ) )) ];
81  
82 return $list;
83 }
84  
85 sub agg_ports_cisco {
86 my $dev = shift;
87 my $group = $dev->lag_members;
88  
89 my $mapping = {};
90 for my $master (keys %$group) {
91 my $slaves = $group->{$master};
92 for my $slave (@$slaves) {
93 $mapping->{$slave} = $master;
94 }
95 }
96  
97 return $mapping;
98 }
99  
100 sub agg_ports_pagp {
101 my $dev = shift;
102  
103 # Note that this mapping will miss any interfaces that are down during
104 # polling. If one of the members is up, we could use
105 # pagpAdminGroupCapability to figure things out, but if they're all
106 # down, we're hosed. Since we could be hosed anyway, we skip the fancy
107 # stuff.
108 my $mapping = {};
109 my $group = $dev->pagpGroupIfIndex;
110 for my $slave (keys %$group) {
111 my $master = $group->{$slave};
112 next if($master == 0 || $slave == $master);
113  
114 $mapping->{$slave} = $master;
115 }
116  
117 return $mapping;
118 }
119  
120 sub agg_ports_lag {
121 my $dev = shift;
122  
123 # same note as for agg_ports_pagp, it will miss mappings if interfaces
124 # are down or lacp is not synced.
125  
126 my $mapping = {};
127 my $group = $dev->dot3adAggPortSelectedAggID;
128 for my $slave (keys %$group) {
129 my $master = $group->{$slave};
130 next if($master == 0 || $slave == $master);
131  
132 $mapping->{$slave} = $master;
133 }
134  
135 return $mapping;
136 }
137  
138 # version of SNMP::Info::Aggregate which uses propVirtual
139 # because CISCO-PAGP-MIB says "The ifType of an agport is propVirtual"
140 # but we need to filter out the VLANs and stuff so check against
141 # CISCO-PAGP-MIB::pagpEthcOperationMode
142 sub agg_ports_propvirtual {
143 my $dev = shift;
144 my $partial = shift;
145  
146 my $ifStack = $dev->ifStackStatus();
147 my $pagpOperMode = $dev->pagpEthcOperationMode() || {};
148 # TODO: if we want to do partial, we need to use inverse status
149 my $ifType = $dev->ifType();
150  
151 my $ret = {};
152  
153 foreach my $idx ( keys %$ifStack ) {
154 my ( $higher, $lower ) = split /\./, $idx;
155 next if ( $higher == 0 or $lower == 0 );
156 next unless exists $ifType->{ $higher } and defined $ifType->{ $higher };
157  
158 if ( $ifType->{ $higher } eq 'propVirtual' ) {
159  
160 # lower needs also to be configured in pagpEthcOperationMode
161 next unless exists $pagpOperMode->{$lower}
162 and defined $pagpOperMode->{$lower}
163 and $pagpOperMode->{$lower} =~ m/^(?:manual|pagpOn)$/;
164  
165 $ret->{ $lower } = $higher;
166 }
167 }
168  
169 return $ret;
170 }
171  
172  
173 # combine PAgP, LAG & Cisco proprietary data
174 sub agg_ports {
175 my $ret = {
176 %{agg_ports_propvirtual(@_)},
177 %{agg_ports_ifstack(@_)},
178 %{agg_ports_pagp(@_)},
179 %{agg_ports_lag(@_)},
180 %{agg_ports_cisco(@_)},
181 };
182 return $ret;
183 }
184  
185 sub _agg_ports_walked {
186 return qw/ifStackStatus ifType pagpEthcOperationMode pagpGroupIfIndex
187 dot3adAggPortSelectedAggID lag_members/;
188 }
189  
190 1;
191  
192 __END__