blib/lib/Net/SNMP/Mixin/Dot1dBase.pm

filter: Statementstmt 27.5% 27 / 98 Branchbran 2.3% 1 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 8 / 16 Podpod 100% 3 / 3 totaltotal 23.2% 39 / 168 CC 23 SCAR 29.4
CC
23
Cov
19%
_init
42.0
_fetch_dot1d_base
30.0
_fetch_dot1d_base_ports
30.0
CRAP 18.8
↑ Summary
1 package Net::SNMP::Mixin::Dot1dBase;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #
7 # store this package name in a handy variable,
8 # used for unambiguous prefix of mixin attributes
9 # storage in object hash
10 #
11 4 my $prefix = __PACKAGE__;
12  
13 #
14 # this module import config
15 #
16 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Net::SNMP::Mixin::Util qw/idx2val normalize_mac push_error get_init_slot/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 #
20 # this module export config
21 #
22 4 my @mixin_methods;
23  
24 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 @mixin_methods = (
26 qw/
27 get_dot1d_base_group
28 map_bridge_ports2if_indexes
29 map_if_indexes2bridge_ports
30 /
31 );
32 }
33  
34 4 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 exports => [@mixin_methods],
36 groups => { default => [@mixin_methods], },
37 4 };
38  
39 #
40 # SNMP oid constants used in this module
41 #
42 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 DOT1D_BASE_BRIDGE_ADDRESS => '1.3.6.1.2.1.17.1.1.0',
44 DOT1D_BASE_NUM_PORTS => '1.3.6.1.2.1.17.1.2.0',
45 DOT1D_BASE_TYPE => '1.3.6.1.2.1.17.1.3.0',
46  
47 DOT1D_BASE_PORT_IF_INDEX => '1.3.6.1.2.1.17.1.4.1.2',
48 4 };
49  
50 =head1 NAME
51  
52 Net::SNMP::Mixin::Dot1dBase - mixin class for the switch dot1d base values
53  
54 =cut
55  
56 4 our $VERSION = '0.10';
57  
58 =head1 SYNOPSIS
59  
60 A Net::SNMP mixin class for Dot1d base info.
61  
62 use Net::SNMP;
63 use Net::SNMP::Mixin;
64  
65 # class based mixin
66 Net::SNMP->mixer('Net::SNMP::Mixin::Dot1dBase');
67  
68 # ...
69  
70 my $session = Net::SNMP->session( -hostname => 'foo.bar.com' );
71  
72 $session->mixer('Net::SNMP::Mixin::Dot1dBase');
73 $session->init_mixins;
74 snmp_dispatcher() if $session->nonblocking;
75 $session->init_ok;
76 die $session->errors if $session->errors;
77  
78 my $base_group = $session->get_dot1d_base_group;
79  
80 printf "BridgeAddr: %s NumPorts: %d Type: %d\n",
81 $base_group->{dot1dBaseBridgeAddress},
82 $base_group->{dot1dBaseNumPorts},
83 $base_group->{dot1dBaseType};
84  
85 my $map = $session->map_bridge_ports2if_indexes;
86  
87 foreach my $bridge_port ( sort {$a <=> $b} keys %$map ) {
88 my $if_index = $map->{$bridge_port};
89 printf "bridgePort: %4d -> ifIndex: %4\n", $bridge_port, $if_index;
90 }
91  
92  
93 =head1 DESCRIPTION
94  
95 A mixin class for basic switch information from the BRIDGE-MIB.
96  
97 Besides the bridge address and the number of bridge ports, it's primary use is the mapping between dot1dBasePorts and ifIndexes.
98  
99 =head1 MIXIN METHODS
100  
101 =head2 B<< OBJ->get_dot1d_base_group() >>
102  
103 Returns the dot1dBase group as a hash reference:
104  
105 {
106 dot1dBaseBridgeAddress => MacAddress,
107 dot1dBaseNumPorts => INTEGER,
108 dot1dBaseType => INTEGER,
109 }
110  
111 =cut
112  
113 0 ▶sub get_dot1d_base_group {
Subroutine get_dot1d_base_group: not called CC=2 SCAR=17.9
Pod documented
114 0 my $session = shift;
115 0 my $agent = $session->hostname;
116  
117 0 ▶ Carp::croak "$agent: '$prefix' not initialized,"
Branch: 1 branch
BranchTrueFalseTotal
unless $session->init_ok($prefix) 0 0 0
118 unless $session->init_ok($prefix);
119  
120 0 my $result = { %{ $session->{$prefix}{dot1dBase} } };
121  
122 # normalize the MAC address
123 $result->{dot1dBaseBridgeAddress} =
124 0 normalize_mac( $result->{dot1dBaseBridgeAddress} );
125  
126 0 return $result;
127 }
128  
129 =head2 B<< OBJ->map_bridge_ports2if_indexes() >>
130  
131 Returns a reference to a hash with the following entries:
132  
133 {
134 # INTEGER INTEGER
135 dot1dBasePort => dot1dBasePortIfIndex,
136 }
137  
138 =cut
139  
140 0 ▶sub map_bridge_ports2if_indexes {
Subroutine map_bridge_ports2if_indexes: not called CC=3 SCAR=24.8
Pod documented
141 0 my ( $session, ) = @_;
142 0 my $agent = $session->hostname;
143  
144 0 ▶ Carp::croak "$agent: '$prefix' not initialized,"
Branch: 1 branch
BranchTrueFalseTotal
unless $session->init_ok($prefix) 0 0 0
145 unless $session->init_ok($prefix);
146  
147 # datastructure:
148 # $session->{$prefix}{dot1dBasePortIfIndex}{$dot1d_base_port} = ifIndex
149 #
150  
151 0 my $result = {};
152  
153 0 ▶ while ( my ( $bridge_port, $if_index ) = each %{ $session->{$prefix}{dot1dBasePortIfIndex} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$session{$prefix}{"dot1dBasePortIfIndex"};}) 0 0 0
154 0 $result->{$bridge_port} = $if_index;
155 }
156  
157 0 return $result;
158 }
159  
160 =head2 B<< OBJ->map_if_indexes2bridge_ports() >>
161  
162 Returns a reference to a hash with the following entries:
163  
164 {
165 # INTEGER INTEGER
166 dot1dBasePortIfIndex => dot1dBasePort ,
167 }
168  
169 =cut
170  
171 0 ▶sub map_if_indexes2bridge_ports {
Subroutine map_if_indexes2bridge_ports: not called CC=3 SCAR=24.8
Pod documented
172 0 my ( $session, ) = @_;
173 0 my $agent = $session->hostname;
174  
175 0 ▶ Carp::croak "$agent: '$prefix' not initialized,"
Branch: 1 branch
BranchTrueFalseTotal
unless $session->init_ok($prefix) 0 0 0
176 unless $session->init_ok($prefix);
177  
178 # datastructure:
179 # $session->{$prefix}{dot1dBasePortIfIndex}{$dot1d_base_port} = ifIndex
180 #
181  
182 0 my $result = {};
183  
184 0 ▶ while ( my ( $bridge_port, $if_index ) = each %{ $session->{$prefix}{dot1dBasePortIfIndex} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$session{$prefix}{"dot1dBasePortIfIndex"};}) 0 0 0
185 0 $result->{$if_index} = $bridge_port;
186 }
187  
188 0 return $result;
189 }
190  
191 =head1 INITIALIZATION
192  
193 =cut
194  
195 =head2 B<< OBJ->_init($reload) >>
196  
197 Fetch the dot1d base related snmp values from the host. Don't call this method direct!
198  
199 =cut
200  
201 #
202 # due to the asynchron nature, we don't know what init job is really the last, we decrement
203 # the value after each callback
204 #
205 4 ▶use constant THIS_INIT_JOBS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207 0 ▶sub _init {
Subroutine _init: not called CC=6 SCAR=37.4
208 0 my ( $session, $reload ) = @_;
209 0 my $agent = $session->hostname;
210  
211 die "$agent: $prefix already initialized and reload not forced.\n"
212 if exists get_init_slot($session)->{$prefix}
213 0 ▶ && get_init_slot($session)->{$prefix} == 0
Branch: 1 branch
BranchTrueFalseTotal
if exists get_init_slot($session)->{$prefix} and get_init_slot($session)->{$prefix} == 0 and not $reload 0 0 0
Condition0%0/6
exists get_init_slot($session)->{$prefix} and get_init_slot($session)->{$prefix} == 0 and not $reload
!ll&&!rl&&r
000
exists get_init_slot($session)->{$prefix} and get_init_slot($session)->{$prefix} == 0
!ll&&!rl&&r
000
MC/DC0%0/3
exists get_init_slot($session)->{$prefix} and get_init_slot($session)->{$prefix} == 0 and not $reload
exists get_init_slot($session)->{$prefix} get_init_slot($session)->{$prefix} == 0 not $reload
Truth table0%0/4
A and B and not(C)
A= exists get_init_slot($session)->{$prefix}B= get_init_slot($session)->{$prefix} == 0C= not $reload
ABCresult
0XX0
10X0
1100
1111
214 && not $reload;
215  
216 # set number of async init jobs for proper initialization
217 0 get_init_slot($session)->{$prefix} = THIS_INIT_JOBS;
218  
219 # initialize the object for dot1dbase infos
220 0 _fetch_dot1d_base($session);
221 0 ▶ return if $session->error;
Branch: 1 branch
BranchTrueFalseTotal
if $session->error 0 0 0
222  
223 # LLDP, Dot1Q, STP, LLDP, ... tables are indexed
224 # by dot1dbaseports and not ifIndexes
225 # table to map between dot1dBasePort <-> ifIndex
226  
227 0 _fetch_dot1d_base_ports($session);
228 0 ▶ return if $session->error;
Branch: 1 branch
BranchTrueFalseTotal
if $session->error 0 0 0
229  
230 0 return 1;
231 }
232  
233 =head1 PRIVATE METHODS
234  
235 Only for developers or maintainers.
236  
237 =head2 B<< _fetch_dot1d_base($session) >>
238  
239 Fetch values from the dot1dBase group once during object initialization.
240  
241 =cut
242  
243 0 ▶sub _fetch_dot1d_base {
Subroutine _fetch_dot1d_base: not called CC=5 SCAR=34.0
244 0 my $session = shift;
245 0 my $result;
246  
247 # fetch the dot1dBase group
248 0 ▶ $result = $session->get_request(
Branch: 1 branch
BranchTrueFalseTotal
$session->nonblocking ? : 0 0 0
249 -varbindlist => [
250  
251 DOT1D_BASE_BRIDGE_ADDRESS,
252 DOT1D_BASE_NUM_PORTS,
253 DOT1D_BASE_TYPE,
254 ],
255  
256 # define callback if in nonblocking mode
257 $session->nonblocking ? ( -callback => \&_dot1d_base_cb ) : (),
258 );
259  
260 0 ▶ unless ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 0 0
261 0 ▶ if ( my $err_msg = $session->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err_msg = $session->error) 0 0 0
262 0 push_error( $session, "$prefix: $err_msg" );
263 }
264 0 return;
265 }
266  
267 # in nonblocking mode the callback will be called asynchronously
268 0 ▶ return 1 if $session->nonblocking;
Branch: 1 branch
BranchTrueFalseTotal
if $session->nonblocking 0 0 0
269  
270 # call the callback function in blocking mode by hand
271 0 _dot1d_base_cb($session);
272  
273 }
274  
275 =head2 B<< _dot1d_base_cb($session) >>
276  
277 The callback for _fetch_dot1d_base.
278  
279 =cut
280  
281 0 ▶sub _dot1d_base_cb {
Subroutine _dot1d_base_cb: not called CC=3 SCAR=24.8
282 0 my $session = shift;
283 0 my $vbl = $session->var_bind_list;
284  
285 0 ▶ unless ( defined $vbl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vbl) 0 0 0
286 0 ▶ if ( my $err_msg = $session->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err_msg = $session->error) 0 0 0
287 0 push_error( $session, "$prefix: $err_msg" );
288 }
289 0 return;
290 }
291  
292 $session->{$prefix}{dot1dBase}{dot1dBaseBridgeAddress} =
293 0 $vbl->{ DOT1D_BASE_BRIDGE_ADDRESS() };
294  
295 $session->{$prefix}{dot1dBase}{dot1dBaseNumPorts} =
296 0 $vbl->{ DOT1D_BASE_NUM_PORTS() };
297  
298 0 $session->{$prefix}{dot1dBase}{dot1dBaseType} = $vbl->{ DOT1D_BASE_TYPE() };
299  
300 # this init job is finished
301 0 get_init_slot($session)->{$prefix}--;
302  
303 0 return 1;
304 }
305  
306 =head2 B<< _fetch_dot1d_base_ports($session) >>
307  
308 Populate the object with the dot1dBasePorts.
309  
310 =cut
311  
312 0 ▶sub _fetch_dot1d_base_ports {
Subroutine _fetch_dot1d_base_ports: not called CC=5 SCAR=34.0
313 0 my $session = shift;
314 0 my $result;
315  
316 # fetch the dot1dBasePorts, in blocking or nonblocking mode
317 0 ▶ $result = $session->get_entries(
Branch: 1 branch
BranchTrueFalseTotal
$session->nonblocking ? : 0 0 0
318 -columns => [ DOT1D_BASE_PORT_IF_INDEX, ],
319  
320 # define callback if in nonblocking mode
321 $session->nonblocking ? ( -callback => \&_dot1d_base_ports_cb ) : (),
322 );
323  
324 0 ▶ unless ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 0 0
325 0 ▶ if ( my $err_msg = $session->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err_msg = $session->error) 0 0 0
326 0 push_error( $session, "$prefix: $err_msg" );
327 }
328 0 return;
329 }
330  
331 # in nonblocking mode the callback will be called asynchronously
332 0 ▶ return 1 if $session->nonblocking;
Branch: 1 branch
BranchTrueFalseTotal
if $session->nonblocking 0 0 0
333  
334 # call the callback funktion in blocking mode by hand
335 0 _dot1d_base_ports_cb($session);
336  
337 }
338  
339 =head2 B<< _dot1d_base_ports_cb($session) >>
340  
341 The callback for _fetch_dot1d_base_ports.
342  
343 =cut
344  
345 0 ▶sub _dot1d_base_ports_cb {
Subroutine _dot1d_base_ports_cb: not called CC=3 SCAR=24.8
346 0 my $session = shift;
347 0 my $vbl = $session->var_bind_list;
348  
349 0 ▶ unless ( defined $vbl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vbl) 0 0 0
350 0 ▶ if ( my $err_msg = $session->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err_msg = $session->error) 0 0 0
351 0 push_error( $session, "$prefix: $err_msg" );
352 }
353 0 return;
354 }
355  
356 # mangle result table to get plain idx->value
357  
358 $session->{$prefix}{dot1dBasePortIfIndex} =
359 0 idx2val( $vbl, DOT1D_BASE_PORT_IF_INDEX );
360  
361 # this init job is finished
362 0 get_init_slot($session)->{$prefix}--;
363  
364 0 return 1;
365 }
366  
367 =head1 REQUIREMENTS
368  
369 L<< Net::SNMP >>, L<< Net::SNMP::Mixin >>
370  
371 =head1 AUTHOR
372  
373 Karl Gaissmaier <karl.gaissmaier at uni-ulm.de>
374  
375 =head1 COPYRIGHT & LICENSE
376  
377 Copyright 2008-2021 Karl Gaissmaier, all rights reserved.
378  
379 This program is free software; you can redistribute it and/or modify it
380 under the same terms as Perl itself.
381  
382 =cut
383  
384 4 ▶unless ( caller() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 0 4 4
385 0 print "$prefix compiles and initializes successful.\n";
386 }
387  
388 4 1;
389  
390 # vim: sw=2
↑ Summary