Interfaces.pm

filter: Statementstmt 23.5% 20 / 85 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 37.5% 6 / 16 Podpod 100% 7 / 7 totaltotal 22.0% 33 / 150 CC 20 SCAR 40.5
CC
20
Cov
17%
new
156.0
AUTOLOAD
13.8
all_interfaces
12.0
CRAP 57.5
↑ Summary Interfaces/Details.pm »
1 #*****************************************************************************
2 #* *
3 #* Gellyfish Software *
4 #* *
5 #* *
6 #*****************************************************************************
7 #* *
8 #* PROGRAM : Net::SNMP::Interfaces. *
9 #* *
10 #* AUTHOR : JNS *
11 #* *
12 #* DESCRIPTION : Simple SNMP stuff for Interfaces. *
13 #* *
14 #* *
15 #*****************************************************************************
16  
17 package Net::SNMP::Interfaces;
18  
19 =head1 NAME
20  
21 Net::SNMP::Interfaces - provide simple methods to gain interface data via
22  
23 SNMP
24  
25 =head1 SYNOPSIS
26  
27 use Net::SNMP::Interfaces;
28  
29 my $interfaces = Net::SNMP::Interfaces->new(Hostname => 'localhost',
30 Community => 'public' );
31  
32 my @ifnames = $interfaces->all_interfaces();
33  
34 =head1 DESCRIPTION
35  
36 Net::SNMP::Interfaces aims to provide simple object methods to obtain
37 information about a host's network interfaces ( be it a server a router
38 or whatever ). The motivation was largely to allow a programmer to use
39 SNMP to obtain this information without needing to know a great deal
40 about the gory details.
41  
42 The module uses Net::SNMP under the hood to do the dirty work although
43 the user shouldn't have to worry about that ( the Net::SNMP object is
44 available though for those who might feel the need ).
45  
46 The actual details for a particular interface are obtained from the methods
47 of Net::SNMP::Interfaces::Details - objects of which type can be obtained
48 for the methods all_interfaces() and interface().
49  
50 Of course the simpler interface has its limitations and there may well
51 be things that you would like to do which you cant do with this module -
52 in which case I would recommend that you get a good book on SNMP and
53 use Net::SNMP :)
54  
55 The module uses blocking SNMP requests at the current time so if some
56 of the methods are taking too long you may want to time them out
57 yourself using alarm().
58  
59 =cut
60  
61  
62  
63 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Net::SNMP;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67  
68 1 ▶use Net::SNMP::Interfaces::Details;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
71 @ISA
72 $VERSION
73 $AUTOLOAD
74 1 );
75
76  
77  
78 1 ($VERSION) = q$Revision: 1.4 $ =~ /([\d.]+)/;
79  
80 =head2 METHODS
81  
82 =over
83  
84 =item new( HASH %args )
85  
86 The constructor of the class. It takes several arguments that are passed
87 to Net::SNMP :
88  
89 =over
90  
91 =item Hostname
92  
93 The name of the host which you want to connect to. Defaults to 'localhost'.
94  
95 =item Community
96  
97 The SNMP community string which you want to use for this session. The default
98 is 'public'.
99  
100 =item Port
101  
102 The UDP port that the SNMP service is listening on. The default is 161.
103  
104 =item Version
105  
106 The SNMP version (as described in the L<Net::SNMP> documentation) to be
107 used. The default is 'snmpv1'. Support for SNMPv3 is currently somehwat
108 limited.
109  
110 =back
111  
112 There is a also an optional argument 'RaiseError' which determines
113 the behaviour of the module in the event there is an error while creating
114 the SNMP. Normally new() will return undef if there was an error but if
115 RaiseError is set to a true value it will die() printing the error string
116 to STDERR. If this is not set and an error occurs undef will be return
117 and the variable $Net::SNMP::Interfaces::error will contain the test of
118 the error.
119  
120 Because the interfaces are discovered in the constructor, if the module
121 is to be used in a long running program to monitor a host where
122 interfaces might be added or removed it is recommended that the object
123 returned by new() is periodically destroyed and a new one constructed.
124  
125 =cut
126  
127  
128 sub new
129 0 ▶{
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
130 0 my ( $proto , %args ) = @_;
131
132 0 my $self = {};
133  
134 0 ▶ $self->{_hostname} = $args{Hostname} || 'localhost';
Condition0%0/2
$args{'Hostname'} || 'localhost'
l!l
00
MC/DC0%0/1
$args{'Hostname'} || 'localhost'
$args{'Hostname'}
Truth table0%0/2
A
A= $args{'Hostname'}
Aresult
11
00
135 0 ▶ $self->{_community} = $args{Community} || 'public';
Condition0%0/2
$args{'Community'} || 'public'
l!l
00
MC/DC0%0/1
$args{'Community'} || 'public'
$args{'Community'}
Truth table0%0/2
A
A= $args{'Community'}
Aresult
11
00
136 0 ▶ $self->{_port} = $args{Port} || 161;
Condition0%0/2
$args{'Port'} || 161
l!l
00
MC/DC0%0/1
$args{'Port'} || 161
$args{'Port'}
Truth table0%0/2
A
A= $args{'Port'}
Aresult
11
00
137 $self->{_version} = $args{Version} || 'snmpv1',
138 0 ▶ $self->{_raise} = $args{RaiseError} || 0;
Condition0%0/4
$args{'Version'} || 'snmpv1'
l!l
00
$args{'RaiseError'} || 0
l!l
00
MC/DC0%0/1
$args{'Version'} || 'snmpv1'
$args{'Version'}
MC/DC0%0/1
$args{'RaiseError'} || 0
$args{'RaiseError'}
Truth table0%0/2
A
A= $args{'Version'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'RaiseError'}
Aresult
11
00
139  
140  
141 my ($session, $error) = Net::SNMP->session(
142 -hostname => $self->{_hostname},
143 -community => $self->{_community},
144 -port => $self->{_port},
145 -version => $self->{_version},
146 0 );
147  
148 0 ▶ if (!defined($session))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session) 0 0 0
149 {
150 0 ▶ if ( $self->{_raise} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_raise'}) { } 0 0 0
151 {
152 0 croak sprintf("%s: %s", __PACKAGE__, $error);
153 }
154 else
155 {
156 0 $Net::SNMP::Interfaces::error = $error;
157 0 return undef;
158 }
159 }
160  
161 0 $self->{_snmp_session} = $session;
162  
163 0 my $ifIndex = '1.3.6.1.2.1.2.2.1.1';
164 0 my $ifDescr = '1.3.6.1.2.1.2.2.1.2';
165  
166 0 my $response;
167  
168 0 ▶ if (!defined($response = $session->get_table($ifIndex)))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($response = $session->get_table($ifIndex))) 0 0 0
169 {
170 0 ▶ if ( $self->{_raise} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_raise'}) { } 0 0 0
171 {
172 0 $session->close;
173 0 croak sprintf("%s: %s",__PACKAGE__, $session->error);
174 }
175 else
176 {
177 0 $Net::SNMP::Interfaces::error = $session->error();
178 0 return undef;
179 }
180 }
181  
182  
183 0 foreach my $index ( values %{$response} )
184 {
185 0 my $this_desc = "$ifDescr.$index";
186  
187 0 my $description;
188  
189 0 ▶ if ( defined( $description = $session->get_request($this_desc)) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($description = $session->get_request($this_desc))) { } 0 0 0
190 {
191 0 $self->{_desc2index}->{$description->{$this_desc}} = $index;
192 0 $self->{_index2desc}->{$index} = $description->{$this_desc};
193 }
194 else
195 {
196 0 $self->{_lasterror} = $session->error();
197 }
198 }
199  
200 0 return bless $self, $proto;
201 }
202  
203 =item if_names()
204  
205 Returns a list of the interface names.
206  
207 =cut
208  
209 sub if_names
210 0 ▶{
Subroutine if_names: not called CC=1 SCAR=6.9
Pod documented
211 0 my ( $self ) = @_;
212  
213 0 return keys %{$self->{_desc2index}};
214 }
215  
216  
217 =item if_indices()
218  
219 Returns a list of the indices of the interfaces - this probably shouldn't
220 be necessary but is here for completeness anyway. If you dont know what
221 the index is for you are safe to ignore this.
222  
223 =cut
224  
225 sub if_indices
226 0 ▶{
Subroutine if_indices: not called CC=1 SCAR=6.9
Pod documented
227 0 my ( $self ) = @_;
228  
229 0 return keys %{$self->{_index2desc}};
230 }
231  
232 =item error()
233  
234 Returns the text of the last Net::SNMP error. This method only makes sense
235 if the previous method call indicated an error by a false return.
236  
237 =cut
238  
239 sub error
240 0 ▶{
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
241 0 my ($self ) = @_;
242  
243 0 ▶ return $self->{_lasterror} || $self->session()->error();
Condition0%0/3
$self->{'_lasterror'} || $self->session->error
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_lasterror'} || $self->session->error
$self->{'_lasterror'} $self->session->error
Truth table0%0/3
A || B
A= $self->{'_lasterror'}B= $self->session->error
ABresult
1X1
011
000
244 }
245  
246 =item session()
247  
248 Returns the Net::SNMP session object for this instance. Or a false value
249 if there is no open session. This might be used to call methods on the
250 Net::SNMP object if some facility is needed that isnt supplied by this
251 module.
252  
253 =cut
254  
255 sub session
256 0 ▶{
Subroutine session: not called CC=2 SCAR=17.9
Pod documented
257 0 my ( $self ) = @_;
258  
259 0 ▶ return exists $self->{_snmp_session} ? $self->{_snmp_session} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_snmp_session'} ? : 0 0 0
260 }
261  
262 =item all_interfaces()
263  
264 Returns a list of Net::SNMP::Interface::Details objects corresponding to
265 the interfaces discovered on this host. In scalar context it will return
266 a reference to an array.
267  
268 =cut
269  
270 sub all_interfaces
271 0 ▶{
Subroutine all_interfaces: not called CC=3 SCAR=24.8
Pod documented
272 0 my ( $self ) = @_;
273  
274 0 my @interfaces;
275  
276
277 0 for my $index ( sort $self->if_indices() )
278 {
279 my %args = (
280 Index => $index,
281 0 Name => $self->{_index2desc}->{$index},
282 Session => $self->session()
283 );
284  
285 0 push @interfaces, Net::SNMP::Interfaces::Details->new(%args);
286 }
287  
288 0 ▶ return wantarray ? @interfaces : \@interfaces;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
289 }
290  
291 =item interface( SCALAR $name )
292  
293 Returns a Net::SNMP::Interfaces::Details object for the named interface.
294 Returns undef if the supplied name is not a known interface.
295  
296 =cut
297  
298 sub interface
299 0 ▶{
Subroutine interface: not called CC=2 SCAR=17.9
Pod documented
300 0 my ( $self, $name ) = @_;
301  
302 0 my $index = $self->{_desc2index}->{$name};
303  
304 0 ▶ if ( defined $index )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) { } 0 0 0
305 {
306 0 return Net::SNMP::Interfaces::Details->new(
307 Name => $name,
308 Index => $index,
309 Session => $self->session()
310 );
311 }
312 else
313 {
314 0 return undef;
315 }
316 }
317  
318 =for pod
319  
320 In addition to the methods above, you can also use the methods from
321 Net::SNMP::Interfaces::Details but with the addition of the interface
322 name as an argument. e.g:
323  
324 $in_octs = $self->ifInOctets('eth0');
325  
326 Please see the documentation for Net::SNMP::Interfaces::Details for more
327 on these methods.
328  
329 =cut
330  
331 sub AUTOLOAD
332 0 ▶{
Subroutine AUTOLOAD: not called CC=4 SCAR=26.3
333 0 my ( $self, $name ) = @_;
334  
335 0 ▶ return if $AUTOLOAD =~ /DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::SNMP::Interfaces::AUTOLOAD =~ /DESTROY$/ 0 0 0
336  
337 0 ▶ croak "No name" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
338 0 ▶ return undef unless exists $self->{_desc2index}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_desc2index"}{$name} 0 0 0
339  
340 0 my ($meth) = $AUTOLOAD =~ /::([^:]+)$/;
341  
342 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
343  
344 0 ▶ *{$AUTOLOAD} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
345 0 my ( $self, $name ) = @_;
346 0 return $self->interface($name)->$meth() ;
347 0 };
348  
349 0 goto &{$AUTOLOAD};
350  
351 }
352  
353  
354 sub DESTROY
355 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
356 0 my ( $self ) = @_;
357  
358 0 $self->session()->close();
359 }
360  
361 1 1;
362 __END__
↑ Summary Interfaces/Details.pm »