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

filter: Statementstmt 34.0% 32 / 94 Branchbran 5.0% 1 / 20 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 69.2% 9 / 13 Podpod 100% 1 / 1 totaltotal 28.8% 43 / 149 CC 17 SCAR 32.4
CC
17
Cov
26%
get_ip_route_table
56.0
_init
30.0
_fetch_ip_route_tbl
30.0
CRAP 25.4
↑ Summary
1 package Net::SNMP::Mixin::IpRouteTable;
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 qw/oid_lex_sort/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Net::SNMP::Mixin::Util qw/idx2val push_error get_init_slot/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 #
21 # this module export config
22 #
23 4 my @mixin_methods;
24  
25 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 @mixin_methods = (qw/ get_ip_route_table /);
27 }
28  
29 4 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 exports => [@mixin_methods],
31 groups => { default => [@mixin_methods], },
32 4 };
33  
34 #
35 # SNMP oid constants used in this module
36 #
37 # from mib-II
38 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 IP_ROUTE_TABLE => '1.3.6.1.2.1.4.21',
40  
41 IP_ROUTE_DEST => '1.3.6.1.2.1.4.21.1.1',
42 IP_ROUTE_IFINDEX => '1.3.6.1.2.1.4.21.1.2',
43 IP_ROUTE_METRIC1 => '1.3.6.1.2.1.4.21.1.3',
44 IP_ROUTE_METRIC2 => '1.3.6.1.2.1.4.21.1.4',
45 IP_ROUTE_METRIC3 => '1.3.6.1.2.1.4.21.1.5',
46 IP_ROUTE_METRIC4 => '1.3.6.1.2.1.4.21.1.6',
47 IP_ROUTE_NEXTHOP => '1.3.6.1.2.1.4.21.1.7',
48 IP_ROUTE_TYPE => '1.3.6.1.2.1.4.21.1.8',
49 IP_ROUTE_PROTO => '1.3.6.1.2.1.4.21.1.9',
50 IP_ROUTE_AGE => '1.3.6.1.2.1.4.21.1.10',
51 IP_ROUTE_MASK => '1.3.6.1.2.1.4.21.1.11',
52 IP_ROUTE_METRIC5 => '1.3.6.1.2.1.4.21.1.12',
53 IP_ROUTE_INFO => '1.3.6.1.2.1.4.21.1.13',
54 4 };
55  
56 #
57 # the ipRouteType enum
58 #
59 4 my %ip_route_type_enum = (
60 1 => 'other',
61 2 => 'invalid',
62 3 => 'direct',
63 4 => 'indirect',
64 );
65  
66 #
67 # the ipRouteProto enum
68 #
69 4 my %ip_route_proto_enum = (
70 1 => 'other',
71 2 => 'local',
72 3 => 'netmgmt',
73 4 => 'icmp',
74 5 => 'egp',
75 6 => 'ggp',
76 7 => 'hello',
77 8 => 'rip',
78 9 => 'is-is',
79 10 => 'es-is',
80 11 => 'ciscoIgrp',
81 12 => 'bbnSpfIgp',
82 13 => 'ospf',
83 14 => 'bgp',
84 );
85  
86 =head1 NAME
87  
88 Net::SNMP::Mixin::IpRouteTable - mixin class for the mib-II ipRouteTable
89  
90 =head1 VERSION
91  
92 Version 0.04
93  
94 =cut
95  
96 4 our $VERSION = '0.04';
97  
98 =head1 SYNOPSIS
99  
100 use Net::SNMP;
101 use Net::SNMP::Mixin;
102  
103 #...
104  
105 my $session = Net::SNMP->session( -hostname => 'foo.bar.com' );
106  
107 $session->mixer('Net::SNMP::Mixin::IpRouteTable');
108 $session->init_mixins;
109 snmp_dispatcher();
110 $session->init_ok();
111 die $session->errors if $session->errors;
112  
113 my @routes = get_ip_route_table();
114  
115 foreach my $route ( @routes ) {
116  
117 my $dest = $route->{ipRouteDest};
118 my $mask = $route->{ipRouteMask};
119 my $nhop = $route->{ipRouteNextHop};
120 my $proto_str = $route->{ipRouteProtoString};
121 my $type_str = $route->{ipRouteTypeString};
122  
123 print "$dest/$mask => $nhop $proto_str $type_str\n";
124 }
125  
126 =head1 DESCRIPTION
127  
128 A Net::SNMP mixin class for mib-II ipRouteTable info.
129  
130 =head1 MIXIN METHODS
131  
132 =head2 B<< OBJ->get_ip_route_table() >>
133  
134 Returns the mib-II ipRouteTable as a list. Every route entry is a reference to a hash with the following fields and values:
135  
136 {
137 ipRouteDest => IpAddress,
138 ipRouteMask => IpAddress,
139 ipRouteNextHop => IpAddress,
140 ipRouteIfIndex => INTEGER,
141 ipRouteMetric1 => INTEGER,
142 ipRouteMetric2 => INTEGER,
143 ipRouteMetric3 => INTEGER,
144 ipRouteMetric4 => INTEGER,
145 ipRouteMetric5 => INTEGER,
146 ipRouteType => INTEGER,
147 ipRouteTypeString => String, # resolved enum
148 ipRouteProto => INTEGER,
149 ipRouteTypeProto => String, # resolved enum
150 ipRouteAge => INTEGER,
151 ipRouteInfo => OBJECT IDENTIFIER
152 }
153  
154 =cut
155  
156 0 ▶sub get_ip_route_table {
Subroutine get_ip_route_table: not called CC=7 SCAR=40.3
Pod documented
157 0 my $session = shift;
158 0 my $agent = $session->hostname;
159  
160 0 ▶ Carp::croak "$agent: '$prefix' not initialized,"
Branch: 1 branch
BranchTrueFalseTotal
unless $session->init_ok($prefix) 0 0 0
161 unless $session->init_ok($prefix);
162  
163 # stash for return values
164 0 my @route_tbl;
165  
166 my (
167 0 $ipRouteDest, $ipRouteMask, $ipRouteNextHop, $ipRouteIfIndex, $ipRouteMetric1,
168 $ipRouteMetric2, $ipRouteMetric3, $ipRouteMetric4, $ipRouteMetric5, $ipRouteType,
169 $ipRouteTypeString, $ipRouteProto, $ipRouteProtoString, $ipRouteAge, $ipRouteInfo,
170 );
171  
172 # index is ipRouteDest
173 0 foreach my $idx ( oid_lex_sort keys %{ $session->{$prefix}{ipRouteDest} } ) {
174  
175 0 $ipRouteDest = $session->{$prefix}{ipRouteDest}{$idx};
176 0 $ipRouteMask = $session->{$prefix}{ipRouteMask}{$idx};
177 0 $ipRouteNextHop = $session->{$prefix}{ipRouteNextHop}{$idx};
178 0 $ipRouteIfIndex = $session->{$prefix}{ipRouteIfIndex}{$idx};
179 0 $ipRouteMetric1 = $session->{$prefix}{ipRouteMetric1}{$idx};
180 0 $ipRouteMetric2 = $session->{$prefix}{ipRouteMetric2}{$idx};
181 0 $ipRouteMetric3 = $session->{$prefix}{ipRouteMetric3}{$idx};
182 0 $ipRouteMetric4 = $session->{$prefix}{ipRouteMetric4}{$idx};
183 0 $ipRouteMetric5 = $session->{$prefix}{ipRouteMetric5}{$idx};
184 0 ▶ $ipRouteType = $session->{$prefix}{ipRouteType}{$idx} || -1;
Condition0%0/2
$session->{$prefix}{'ipRouteType'}{$idx} || -1
l!l
00
MC/DC0%0/1
$session->{$prefix}{'ipRouteType'}{$idx} || -1
$session->{$prefix}{'ipRouteType'}{$idx}
Truth table0%0/2
A
A= $session->{$prefix}{'ipRouteType'}{$idx}
Aresult
11
00
185 0 ▶ $ipRouteTypeString = $ip_route_type_enum{$ipRouteType} || 'unknown';
Condition0%0/2
$ip_route_type_enum{$ipRouteType} || 'unknown'
l!l
00
MC/DC0%0/1
$ip_route_type_enum{$ipRouteType} || 'unknown'
$ip_route_type_enum{$ipRouteType}
Truth table0%0/2
A
A= $ip_route_type_enum{$ipRouteType}
Aresult
11
00
186 0 ▶ $ipRouteProto = $session->{$prefix}{ipRouteProto}{$idx} || -1;
Condition0%0/2
$session->{$prefix}{'ipRouteProto'}{$idx} || -1
l!l
00
MC/DC0%0/1
$session->{$prefix}{'ipRouteProto'}{$idx} || -1
$session->{$prefix}{'ipRouteProto'}{$idx}
Truth table0%0/2
A
A= $session->{$prefix}{'ipRouteProto'}{$idx}
Aresult
11
00
187 0 ▶ $ipRouteProtoString = $ip_route_proto_enum{$ipRouteProto}
Condition0%0/2
$ip_route_proto_enum{$ipRouteProto} || 'unknown'
l!l
00
MC/DC0%0/1
$ip_route_proto_enum{$ipRouteProto} || 'unknown'
$ip_route_proto_enum{$ipRouteProto}
Truth table0%0/2
A
A= $ip_route_proto_enum{$ipRouteProto}
Aresult
11
00
188 || 'unknown';
189 0 $ipRouteAge = $session->{$prefix}{ipRouteAge}{$idx};
190 0 $ipRouteInfo = $session->{$prefix}{ipRouteInfo}{$idx};
191  
192 0 push @route_tbl,
193 {
194 ipRouteDest => $ipRouteDest,
195 ipRouteMask => $ipRouteMask,
196 ipRouteNextHop => $ipRouteNextHop,
197 ipRouteIfIndex => $ipRouteIfIndex,
198 ipRouteMetric1 => $ipRouteMetric1,
199 ipRouteMetric2 => $ipRouteMetric2,
200 ipRouteMetric3 => $ipRouteMetric3,
201 ipRouteMetric4 => $ipRouteMetric4,
202 ipRouteMetric5 => $ipRouteMetric5,
203 ipRouteType => $ipRouteType,
204 ipRouteTypeString => $ipRouteTypeString,
205 ipRouteProto => $ipRouteProto,
206 ipRouteProtoString => $ipRouteProtoString,
207 ipRouteAge => $ipRouteAge,
208 ipRouteInfo => $ipRouteInfo,
209 };
210 }
211  
212 0 return @route_tbl;
213 }
214  
215 =head1 INITIALIZATION
216  
217 =head2 B<< OBJ->_init($reload) >>
218  
219 Fetch the mib-II ipRouteTable from the host. Don't call this method direct!
220  
221 =cut
222  
223 #
224 # due to the asynchron nature, we don't know what init job is really the last, we decrement
225 # the value after each callback
226 #
227 4 ▶use constant THIS_INIT_JOBS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
228  
229 0 ▶sub _init {
Subroutine _init: not called CC=5 SCAR=34.0
230 0 my ( $session, $reload ) = @_;
231 0 my $agent = $session->hostname;
232  
233 die "$agent: $prefix already initialized and reload not forced.\n"
234 if exists get_init_slot($session)->{$prefix}
235 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
236 && not $reload;
237  
238 # set number of async init jobs for proper initialization
239 0 get_init_slot($session)->{$prefix} = THIS_INIT_JOBS;
240  
241 # populate the object with needed mib values
242 #
243 # initialize the object for ipRouteTable infos
244 0 _fetch_ip_route_tbl($session);
245 0 ▶ return if $session->error;
Branch: 1 branch
BranchTrueFalseTotal
if $session->error 0 0 0
246  
247 0 return 1;
248 }
249  
250 =head1 PRIVATE METHODS
251  
252 Only for developers or maintainers.
253  
254 =head2 B<< _fetch_ip_route_tbl($session) >>
255  
256 Fetch the ipRouteTable once during object initialization.
257  
258 =cut
259  
260 0 ▶sub _fetch_ip_route_tbl {
Subroutine _fetch_ip_route_tbl: not called CC=5 SCAR=34.0
261 0 my $session = shift;
262 0 my $result;
263  
264 # fetch the ipRouteTable
265 0 ▶ $result = $session->get_table(
Branch: 1 branch
BranchTrueFalseTotal
$session->nonblocking ? : 0 0 0
266 -baseoid => IP_ROUTE_TABLE,
267  
268 # define callback if in nonblocking mode
269 $session->nonblocking ? ( -callback => \&_ip_route_tbl_cb ) : (),
270 );
271  
272 0 ▶ unless ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 0 0
273 0 ▶ if (my $err_msg = $session->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err_msg = $session->error) 0 0 0
274 0 push_error($session, "$prefix: $err_msg");
275 };
276 0 return;
277 }
278  
279 # in nonblocking mode the callback will be called asynchronously
280 0 ▶ return 1 if $session->nonblocking;
Branch: 1 branch
BranchTrueFalseTotal
if $session->nonblocking 0 0 0
281  
282 # ok we are in synchronous mode, call the result mangling function
283 # by hand
284 0 _ip_route_tbl_cb($session);
285  
286 }
287  
288 =head2 B<< _ip_route_tbl_cb($session) >>
289  
290 The callback for _fetch_ip_route_tbl().
291  
292 =cut
293  
294 0 ▶sub _ip_route_tbl_cb {
Subroutine _ip_route_tbl_cb: not called CC=3 SCAR=24.8
295 0 my $session = shift;
296 0 my $vbl = $session->var_bind_list;
297  
298 0 ▶ unless ( defined $vbl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vbl) 0 0 0
299 0 ▶ if (my $err_msg = $session->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err_msg = $session->error) 0 0 0
300 0 push_error($session, "$prefix: $err_msg");
301 };
302 0 return;
303 }
304  
305 # mangle result table to get plain idx->value
306 #
307 # result hashes: ipRouteDest => values
308 #
309  
310 $session->{$prefix}{ipRouteDest} =
311 0 idx2val( $vbl, IP_ROUTE_DEST, undef, undef, );
312  
313 $session->{$prefix}{ipRouteIfIndex} =
314 0 idx2val( $vbl, IP_ROUTE_IFINDEX, undef, undef, );
315  
316 $session->{$prefix}{ipRouteMetric1} =
317 0 idx2val( $vbl, IP_ROUTE_METRIC1, undef, undef, );
318  
319 $session->{$prefix}{ipRouteMetric2} =
320 0 idx2val( $vbl, IP_ROUTE_METRIC2, undef, undef, );
321  
322 $session->{$prefix}{ipRouteMetric3} =
323 0 idx2val( $vbl, IP_ROUTE_METRIC3, undef, undef, );
324  
325 $session->{$prefix}{ipRouteMetric4} =
326 0 idx2val( $vbl, IP_ROUTE_METRIC4, undef, undef, );
327  
328 $session->{$prefix}{ipRouteMetric5} =
329 0 idx2val( $vbl, IP_ROUTE_METRIC5, undef, undef, );
330  
331 $session->{$prefix}{ipRouteNextHop} =
332 0 idx2val( $vbl, IP_ROUTE_NEXTHOP, undef, undef, );
333  
334 $session->{$prefix}{ipRouteType} =
335 0 idx2val( $vbl, IP_ROUTE_TYPE, undef, undef, );
336  
337 $session->{$prefix}{ipRouteProto} =
338 0 idx2val( $vbl, IP_ROUTE_PROTO, undef, undef, );
339  
340 $session->{$prefix}{ipRouteAge} =
341 0 idx2val( $vbl, IP_ROUTE_AGE, undef, undef, );
342  
343 $session->{$prefix}{ipRouteMask} =
344 0 idx2val( $vbl, IP_ROUTE_MASK, undef, undef, );
345  
346 $session->{$prefix}{ipRouteInfo} =
347 0 idx2val( $vbl, IP_ROUTE_INFO, undef, undef, );
348  
349 # this init job is finished
350 0 get_init_slot($session)->{$prefix}--;
351  
352 0 return 1;
353 }
354  
355 4 ▶unless ( caller() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 0 4 4
356 0 print "$prefix compiles and initializes successful.\n";
357 }
358  
359 =head1 SEE ALSO
360  
361 L<< Net::SNMP::Mixin >>
362  
363 =head1 REQUIREMENTS
364  
365 L<< Net::SNMP >>, L<< Net::SNMP::Mixin >>
366  
367 =head1 BUGS, PATCHES & FIXES
368  
369 There are no known bugs at the time of this release. However, if you spot a bug or are experiencing difficulties that are not explained within the POD documentation, please submit a bug to the RT system (see link below). However, it would help greatly if you are able to pinpoint problems or even supply a patch.
370  
371 Fixes are dependant upon their severity and my availablity. Should a fix not be forthcoming, please feel free to (politely) remind me by sending an email to gaissmai@cpan.org .
372  
373 RT: http://rt.cpan.org/Public/Dist/Display.html?Name=Net-SNMP-Mixin-IpRouteTable
374  
375  
376 =head1 AUTHOR
377  
378 Karl Gaissmaier <karl.gaissmaier at uni-ulm.de>
379  
380 =head1 COPYRIGHT & LICENSE
381  
382 Copyright 2008-2016 Karl Gaissmaier, all rights reserved.
383  
384 This program is free software; you can redistribute it and/or modify it
385 under the same terms as Perl itself.
386  
387 =cut
388  
389 4 1;
390  
391 # vim: sw=2
↑ Summary