App-Netsync

 view release on metacpan or  search on metacpan

lib/App/Netsync/SNMP.pm  view on Meta::CPAN

     'AuthPass'  => 'your password here',
     'PrivProto' => 'AES',
     'PrivPass'  => 'your key here',
 },[
     'IF-MIB','ENTITY-MIB',  # standard
     'CISCO-STACK-MIB',      # Cisco
     'FOUNDRY-SN-AGENT-MIB', # Brocade
     'SEMI-MIB',             # HP
 ]);

 my $ip      = '93.184.216.119';
 my $session = App::Netsync::SNMP::Session $ip;

 my $info1   = App::Netsync::SNMP::Info $ip;
 my $info2   = App::Netsync::SNMP::Info $session;

 my ($ifNames,$ifIIDs) = App::Netsync::SNMP::get1 ([
     ['.1.3.6.1.2.1.31.1.1.1.1' => 'ifName'],
     ['.1.3.6.1.2.1.2.2.1.2'    => 'ifDescr'],
 ],$session);

 App::Netsync::SNMP::set ('ifAlias',$_,'Vote for Pedro',$session) foreach @$ifIIDs;

=cut


use 5.006;
use strict;
use warnings FATAL => 'all';
use autodie; #XXX Is autodie adequate?

use File::Basename;
use Scalar::Util 'blessed';
use SNMP;
use SNMP::Info;
use version;

our ($SCRIPT,$VERSION);
our %config;

BEGIN {
    ($SCRIPT)  = fileparse ($0,"\.[^.]*");
    ($VERSION) = version->declare('v4.0.0');
}

INIT {
    $config{'AuthPass'}        = undef;
    $config{'AuthProto'}       = 'MD5';
    $config{'Community'}       = 'public';
    $config{'Context'}         = undef;
    $config{'ContextEngineId'} = undef;
    $config{'DestHost'}        = undef;
    $config{'PrivPass'}        = undef;
    $config{'PrivProto'}       = 'DES';
    $config{'RemotePort'}      = 161;
    $config{'Retries'}         = 5;
    $config{'RetryNoSuch'}     = 0;
    $config{'SecEngineId'}     = undef;
    $config{'SecLevel'}        = 'noAuthNoPriv';
    $config{'SecName'}         = 'initial';
    $config{'Timeout'}         = 1000000;
    $config{'Version'}         = 3;

    $config{'MIBdir'}          = '/usr/share/'.$SCRIPT.'/mib';
    SNMP::addMibDirs($config{'MIBdir'});
}


=head1 METHODS

=head2 configure

configure the operating environment

B<Arguments>

I<( \%environment , \@MIBs )>

=over 3

=item environment

key-value pairs of environment configurations

B<Available Environment Settings>

=over 4

=item MIBdir

the location of necessary MIBs

default: F</usr/share/E<lt>script nameE<gt>/mib>

=back

See SNMP::Session documentation for more acceptable settings.

=item MIBs

a list of MIBs to load

=back

=cut

sub configure {
    warn 'too few arguments'  if @_ < 2;
    warn 'too many arguments' if @_ > 2;
    my ($environment,$MIBs) = @_;

    $config{$_} = $environment->{$_} foreach keys %$environment;

    my $success = 1;
    foreach my $MIB (@$MIBs) {
        if (defined $MIB) {
            $success = 0 unless defined SNMP::loadModules($MIB);
        }
    }
    SNMP::initMib();

    $config{'ContextEngineId'} //= $config{'SecEngineId'};
    unless (($config{'Version'} < 3) or
            ($config{'SecLevel'} eq 'noAuthNoPriv') or
            ($config{'SecLevel'} eq 'authNoPriv' and defined $config{'AuthPass'}) or
            (defined $config{'AuthPass'} and defined $config{'PrivPass'})) {
        warn 'SNMPv3 configuration is inadequate.';
        $success = 0;
    }
    return $success;
}


=head2 Session

returns an SNMP::Session object.

I<Note: configure needs to be run first!>

B<Arguments>

I<( $ip )>

=over 3

=item ip

an IP address to connect to

=back

=cut

sub Session {
    warn 'too few arguments'  if @_ < 1;
    warn 'too many arguments' if @_ > 1;
    my ($ip) = @_;

    return SNMP::Session->new(
        'AuthPass'        => $config{'AuthPass'},
        'AuthProto'       => $config{'AuthProto'},
        'Community'       => $config{'Community'},
        'Context'         => $config{'Context'},
        'ContextEngineId' => $config{'ContextEngineId'},
        'DestHost'        => $ip,
        'PrivPass'        => $config{'PrivPass'},
        'PrivProto'       => $config{'PrivProto'},
        'RemotePort'      => $config{'RemotePort'},
        'Retries'         => $config{'Retries'},
        'RetryNoSuch'     => $config{'RetryNoSuch'},
        'SecEngineId'     => $config{'SecEngineId'},
        'SecLevel'        => $config{'SecLevel'},
        'SecName'         => $config{'SecName'},
        'Timeout'         => $config{'Timeout'},
        'Version'         => $config{'Version'},
    );
}


=head2 Info

returns an SNMP::Info object

I<Note: configure needs to be run first!>

B<Arguments>

I<( $ip )>

=over 3

=item ip

an IP address to connect to OR an SNMP::Session

=back

I<Note: The following snippets are equivalent:>

=over 3

=item C<App::Netsync::SNMP::Info $ip;>

=item C<App::Netsync::SNMP::Info App::Netsync::SNMP::Session $ip;>

=back

=cut

sub Info {
    warn 'too few arguments'  if @_ < 1;
    warn 'too many arguments' if @_ > 1;
    my ($ip) = @_;

    my $session = Session $ip;
    my $info = SNMP::Info->new(
        'AutoSpecify' => 1,
        'Session'     => $session,
    );
    return ($session,$info);
}


=head2 get1

attempt to retrieve an OID from a provided list, stopping on success

B<Arguments>

I<( \@OIDs , $ip )>

=over 3

=item OIDs

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.929 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )