⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmp.pm

📁 ucd-snmp源代码
💻 PM
📖 第 1 页 / 共 4 页
字号:
        }      }   }   $this->{UseLongNames} ||= $SNMP::use_long_names;   $this->{UseSprintValue} ||= $SNMP::use_sprint_value;   $this->{UseEnums} ||= $SNMP::use_enums;   $this->{UseNumeric} ||= $SNMP::use_numeric;   $this->{TimeStamp} ||= $SNMP::timestamp_vars;   SNMP::_update_session($this->{Version},		 $this->{Community},		 $this->{DestAddr},		 $this->{RemotePort},		 $this->{Retries},		 $this->{Timeout},		);}sub set {   my $this = shift;   my $vars = shift;   my $varbind_list_ref;   my $res = 0;   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/);     my $val = shift;     $varbind_list_ref = [[$tag, $iid, $val]];   }   my $cb = shift;   $res = SNMP::_set($this, $varbind_list_ref, $cb);}sub get {   my $this = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/);     $varbind_list_ref = [[$tag, $iid]];   }   my $cb = shift;   @res = SNMP::_get($this, $this->{RetryNoSuch}, $varbind_list_ref, $cb);   return(wantarray() ? @res : $res[0]);}sub fget {   my $this = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/);     $varbind_list_ref = [[$tag, $iid]];   }   my $cb = shift;   SNMP::_get($this, $this->{RetryNoSuch}, $varbind_list_ref, $cb);   foreach $varbind (@$varbind_list_ref) {     $sub = $this->{VarFormats}{$varbind->[$SNMP::Varbind::tag_f]} ||	 $this->{TypeFormats}{$varbind->[$SNMP::Varbind::type_f]};     &$sub($varbind) if defined $sub;     push(@res, $varbind->[$SNMP::Varbind::val_f]);   }   return(wantarray() ? @res : $res[0]);}sub getnext {   my $this = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/);     $varbind_list_ref = [[$tag, $iid]];   }   my $cb = shift;   @res = SNMP::_getnext($this, $varbind_list_ref, $cb);   return(wantarray() ? @res : $res[0]);}sub fgetnext {   my $this = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/);     $varbind_list_ref = [[$tag, $iid]];   }   my $cb = shift;   SNMP::_getnext($this, $varbind_list_ref, $cb);   foreach $varbind (@$varbind_list_ref) {     $sub = $this->{VarFormats}{$varbind->[$SNMP::Varbind::tag_f]} ||	 $this->{TypeFormats}{$varbind->[$SNMP::Varbind::type_f]};     &$sub($varbind) if defined $sub;     push(@res, $varbind->[$SNMP::Varbind::val_f]);   }   return(wantarray() ? @res : $res[0]);}sub getbulk {   my $this = shift;   my $nonrepeaters = shift;   my $maxrepetitions = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {     my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/);     $varbind_list_ref = [[$tag, $iid]];   }   my $cb = shift;   @res = SNMP::_getbulk($this, $nonrepeaters, $maxrepetitions, $varbind_list_ref, $cb);   return(wantarray() ? @res : $res[0]);}sub bulkwalk {   my $this = shift;   my $nonrepeaters = shift;   my $maxrepetitions = shift;   my $vars = shift;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {      $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {      $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {      $varbind_list_ref = [$vars];      $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   } else {      my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|\w+)\.?(.*)$/);      $varbind_list_ref = [[$tag, $iid]];   }   if (scalar @$varbind_list_ref == 0) {      $this->{ErrorNum} = SNMP::constant("SNMPERR_GENERR", 0);      $this->{ErrorStr} = "cannot bulkwalk() empty variable list";      return undef;   }   if (scalar @$varbind_list_ref < $nonrepeaters) {      $this->{ErrorNum} = SNMP::constant("SNMPERR_GENERR", 0);      $this->{ErrorStr} = "bulkwalk() needs at least $nonrepeaters varbinds";      return undef;   }   my $cb = shift;   @res = SNMP::_bulkwalk($this, $nonrepeaters, $maxrepetitions,						$varbind_list_ref, $cb);   # Return, in list context, a copy of the array of arrays of Varbind refs.   # In scalar context, return either a reference to the array of arrays of   # Varbind refs, or the request ID for an asynchronous bulkwalk.  This is   # a compromise between the getbulk()-ish return, and the more useful array   # of arrays of Varbinds return from the synchronous bulkwalk().   #   return @res if (wantarray());   return defined($cb) ? $res[0] : \@res;}%trap_type = (coldStart => 0, warmStart => 1, linkDown => 2, linkUp => 3,	      authFailure => 4, egpNeighborLoss => 5, specific => 6 );sub trap {# (v1) enterprise, agent, generic, specific, uptime, <vars># $sess->trap(enterprise=>'.1.3.6.1.4.1.2021', # or 'ucdavis' [default]#             agent => '127.0.0.1', # or 'localhost',[default 1st intf on host]#             generic => specific,  # can be omitted if 'specific' supplied#             specific => 5,        # can be omitted if 'generic' supplied#             uptime => 1234,       # default to localhost uptime (0 on win32)#             [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars#                                                          # always last# (v2) oid, uptime, <vars># $sess->trap(uptime => 1234,#             oid => 'snmpRisingAlarm',#             [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars#                                                          # always last#                                                          # always last   my $this = shift;   my $vars = pop if ref($_[$#_]); # last arg may be varbind or varlist   my %param = @_;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   }   if ($this->{Version} eq '1') {       my $enterprise = $param{enterprise} || 'ucdavis';       $enterprise = SNMP::translateObj($enterprise)	   unless $enterprise =~ /^[\.\d]+$/;       my $agent = $param{agent} || '';       my $generic = $param{generic} || 'specific';       $generic = $trap_type{$generic} || $generic;       my $uptime = $param{uptime} || SNMP::_sys_uptime();       my $specific = $param{specific} || 0;       @res = SNMP::_trapV1($this, $enterprise, $agent, $generic, $specific,			  $uptime, $varbind_list_ref);   } elsif  (($this->{Version} eq '2')|| ($this->{Version} eq '2c')) {       my $trap_oid = $param{oid} || $param{trapoid} || '.0.0';       my $uptime = $param{uptime} || SNMP::_sys_uptime();       @res = SNMP::_trapV2($this, $uptime, $trap_oid, $varbind_list_ref);   }   return(wantarray() ? @res : $res[0]);}sub inform {# (v3) oid, uptime, <vars># $sess->inform(uptime => 1234,#             oid => 'coldStart',#             [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars#                                                          # then callback                                                           # always last   my $this = shift;   my $vars;   my $cb;   $cb = pop if ref($_[$#_]) eq 'CODE'; # last arg may be code   $vars = pop if ref($_[$#_]); # varbind or varlist   my %param = @_;   my ($varbind_list_ref, @res);   if (ref($vars) =~ /SNMP::VarList/) {     $varbind_list_ref = $vars;   } elsif (ref($vars) =~ /SNMP::Varbind/) {     $varbind_list_ref = [$vars];   } elsif (ref($vars) =~ /ARRAY/) {     $varbind_list_ref = [$vars];     $varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;   }   my $trap_oid = $param{oid} || $param{trapoid};   my $uptime = $param{uptime} || SNMP::_sys_uptime();   if($this->{Version} eq '3') {     @res = SNMP::_inform($this, $uptime, $trap_oid, $varbind_list_ref, $cb);   } else {     warn("error:inform: This version doesn't support the command\n");   }   return(wantarray() ? @res : $res[0]);}package SNMP::TrapSession;@ISA = ('SNMP::Session');sub new {   my $type = shift;   # allow override of remote SNMP trap port   unless (grep(/RemotePort/, @_)) {       push(@_, 'RemotePort', 162); # push on new default for trap session   }   SNMP::Session::new($type, @_);}package SNMP::Varbind;$tag_f = 0;$iid_f = 1;$val_f = 2;$type_f = 3;$time_f = 4;sub new {   my $type = shift;   my $this = shift;   $this ||= [];   bless $this;}sub tag {  $_[0]->[$tag_f];}sub iid {  $_[0]->[$iid_f];}sub val {  $_[0]->[$val_f];}sub type {  $_[0]->[$type_f];}sub name {   if (defined($_[0]->[$iid_f]) && ($_[0]->[$iid_f] =~ m/^[0-9]+$/)) {      return $_[0]->[$tag_f] . "." . $_[0]->[$iid_f];   }   return $_[0]->[$tag_f];}sub stamp {   $_[0]->[$time_f];}sub fmt {    my $self = shift;    return $self->name . " = \"" . $self->val . "\" (" . $self->type . ")";}#sub DESTROY {#    print "SNMP::Varbind::DESTROY($_[0])\n";#}package SNMP::VarList;sub new {   my $type = shift;   my $this = [];   my $varb;   foreach $varb (@_) {     $varb = new SNMP::Varbind($varb) unless ref($varb) =~ /SNMP::Varbind/;     push(@{$this}, $varb);   }   bless $this;}#sub DESTROY {#    print "SNMP::VarList::DESTROY($_[0])\n";#}package SNMP::DEBUGGING;# controls info/debugging output from SNMP module and libsnmp# $SNMP::debugging == 1    =>   enables general info and warning output#                                (eqiv. to setting $SNMP::verbose)# $SNMP::debugging == 2    =>   enables do_debugging from libsnmp as well# $SNMP::debugging == 3    =>   enables packet_dump from libsnmp as wellsub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }sub FETCH { ${$_[0]}; }sub STORE {    $SNMP::verbose = $_[1];    SNMP::_set_debugging($_[1]>1);    $SNMP::dump_packet = ($_[1]>2);    ${$_[0]} = $_[1];}sub DELETE {    $SNMP::verbose = 0;    SNMP::_set_debugging(0);    $SNMP::dump_packet = 0;    ${$_[0]} = undef;}package SNMP::DEBUG_INTERNALS;		# Controls SNMP.xs debugging.sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }sub FETCH { ${$_[0]}; }sub STORE {    SNMP::_debug_internals($_[1]);    ${$_[0]} = $_[1];}sub DELETE {    SNMP::_debug_internals(0);    ${$_[0]} = undef;}package SNMP::DUMP_PACKET;# controls packet dump output from libsnmpsub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }sub FETCH { ${$_[0]}; }sub STORE { SNMP::_dump_packet($_[1]); ${$_[0]} = $_[1]; }sub DELETE { SNMP::_dump_packet(0); ${$_[0]} = 0; }package SNMP::MIB;sub TIEHASH {    bless {};}sub FETCH {    my $this = shift;    my $key = shift;    if (!defined $this->{$key}) {	tie(%{$this->{$key}}, SNMP::MIB::NODE, $key) or return undef;    }    $this->{$key};}sub STORE {    warn "STORE(@_) : write access to the MIB not implemented\n";}sub DELETE {    delete $_[0]->{$_[1]}; # just delete cache entry}sub FIRSTKEY { return '.1'; } # this should actually start at .0 but                              # because nodes are not stored in lexico                              # order in ucd-snmp node tree walk will                              # miss most of the treesub NEXTKEY { # this could be sped up by using an XS __get_next_oid maybe   my $node = $_[0]->FETCH($_[1])->{nextNode};   $node->{objectID};}sub EXISTS { exists $_[0]->{$_[1]} || $_[0]->FETCH($_[1]); }sub CLEAR { undef %{$_[0]}; } # clear the cachepackage SNMP::MIB::NODE;my %node_elements =    (     objectID => 0, # dotted decimal fully qualified OID

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -