File Coverage

blib/lib/Net/SNMP/Mixin/IpCidrRouteTable.pm
Criterion Covered Total %
statement 43 86 50.0
branch 12 18 66.6
condition 1 15 6.6
subroutine 12 12 100.0
pod 1 1 100.0
total 69 132 52.2


line stmt bran cond sub pod time code
1             package Net::SNMP::Mixin::IpCidrRouteTable;
2              
3 4     4   404590 use strict;
  4         12  
  4         149  
4 4     4   22 use warnings;
  4         9  
  4         165  
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             my $prefix = __PACKAGE__;
12              
13             #
14             # this module import config
15             #
16 4     4   22 use Carp ();
  4         13  
  4         77  
17 4     4   1529 use Net::SNMP qw(oid_lex_sort);
  4         82361  
  4         593  
18 4     4   3832 use Net::SNMP::Mixin::Util qw/idx2val push_error/;
  4         16808  
  4         31  
19              
20             #
21             # this module export config
22             #
23             my @mixin_methods;
24              
25             BEGIN {
26 4     4   1229 @mixin_methods = (qw/ get_ip_cidr_route_table /);
27             }
28              
29 4         33 use Sub::Exporter -setup => {
30             exports => [@mixin_methods],
31             groups => { default => [@mixin_methods], },
32 4     4   27 };
  4         8  
33              
34             #
35             # SNMP oid constants used in this module
36             #
37             # from IP Forwarding Table MIB, RFC 2096
38             #
39             use constant {
40 4         5647 IP_CIDR_ROUTE_TABLE => '1.3.6.1.2.1.4.24.4',
41             IP_CIDR_ROUTE_ENTRY => '1.3.6.1.2.1.4.24.4.1',
42              
43             IP_CIDR_ROUTE_DEST => '1.3.6.1.2.1.4.24.4.1.1',
44             IP_CIDR_ROUTE_MASK => '1.3.6.1.2.1.4.24.4.1.2',
45             IP_CIDR_ROUTE_TOS => '1.3.6.1.2.1.4.24.4.1.3',
46             IP_CIDR_ROUTE_NEXTHOP => '1.3.6.1.2.1.4.24.4.1.4',
47             IP_CIDR_ROUTE_IFINDEX => '1.3.6.1.2.1.4.24.4.1.5',
48             IP_CIDR_ROUTE_TYPE => '1.3.6.1.2.1.4.24.4.1.6',
49             IP_CIDR_ROUTE_PROTO => '1.3.6.1.2.1.4.24.4.1.7',
50             IP_CIDR_ROUTE_AGE => '1.3.6.1.2.1.4.24.4.1.8',
51             IP_CIDR_ROUTE_INFO => '1.3.6.1.2.1.4.24.4.1.9',
52             IP_CIDR_ROUTE_NEXTHOPAS => '1.3.6.1.2.1.4.24.4.1.10',
53             IP_CIDR_ROUTE_METRIC1 => '1.3.6.1.2.1.4.24.4.1.11',
54             IP_CIDR_ROUTE_METRIC2 => '1.3.6.1.2.1.4.24.4.1.12',
55             IP_CIDR_ROUTE_METRIC3 => '1.3.6.1.2.1.4.24.4.1.13',
56             IP_CIDR_ROUTE_METRIC4 => '1.3.6.1.2.1.4.24.4.1.14',
57             IP_CIDR_ROUTE_METRIC5 => '1.3.6.1.2.1.4.24.4.1.15',
58             IP_CIDR_ROUTE_STATUS => '1.3.6.1.2.1.4.24.4.1.16',
59 4     4   1615 };
  4         8  
60              
61             #
62             # the ipCidrRouteType enum
63             #
64             my %type_enum = (
65             1 => 'other',
66             2 => 'reject',
67             3 => 'local',
68             4 => 'remote',
69             );
70              
71             #
72             # the ipCidrRouteProto enum
73             #
74             my %proto_enum = (
75             1 => 'other',
76             2 => 'local',
77             3 => 'netmgmt',
78             4 => 'icmp',
79             5 => 'egp',
80             6 => 'ggp',
81             7 => 'hello',
82             8 => 'rip',
83             9 => 'is-is',
84             10 => 'es-is',
85             11 => 'ciscoIgrp',
86             12 => 'bbnSpfIgp',
87             13 => 'ospf',
88             14 => 'bgp',
89             15 => 'idpr',
90             16 => 'ciscoEigrp',
91             );
92              
93             #
94             # the ipCidrRouteStatus enum
95             #
96             my %status_enum = (
97             1 => 'active',
98             2 => 'notInService',
99             3 => 'notReady',
100             4 => 'createAndGo',
101             5 => 'createAndWait',
102             6 => 'destroy',
103             );
104              
105             =head1 NAME
106              
107             Net::SNMP::Mixin::IpCidrRouteTable - mixin class for the mib-II ipCidrRouteTable
108              
109             =head1 VERSION
110              
111             Version 0.04
112              
113             =cut
114              
115             our $VERSION = '0.04';
116              
117             =head1 SYNOPSIS
118              
119             use Net::SNMP;
120             use Net::SNMP::Mixin;
121              
122             #...
123              
124             my $session = Net::SNMP->session( -hostname => 'foo.bar.com' );
125              
126             $session->mixer('Net::SNMP::Mixin::IpCidrRouteTable');
127             $session->init_mixins;
128             snmp_dispatcher();
129              
130             die $session->errors if $session->errors;
131              
132             my @routes = $session->get_ip_cidr_route_table();
133              
134             foreach my $route (@routes) {
135             my $dest = $route->{ipCidrRouteDest};
136             my $mask = $route->{ipCidrRouteMask};
137             my $tos = $route->{ipCidrRouteTos};
138             my $next_hop = $route->{ipCidrRouteNextHop};
139             my $if_index = $route->{ipCidrRouteIfIndex};
140              
141             print "$dest/$mask/$tos/$next_hop => $if_index/$next_hop\n";
142             }
143              
144             =head1 DESCRIPTION
145              
146             A Net::SNMP mixin class for mib-II ipCidrRouteTable info.
147              
148             =head1 MIXIN METHODS
149              
150             =head2 B<< OBJ->get_ip_cidr_route_table() >>
151              
152             Returns a sorted list of mib-II ipCidrRouteTable entries. Every list element (route entry) is a hashref with the following fields and values:
153              
154             {
155             ipCidrRouteDest => IpAddress, # tbl index
156             ipCidrRouteMask => IpAddress, # tbl index
157             ipCidrRouteTos => Integer32, # tbl index
158             ipCidrRouteNextHop => IpAddress, # tbl index
159             ipCidrRouteIfIndex => Integer32,
160             ipCidrRouteType => INTEGER,
161             ipCidrRouteTypeString => String, # resolved enum
162             ipCidrRouteProto => INTEGER,
163             ipCidrRouteProtoString => String, # resolved enum
164             ipCidrRouteAge => Integer32,
165             ipCidrRouteInfo => OBJECT IDENTIFIER,
166             ipCidrRouteNextHopAS => Integer32,
167             ipCidrRouteMetric1 => Integer32,
168             ipCidrRouteMetric2 => Integer32,
169             ipCidrRouteMetric3 => Integer32,
170             ipCidrRouteMetric4 => Integer32,
171             ipCidrRouteMetric5 => Integer32,
172             ipCidrRouteStatus => RowStatus,
173             ipCidrRouteStatusString => String, # resolved enum
174             }
175              
176             =cut
177              
178             sub get_ip_cidr_route_table {
179 1     1 1 38444 my $session = shift;
180 1 50       104 Carp::croak "'$prefix' not initialized,"
181             unless $session->{$prefix}{__initialized};
182              
183             # stash for return values
184 0         0 my @route_tbl;
185              
186             my (
187 0         0 $ipCidrRouteDest, $ipCidrRouteMask,
188             $ipCidrRouteTos, $ipCidrRouteNextHop,
189             $ipCidrRouteIfIndex, $ipCidrRouteType,
190             $ipCidrRouteTypeString, $ipCidrRouteProto,
191             $ipCidrRouteProtoString, $ipCidrRouteAge,
192             $ipCidrRouteInfo, $ipCidrRouteNextHopAS,
193             $ipCidrRouteMetric1, $ipCidrRouteMetric2,
194             $ipCidrRouteMetric3, $ipCidrRouteMetric4,
195             $ipCidrRouteMetric5, $ipCidrRouteStatus,
196             $ipCidrRouteStatusString,
197             );
198              
199             #
200             # index is ipCidrRouteDest,ipCidrRouteMask,ipCidrRouteTos,ipCidrRouteNextHop
201             #
202 0         0 foreach
203 0         0 my $idx ( oid_lex_sort keys %{ $session->{$prefix}{ipCidrRouteDest} } )
204             {
205 0         0 $ipCidrRouteDest = $session->{$prefix}{ipCidrRouteDest}{$idx};
206 0         0 $ipCidrRouteMask = $session->{$prefix}{ipCidrRouteMask}{$idx};
207 0         0 $ipCidrRouteTos = $session->{$prefix}{ipCidrRouteTos}{$idx};
208 0         0 $ipCidrRouteNextHop = $session->{$prefix}{ipCidrRouteNextHop}{$idx};
209 0         0 $ipCidrRouteIfIndex = $session->{$prefix}{ipCidrRouteIfIndex}{$idx};
210              
211 0   0     0 $ipCidrRouteType = $session->{$prefix}{ipCidrRouteType}{$idx} || -1;
212 0   0     0 $ipCidrRouteTypeString = $type_enum{$ipCidrRouteType} || 'unknown';
213              
214 0   0     0 $ipCidrRouteProto = $session->{$prefix}{ipCidrRouteProto}{$idx} || -1;
215 0   0     0 $ipCidrRouteProtoString = $proto_enum{$ipCidrRouteProto} || 'unknown';
216              
217 0         0 $ipCidrRouteAge = $session->{$prefix}{ipCidrRouteAge}{$idx};
218 0         0 $ipCidrRouteInfo = $session->{$prefix}{ipCidrRouteInfo}{$idx};
219 0         0 $ipCidrRouteNextHopAS = $session->{$prefix}{ipCidrRouteNextHopAS}{$idx};
220 0         0 $ipCidrRouteMetric1 = $session->{$prefix}{ipCidrRouteMetric1}{$idx};
221 0         0 $ipCidrRouteMetric2 = $session->{$prefix}{ipCidrRouteMetric2}{$idx};
222 0         0 $ipCidrRouteMetric3 = $session->{$prefix}{ipCidrRouteMetric3}{$idx};
223 0         0 $ipCidrRouteMetric4 = $session->{$prefix}{ipCidrRouteMetric4}{$idx};
224 0         0 $ipCidrRouteMetric5 = $session->{$prefix}{ipCidrRouteMetric5}{$idx};
225              
226 0   0     0 $ipCidrRouteStatus = $session->{$prefix}{ipCidrRouteStatus}{$idx} || -1;
227 0   0     0 $ipCidrRouteStatusString = $status_enum{$ipCidrRouteStatus} || 'unknown';
228              
229 0         0 push @route_tbl,
230             {
231             ipCidrRouteDest => $ipCidrRouteDest,
232             ipCidrRouteMask => $ipCidrRouteMask,
233             ipCidrRouteTos => $ipCidrRouteTos,
234             ipCidrRouteNextHop => $ipCidrRouteNextHop,
235             ipCidrRouteIfIndex => $ipCidrRouteIfIndex,
236             ipCidrRouteType => $ipCidrRouteType,
237             ipCidrRouteTypeString => $ipCidrRouteTypeString,
238             ipCidrRouteProto => $ipCidrRouteProto,
239             ipCidrRouteProtoString => $ipCidrRouteProtoString,
240             ipCidrRouteAge => $ipCidrRouteAge,
241             ipCidrRouteInfo => $ipCidrRouteInfo,
242             ipCidrRouteNextHopAS => $ipCidrRouteNextHopAS,
243             ipCidrRouteMetric1 => $ipCidrRouteMetric1,
244             ipCidrRouteMetric2 => $ipCidrRouteMetric2,
245             ipCidrRouteMetric3 => $ipCidrRouteMetric3,
246             ipCidrRouteMetric4 => $ipCidrRouteMetric4,
247             ipCidrRouteMetric5 => $ipCidrRouteMetric5,
248             ipCidrRouteStatus => $ipCidrRouteStatus,
249             ipCidrRouteStatusString => $ipCidrRouteStatusString,
250             };
251             }
252              
253 0         0 return @route_tbl;
254             }
255              
256             =head1 INITIALIZATION
257              
258             =head2 B<< OBJ->_init($reload) >>
259              
260             Fetch the mib-II ipCidrRouteTable from the host. Don't call this method direct!
261              
262             =cut
263              
264             sub _init {
265 4     4   15169 my ( $session, $reload ) = @_;
266              
267 4 50 33     36 die "$prefix already initalized and reload not forced.\n"
268             if $session->{$prefix}{__initialized} && not $reload;
269              
270             # populate the object with needed mib values
271             #
272             # initialize the object for ipCidrRouteTable infos
273 4         13 _fetch_ip_cidr_route_tbl($session);
274 4 100       30 return if $session->error;
275              
276 2         15 return 1;
277             }
278              
279             =head1 PRIVATE METHODS
280              
281             Only for developers or maintainers.
282              
283             =head2 B<< _fetch_ip_cidr_route_tbl($session) >>
284              
285             Fetch the ipCidrRouteTable once during object initialization.
286              
287             =cut
288              
289             sub _fetch_ip_cidr_route_tbl {
290 4     4   8 my $session = shift;
291 4         6 my $result;
292              
293             # fetch the ipCidrRouteTable
294 4 100       29 $result = $session->get_table(
295             -baseoid => IP_CIDR_ROUTE_TABLE,
296              
297             # define callback if in nonblocking mode
298             $session->nonblocking ? ( -callback => \&_ip_cidr_route_tbl_cb ) : (),
299             );
300              
301 4 100       2010380 unless ( defined $result ) {
302              
303             # Net::SNMP looses sometimes error messages in nonblocking
304             # mode, so we save them in an extra buffer
305 2         18 my $err_msg = $session->error;
306 2 50       35 push_error( $session, "$prefix: $err_msg" ) if $err_msg;
307 2         57 return;
308             }
309              
310             # in nonblocking mode the callback will be called asynchronously
311 2 50       9 return 1 if $session->nonblocking;
312              
313             # ok we are in synchronous mode, call the result mangling function
314             # by hand
315 0         0 _ip_cidr_route_tbl_cb($session);
316              
317             }
318              
319             =head2 B<< _ip_cidr_route_tbl_cb($session) >>
320              
321             The callback for _fetch_ip_cidr_route_tbl().
322              
323             =cut
324              
325             sub _ip_cidr_route_tbl_cb {
326 2     2   2004905 my $session = shift;
327 2         7 my $vbl = $session->var_bind_list;
328              
329 2 50       39 unless ( defined $vbl ) {
330              
331             # Net::SNMP looses sometimes error messages in nonblocking
332             # mode, so we save them in an extra buffer
333 2         10 my $err_msg = $session->error;
334 2 50       30 push_error( $session, "$prefix: $err_msg" ) if $err_msg;
335 2         44 return;
336             }
337              
338             # build parallel hashes, keys are the index:
339             #
340             # ipCidrRouteDest,ipCidrRouteMask,ipCidrRouteTos,ipCidrRouteNextHop
341             #
342              
343 0           $session->{$prefix}{ipCidrRouteDest} =
344             idx2val( $vbl, IP_CIDR_ROUTE_DEST, undef, undef, );
345              
346 0           $session->{$prefix}{ipCidrRouteMask} =
347             idx2val( $vbl, IP_CIDR_ROUTE_MASK, undef, undef, );
348              
349 0           $session->{$prefix}{ipCidrRouteTos} =
350             idx2val( $vbl, IP_CIDR_ROUTE_TOS, undef, undef, );
351              
352 0           $session->{$prefix}{ipCidrRouteNextHop} =
353             idx2val( $vbl, IP_CIDR_ROUTE_NEXTHOP, undef, undef, );
354              
355 0           $session->{$prefix}{ipCidrRouteIfIndex} =
356             idx2val( $vbl, IP_CIDR_ROUTE_IFINDEX, undef, undef, );
357              
358 0           $session->{$prefix}{ipCidrRouteType} =
359             idx2val( $vbl, IP_CIDR_ROUTE_TYPE, undef, undef, );
360              
361 0           $session->{$prefix}{ipCidrRouteProto} =
362             idx2val( $vbl, IP_CIDR_ROUTE_PROTO, undef, undef, );
363              
364 0           $session->{$prefix}{ipCidrRouteAge} =
365             idx2val( $vbl, IP_CIDR_ROUTE_AGE, undef, undef, );
366              
367 0           $session->{$prefix}{ipCidrRouteInfo} =
368             idx2val( $vbl, IP_CIDR_ROUTE_INFO, undef, undef, );
369              
370 0           $session->{$prefix}{ipCidrRouteNextHopAS} =
371             idx2val( $vbl, IP_CIDR_ROUTE_NEXTHOPAS, undef, undef, );
372              
373 0           $session->{$prefix}{ipCidrRouteMetric1} =
374             idx2val( $vbl, IP_CIDR_ROUTE_METRIC1, undef, undef, );
375              
376 0           $session->{$prefix}{ipCidrRouteMetric2} =
377             idx2val( $vbl, IP_CIDR_ROUTE_METRIC2, undef, undef, );
378              
379 0           $session->{$prefix}{ipCidrRouteMetric3} =
380             idx2val( $vbl, IP_CIDR_ROUTE_METRIC3, undef, undef, );
381              
382 0           $session->{$prefix}{ipCidrRouteMetric4} =
383             idx2val( $vbl, IP_CIDR_ROUTE_METRIC4, undef, undef, );
384              
385 0           $session->{$prefix}{ipCidrRouteMetric5} =
386             idx2val( $vbl, IP_CIDR_ROUTE_METRIC5, undef, undef, );
387              
388 0           $session->{$prefix}{ipCidrRouteStatus} =
389             idx2val( $vbl, IP_CIDR_ROUTE_STATUS, undef, undef, );
390              
391 0           $session->{$prefix}{__initialized}++;
392             }
393              
394             unless ( caller() ) {
395             print "$prefix compiles and initializes successful.\n";
396             }
397              
398             =head1 SEE ALSO
399              
400             L<< Net::SNMP::Mixin >>
401              
402             =head1 REQUIREMENTS
403              
404             L<< Net::SNMP >>, L<< Net::SNMP::Mixin >>
405              
406             =head1 BUGS, PATCHES & FIXES
407              
408             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.
409              
410             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 .
411              
412             RT: http://rt.cpan.org/Public/Dist/Display.html?Name=Net-SNMP-Mixin-IpCidrRouteTable
413              
414              
415             =head1 AUTHOR
416              
417             Karl Gaissmaier
418              
419             =head1 COPYRIGHT & LICENSE
420              
421             Copyright 2011 Karl Gaissmaier, all rights reserved.
422              
423             This program is free software; you can redistribute it and/or modify it
424             under the same terms as Perl itself.
425              
426             =cut
427              
428             1;
429              
430             # vim: sw=2