File: Interfaces.pm

package info (click to toggle)
libnet-snmp-interfaces-perl 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: perl: 467; makefile: 2
file content (391 lines) | stat: -rw-r--r-- 9,817 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#*****************************************************************************
#*                                                                           *
#*                          Gellyfish Software                               *
#*                                                                           *
#*                                                                           *
#*****************************************************************************
#*                                                                           *
#*      PROGRAM     :  Net::SNMP::Interfaces.                                *
#*                                                                           *
#*      AUTHOR      :  JNS                                                   *
#*                                                                           *
#*      DESCRIPTION :  Simple SNMP stuff for Interfaces.                     *
#*                                                                           *
#*                                                                           *
#*****************************************************************************

package Net::SNMP::Interfaces;

=head1 NAME

Net::SNMP::Interfaces - provide simple methods to gain interface data via

SNMP

=head1 SYNOPSIS

    use Net::SNMP::Interfaces;

    my $interfaces = Net::SNMP::Interfaces->new(Hostname => 'localhost',
                                                Community => 'public' );

    my @ifnames = $interfaces->all_interfaces();

=head1 DESCRIPTION

Net::SNMP::Interfaces aims to provide simple object methods to obtain
information about a host's network interfaces ( be it a server a router
or whatever ).  The motivation was largely to allow a programmer to use
SNMP to obtain this information without needing to know a great deal
about the gory details.

The module uses Net::SNMP under the hood to do the dirty work although
the user shouldn't have to worry about that ( the Net::SNMP object is
available though for those who might feel the need ).

The actual details for a particular interface are obtained from the methods
of Net::SNMP::Interfaces::Details - objects of which type can be obtained
for the methods all_interfaces() and interface().

Of course the simpler interface has its limitations and there may well
be things that you would like to do which you cant do with this module -
in which case I would recommend that you get a good book on SNMP and
use Net::SNMP :)

The module uses blocking SNMP requests at the current time so if some
of the methods are taking too long you may want to time them out
yourself using alarm().

=cut



use strict;
use Net::SNMP;
use Carp;


use Net::SNMP::Interfaces::Details;

use vars qw(
            @ISA
            $VERSION
            $AUTOLOAD
           );
             


($VERSION) = q$Revision: 1.4 $ =~ /([\d.]+)/;

=head2 METHODS

=over

=item new(  HASH %args )

The constructor of the class. It takes several arguments that are passed
to Net::SNMP :

=over

=item Hostname

The name of the host which you want to connect to. Defaults to 'localhost'.

=item Community

The SNMP community string which you want to use for this session.  The default
is 'public'.

=item Port

The UDP port that the SNMP service is listening on.  The default is 161.

=item Version

The SNMP version (as described in the L<Net::SNMP> documentation) to be
used.  The default is 'snmpv1'.  Support for SNMPv3 is currently somewhat
limited.

=back

There is a also an optional argument 'RaiseError' which determines
the behaviour of the module in the event there is an error while creating
the SNMP.  Normally new() will return undef if there was an error but if
RaiseError is set to a true value it will die() printing the error string
to STDERR.  If this is not set and an error occurs undef will be return
and the variable $Net::SNMP::Interfaces::error will contain the test of
the error.

Because the interfaces are discovered in the constructor, if the module
is to be used in a long running program to monitor a host where 
interfaces might be added or removed it is recommended that the object
returned by new() is periodically destroyed and a new one constructed.

=cut


sub new
{
  my ( $proto , %args ) = @_;
  
  my $self = {};

  $self->{_hostname}  = $args{Hostname}  || 'localhost';
  $self->{_community} = $args{Community} || 'public';
  $self->{_port}      = $args{Port}      || 161;
  $self->{_version}   = $args{Version}   || 'snmpv1',
  $self->{_raise}     = $args{RaiseError} || 0;


  my ($session, $error) = Net::SNMP->session(
                                              -hostname  => $self->{_hostname},
                                              -community => $self->{_community},
                                              -port      => $self->{_port},
                                              -version   => $self->{_version},
                                            );

  if (!defined($session)) 
  {
     if ( $self->{_raise} )
     {
       croak sprintf("%s: %s", __PACKAGE__, $error);
     }
     else
     {
       $Net::SNMP::Interfaces::error = $error;
       return undef;
     }
  }

  $self->{_snmp_session} = $session;

  my $ifIndex = '1.3.6.1.2.1.2.2.1.1';
  my $ifDescr = '1.3.6.1.2.1.2.2.1.2';

  my $response;

  if (!defined($response = $session->get_table($ifIndex))) 
  {
     if ( $self->{_raise} )
     {
       $session->close;
       croak sprintf("%s: %s",__PACKAGE__, $session->error);
     }
     else
     {
       $Net::SNMP::Interfaces::error = $session->error();
       return undef;
     }
  }


  foreach my $index ( values %{$response} )
  {
    my $this_desc = "$ifDescr.$index"; 

    my $description;

    if ( defined( $description = $session->get_request($this_desc)) )
    {
      $self->{_desc2index}->{$description->{$this_desc}} = $index;
      $self->{_index2desc}->{$index} = $description->{$this_desc};
    }
    else
    {
      $self->{_lasterror} = $session->error();
    }
  }  

  return bless $self, $proto;
}

