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

📄 snmp.pm

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 PM
📖 第 1 页 / 共 5 页
字号:
do SNMP GETNEXT like getnext and format the values accordingthe handlers specified in $sess->{VarFormats} and$sess->{TypeFormats}=item $sess->set(E<lt>varsE<gt> [,E<lt>callbackE<gt>])do SNMP SET, multiple <vars> formats accepted.the value field in all <vars> formats must be in a canonicalformat (i.e., well known format) to ensure unambiguoustranslation to SNMP MIB data value (see discussion ofcanonical value format <vars> description section),returns snmp_errno. If <callback> supplied methodwill operate asyncronously=item $sess->getbulk(E<lt>non-repeatersE<gt>, E<lt>max-repeatersE<gt>, E<lt>varsE<gt>)do an SNMP GETBULK, from the list of Varbinds, the singlenext lexico instance is fetched for the first n Varbindsas defined by <non-repeaters>. For remaining Varbinds,the m lexico instances are retrieved each of the remainingVarbinds, where m is <max-repeaters>.=item $sess->bulkwalk(E<lt>non-repeatersE<gt>, E<lt>max-repeatersE<gt>, E<lt>varsE<gt> [,E<lt>callbackE<gt>])Do a "bulkwalk" of the list of Varbinds.  This is done bysending a GETBULK request (see getbulk() above) for theVarbinds.  For each requested variable, the response isexamined to see if the next lexico instance has left therequested sub-tree.  Any further instances returned forthis variable are ignored, and the walk for that sub-treeis considered complete.If any sub-trees were not completed when the end of theresponses is reached, another request is composed, consistingof the remaining variables.  This process is repeated untilall sub-trees have been completed, or too many packets havebeen exchanged (to avoid loops).The bulkwalk() method returns an array containing an array ofVarbinds, one for each requested variable, in the order of thevariable requests.  Upon error, bulkwalk() returns undef andsets $sess->ErrorStr and $sess->ErrorNum.  If a callback issupplied, bulkwalk() returns the SNMP request id, and returnsimmediately.  The callback will be called with the suppliedargument list and the returned variables list.Note: Because the client must "discover" that the tree iscomplete by comparing the returned variables with those thatwere requested, there is a potential "gotcha" when using themax-repeaters value.  Consider the following code to print alist of interfaces and byte counts:    $numInts = $sess->get('ifNumber.0');    ($desc, $in, $out) = $sess->bulkwalk(0, $numInts,		  [['ifDescr'], ['ifInOctets'], ['ifOutOctets']]);    for $i (0..($numInts - 1)) {        printf "Interface %4s: %s inOctets, %s outOctets\n",                  $$desc[$i]->val, $$in[$i]->val, $$out[$i]->val;    }This code will produce *two* requests to the agent -- the firstto get the interface values, and the second to discover that allthe information was in the first packet.  To get around this,use '$numInts + 1' for the max_repeaters value.  This asks theagent to include one additional (unrelated) variable that signalsthe end of the sub-tree, allowing bulkwalk() to determine thatthe request is complete.=item $results = $sess->gettable(E<lt>TABLE OIDE<gt>, E<lt>OPTIONS<gt>)This will retrieve an entire table of data and return a hash referenceto that data.  The returned hash reference will have indexes of theOID suffixes for the index data as the key.  The value for each entrywill be another hash containing the data for a given row.  The keys tothat hash will be the column names, and the values will be the data.Example:  #!/usr/bin/perl  use SNMP;  use Data::Dumper;  my $s = new SNMP::Session(DestHost => 'localhost');  print Dumper($s->gettable('ifTable'));On my machine produces:  $VAR1 = {            '6' => {                     'ifMtu' => '1500',                     'ifPhysAddress' => 'PV',                     # ...                     'ifInUnknownProtos' => '0'                   },            '4' => {                     'ifMtu' => '1480',                     'ifPhysAddress' => '',                     # ...                     'ifInUnknownProtos' => '0'                   },            # ...           };By default, it will try to do as optimized retrieval as possible.It'll request multiple columns at once, and use GETBULK if possible.A few options may be specified by passing in an I<OPTIONS> hashcontaining various parameters:=over=item noindexes => 1Instructs the code not to parse the indexes and place the results inthe second hash.  If you don't need the index data, this will befaster.=item columns => [ colname1, ... ]This specifies which columns to collect.  By default, it will try tocollect all the columns defined in the MIB table.=item repeat => I<COUNT>Specifies a GETBULK repeat I<COUNT>.  IE, it will request this manyvarbinds back per column when using the GETBULK operation.  Shorteningthis will mean smaller packets which may help going through somesystems.  By default, this value is calculated and attepmts to guessat what will fit all the results into 1000 bytes.  This calculation isfairly safe, hopefully, but you can either raise or lower the numberusing this option if desired.  In lossy networks, you want to makesure that the packets don't get fragmented and lowering this value isone way to help that.=item nogetbulk => 1Force the use of GETNEXT rather than GETBULK.  (always true forSNMPv1, as it doesn't have GETBULK anyway).  Some agents are greatimplementers of GETBULK and this allows you to force the use ofGETNEXT oprations instead.=item callback => \&subroutine=item callback => [\&subroutine, optarg1, optarg2, ...]If a callback is specified, gettable will return quickly withoutreturning results.  When the results are finally retrieved thecallback subroutine will be called (see the other sections definingcallback behaviour and how to make use of SNMP::MainLoop which isrequired fro this to work).  An additional argument of the normal hashresult will be added to the callback subroutine arguments.Note 1: internally, the gettable function uses it's own callbackswhich are passed to getnext/getbulk as appropriate.Note 2: callback support is only available in the SNMP module version5.04 and above.  To test for this in code intending to support bothversions prior to 5.04 and and 5.04 and up, the following should work:  if ($response = $sess->gettable('ifTable', callback => \&my_sub)) {      # got a response, gettable doesn't support callback      my_sub($response);      $no_mainloop = 1;  }Deciding on whether to use SNMP::MainLoop is left as an excersize tothe reader since it depends on whether your code uses other callbacksas well.=back=back=head1 SNMP::TrapSession$sess = new SNMP::Session(DestHost => 'host', ...)supports all applicable fields from SNMP::Session(see above)=head2 SNMP::TrapSession methods=over=item $sess->trap(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',[dflt 1st intf on host]                generic => specific,  # can be omitted if 'specific' supplied                specific => 5,        # can be omitted if 'generic' supplied                uptime => 1234,       # dflt to localhost uptime (0 on win32)                [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars                                                             # always last=item trap(oid, uptime, <vars>) - v2 format    $sess->trap(oid => 'snmpRisingAlarm',                uptime => 1234,                [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars                                                             # always last=back=head1 Acceptable variable formats:<vars> may be one of the following forms:=over=item SNMP::VarListrepresents an array of MIB objects to get or set,implemented as a blessed reference to an array ofSNMP::Varbinds, (e.g., [<varbind1>, <varbind2>, ...])=item SNMP::Varbindrepresents a single MIB object to get or set, implemented asa blessed reference to a 4 element array;[<obj>, <iid>, <val>, <type>].=over=item <obj>one of the following forms:=over=item 1)leaf identifier (e.g., 'sysDescr') assumed to beunique for practical purposes=item 2)fully qualified identifier (e.g.,'.iso.org.dod.internet.mgmt.mib-2.system.sysDescr')=item 3)fully qualified, dotted-decimal, numeric OID (e.g.,'.1.3.6.1.2.1.1.1')=back=item <iid>the dotted-decimal, instance identifier. forscalar MIB objects use '0'=item <val>the SNMP data value retrieved from or being setto the agents MIB. for (f)get(next) operations<val> may have a variety of formats as determined bysession and package settings. However for setoperations the <val> format must be canonical toensure unambiguous translation. The canonical formsare as follows:=over=item OBJECTIDdotted-decimal (e.g., .1.3.6.1.2.1.1.1)=item OCTETSTRperl scalar containing octets=item INTEGERdecimal signed integer (or enum)=item NETADDRdotted-decimal=item IPADDRdotted-decimal=item COUNTERdecimal unsigned integer=item COUNTER64decimal unsigned integer=item GAUGEdecimal unsigned integer=item UINTEGERdecimal unsigned integer=item TICKSdecimal unsigned integer=item OPAQUEperl scalar containing octets=item NULLperl scalar containing nothing=back=item <type>SNMP data type (see list above), this field ispopulated by 'get' and 'getnext' operations. Insome cases the programmer needs to populate thisfield when passing to a 'set' operation. thisfield need not be supplied when the attributeindicated by <tag> is already described by loadedMib modules. for 'set's, if a numeric OID is usedand the object is not currently in the loaded Mib,the <type> field must be supplied=back=item simple stringlight weight form of <var> used to 'set' or 'get' asingle attribute without constructing an SNMP::Varbind.stored in a perl scalar, has the form '<tag>.<iid>',(e.g., 'sysDescr.0'). for 'set' operations the valueis passed as a second arg. Note: This argument form isnot updated in get[next] operations as are the other forms.=back=head1 Acceptable callback formats<callback> may be one of the following forms:=over=item without arguments=over=item \&subname=item sub { ... }=back=item or with arguments=over=item [ \&subname, $arg1, ... ]=item [ sub { ... }, $arg1, ... ]=item [ "method", $obj, $arg1, ... ]=back=backcallback will be called when response is received or timeoutoccurs. the last argument passed to callback will be aSNMP::VarList reference. In case of timeout the last argumentwill be undef.=over=item &SNMP::MainLoop([<timeout>, [<callback>]])to be used with async SNMP::Sessioncalls. MainLoop must be called after initial async callsso return packets from the agent will not be processed.If no args suplied this function enters an infinite loopso program must be exited in a callback or externallyinterupted. If <timeout(sic)=item &SNMP::finish()This function, when called from an SNMP::MainLoop() callbackfunction, will cause the current SNMP::MainLoop() to returnafter the callback is completed.  finish() can be used toterminate an otherwise-infinite MainLoop.  A new MainLoop()instance can then be started to handle further requests.=back=head1 SNMP package variables and functions=over=item $SNMP::VERSIONthe current version specifier (e.g., 3.1.0)=item $SNMP::auto_init_mibdefault '1', set to 0 to disable automatic readingof the MIB upon session creation. set to non-zeroto call initMib at session creation which will resultin MIB loading according to Net-SNMP env. variables (seeman mib_api)=item $SNMP::verbosedefault '0', controls warning/info output ofSNMP module, 0 => no output, 1 => enables warning/infooutput from SNMP module itself (is also controlledby SNMP::debugging - see below)=item $SNMP::use_long_namesdefault '0', s

⌨️ 快捷键说明

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