=item if_names()

Returns a list of the interface names.

=cut

sub if_names
{
   my ( $self ) = @_;

   return keys %{$self->{_desc2index}};
}


=item if_indices()

Returns a list of the indices of the interfaces - this probably shouldn't
be necessary but is here for completeness anyway.  If you don't know what
the index is for you are safe to ignore this.

=cut

sub if_indices
{
   my ( $self ) = @_;

   return keys %{$self->{_index2desc}};
}

=item  error()

Returns the text of the last Net::SNMP error.  This method only makes sense
if the previous method call indicated an error by a false return.

=cut

sub error
{
  my ($self ) = @_;

  return $self->{_lasterror} || $self->session()->error();
}

=item session()

Returns the Net::SNMP session object for this instance. Or a false value
if there is no open session.  This might be used to call methods on the
Net::SNMP object if some facility is needed that isn't supplied by this
module.

=cut

sub session
{
  my ( $self ) = @_;

  return exists $self->{_snmp_session} ? $self->{_snmp_session} : undef;
}

=item all_interfaces()

Returns a list of Net::SNMP::Interface::Details objects corresponding to
the interfaces discovered on this host.  In scalar context it will return
a reference to an array.

=cut

sub all_interfaces
{
  my ( $self ) = @_;

  my @interfaces;

  
  for my $index ( sort $self->if_indices() )
  {
    my %args = (
                 Index   => $index,
                 Name    => $self->{_index2desc}->{$index},
                 Session => $self->session()
               );

    push @interfaces, Net::SNMP::Interfaces::Details->new(%args);
  }

  return wantarray ? @interfaces : \@interfaces;
}

=item interface( SCALAR $name )

Returns a Net::SNMP::Interfaces::Details object for the named interface.
Returns undef if the supplied name is not a known interface.

=back

=cut

sub interface
{
  my ( $self, $name ) = @_;

  my $index = $self->{_desc2index}->{$name};

  if ( defined $index )
  {
    return Net::SNMP::Interfaces::Details->new(
                                               Name    => $name,
                                               Index   => $index,
                                               Session => $self->session()
                                              );
  }
  else
  {
    return undef;
  }
}

=for pod

In addition to the methods above, you can also use the methods from
Net::SNMP::Interfaces::Details but with the addition of the interface
name as an argument. e.g:

      $in_octs = $self->ifInOctets('eth0');

Please see the documentation for Net::SNMP::Interfaces::Details for more
on these methods.

=cut

sub AUTOLOAD
{
  my ( $self, $name ) = @_;

  return if $AUTOLOAD =~ /DESTROY$/;

  croak "No name" unless $name;
  return undef unless exists $self->{_desc2index}->{$name};

  my ($meth)  = $AUTOLOAD =~ /::([^:]+)$/;

  no strict 'refs';

  *{$AUTOLOAD} = sub {
                        my ( $self, $name ) = @_;
                        return $self->interface($name)->$meth() ;
                     };

  goto &{$AUTOLOAD};

}


sub DESTROY
{
  my ( $self ) = @_;

  $self->session()->close();
}

1;
__END__

=head1 SUPPORT

The code is host on Github at 

     https://github.com/jonathanstowe/Net-SNMP-Interfaces

Pull requests are welcome, especially if they help support SNMPv3 better.

Email to <bug-Net-SNMP-Interfaces@rt.cpan.org> is preferred for non-patch
requests.

=head1 AUTHOR

Jonathan Stowe <jns@gellyfish.co.uk>

=head1 COPYRIGHT

Copyright (c) Jonathan Stowe 2000 -2013.  All rights reserved.  This is free
software it can be ditributed and/or modified under the same terms as
Perl itself.

=head1 SEE ALSO

perl(1), L<Net::SNMP>, L<Net::SNMP::Interfaces::Details>.

=cut