SYNOPSIS

    use SNMP::Multi;

my $req = SNMP::Multi::VarReq->new ( nonrepeaters => 1, hosts => [ qw/ router1.my.com router2.my.com / ], vars => [ [ 'sysUpTime' ], [ 'ifInOctets' ], [ 'ifOutOctets' ] ], ); die "VarReq: $SNMP::Multi::VarReq::error\n" unless $req;

my $sm = SNMP::Multi->new ( Method => 'bulkwalk', MaxSessions => 32, PduPacking => 16, Community => 'public', Version => '2c', Timeout => 5, Retries => 3, UseNumeric => 1, # Any additional options for SNMP::Session::new() ... ) or die "$SNMP::Multi::error\n";

$sm->request($req) or die $sm->error; my $resp = $sm->execute() or die "Execute: $SNMP::Multi::error\n";

print "Got response for ", (join ' ', $resp->hostnames()), "\n"; for my $host ($resp->hosts()) {

print "Results for $host: \n"; for my $result ($host->results()) { if ($result->error()) { print "Error with $host: ", $result->error(), "\n"; next; }

print "Values for $host: ", (join ' ', $result->values()); for my $varlist ($result->varlists()) { print map { "\t" . $_->fmt() . "\n" } @$varlist; } print "\n"; } }

DESCRIPTION

The SNMP::Multi package provides a mechanism to perform \s-1SNMP\s0 operations on several hosts simultaneously. SNMP::Multi builds on G. Marzot's \s-1SNMP\s0 Perl interface to the UC-Davis \s-1SNMP\s0 libraries, using asynchronous \s-1SNMP\s0 operations to send queries/sets to multiple hosts simultaneously.

Results from all hosts are compiled into a single object, which offers methods to access the data in aggregate, or broken down by host or the individual request.

SNMP::Multi supports \s-1SNMP\s0 \s-1GET\s0, \s-1SET\s0, \s-1GETNEXT\s0, \s-1GETBULK\s0 and \s-1BULKWALK\s0 requests. It also performs \s-1PDU\s0 packing in order to improve network efficiency, when packing is possible.

OPTIONS

The SNMP::Multi constructor takes the following options to control its behavior. Any other options are stored and handed to the SNMP::Session constructor when a new \s-1SNMP\s0 session is created. As the behavior of SNMP::Multi depends upon certain SNMP::Session parameters (i.e. Timeout), these will be listed below as SNMP::Multi options. These \*(L"overlapped\*(R" options will be passed un-changed to SNMP::Session's constructor.

``Method''

Specify one of get, set, getnext, getbulk or bulkwalk. The appropriate \s-1SNMP\s0 request will be made to each host for each set of variables requested by the user. This parameter is required. There is no default value.

``Requests''

The SNMP::Multi object may be given a new set of requests via the request() method, or by passing a reference to an SNMP::Multi::VarReq object into the constructor. Any VarReq requests given to the SNMP::Multi object through the constructor will be overwritten by subsequent calls to SNMP::Multi::request(). This parameter is optional.

``PduPacking''

The maximum number of variable requests that will be packed into a single \s-1SNMP\s0 request is controlled by the ``PduPacking'' parameter. \s-1PDU\s0 packing improves the efficiency and accuracy of \s-1SNMP\s0 requests by reducing the number of packets exchanged. Setting this variable to '0' will disable \s-1PDU\s0 packing altogether. \s-1PDU\s0 packing is not performed for \s-1SNMP\s0 \s-1GETBULK\s0 or \s-1BULKWALK\s0 requests. This optional parameter defaults to the value of $SNMP::Multi::pdupacking.

``MaxSessions''

This variable controls the maximum number of \s-1SNMP\s0 sessions that will be kept open simultaneously. Setting ``MaxSessions'' higher increases the number of agents being queried at any time, up to the maximum limit of file descriptors available to the process. SNMP::Multi detects \*(L"out of resources\*(R" conditions (i.e. \s-1EMFILE\s0) and adjusts the number of open connections accordingly. This optional parameter defaults to the value of $SNMP::Multi::maxsessions.

``Concurrent''

The value of ``Concurrent'' limits the number of requests that may be \*(L"in flight\*(R" at any time. It defaults to the value of ``MaxSessions'' (see above). Setting this value higher may reduce the overall runtime of the SNMP::Multi request, but will also likely increase network traffic and congestion (current maintainer has had SNMP::Multi running smoothly with concurrent set to 512). This optional parameter defaults to the value of $SNMP::Multi::maxsessions or the object's 'MaxSessions' parameter.

``GetbulkMax''

Sets the default \*(L"maxrepetitions\*(R" value for \s-1SNMP\s0 \s-1GETBULK\s0 and \s-1BULKWALK\s0 requests. This value may be overridden on a per-request basis (by specifying the 'maxrepetitions' parameter in the SNMP::Multi::VarReq constructor). This optional parameter defaults to the value of $SNMP::Multi::getbulkmax.

``ExternalSelect''

If ``ExternalSelect'' is specified, the SNMP::Multi's execute() method will return immediately after dispatching the first volley of \s-1SNMP\s0 requests. The caller can then use SNMP::select_info() to get a list of the current file descriptors for the \s-1SNMP\s0 sessions, and select() on them. When one of the fd's becomes readable, it should be handed to SNMP::reply_cb() to handle it. Note that \s-1SNMP\s0 bulkwalks use the callbacks to dispatch continuing \s-1GETBULK\s0 requests. This causes the file descriptor to be readable, but SNMP::reply_cb() calls an internal callback in \s-1SNMP\s0.xs's bulkwalk implementation, not the SNMP::Multi handler callback. When the walk completes, the SNMP::Multi callback will be called with the specified arguments.

``Retries'' (shared with SNMP::Session)

The ``Retries'' options specifies the maximum number of retries for each \s-1SNMP\s0 request. Note that this is the number of retries, not the total number of attempted requests. This optional parameter defaults to the value of $SNMP::Multi::maxretries.

``Timeout'' (shared with SNMP::Session)

The ``Timeout'' parameter specifies the timeout in seconds between successive retries for \s-1SNMP\s0 requests. The overall runtime for the complete SNMP::Multi request will be approximately : (retries + 1) * timeout Please note that this is the lower-bound on the time-out. Without sufficient resources (especially file descriptors) to optimize the network communications, completing all requested \s-1SNMP\s0 operations can take considerably longer. An over-all timeout may be specified as the optional \*(L"timeout\*(R" parameter to the SNMP::Multi's execute() method. This optional parameter defaults to the value of $SNMP::Multi::timeout.

``Community'' (shared with SNMP::Session)

The ``Community'' parameter specifies the \s-1SNMP\s0 community string to use when making requests from \s-1SNMP\s0 agents. No mechanism exists at this time to specify a different community for individual agents. This optional parameter defaults to the value of $SNMP::Multi::community.

``Version'' (shared with SNMP::Session)

The ``Version'' option specifies the \s-1SNMP\s0 protocol to use with the agents. Due to the poor error reporting in \s-1SNMP\s0 v1, it is recommended that \s-1SNMP\s0 v2c or v3 be used to communicate with the agents when possible. This optional parameter defaults to the value of $SNMP::Multi::snmpversion.

METHODS

The SNMP::Multi object provides several methods for the caller. In most cases, only the new(), request(), and execute() methods need to be used. The various methods are documented in approximately the order in which they are normally called.

SNMP::Multi::new(...)

Create a new instance of an SNMP::Multi object. See above for a description of the available constructor options.

SNMP::Multi::request( <ref to SNMP::Multi::VarReq> )

request() arranges for the set of host/variable requests stored in the SNMP::Multi::VarReq object to be transferred to the SNMP::Multi object. This can also be done in the constructor using the ``requests'' option. Note that the request() method is not cumulative \*(-- previous requests will be overwritten by subsequent calls to request().

SNMP::Multi::execute( [timeout] )

The execute() function performs the actual work in SNMP::Multi, returning when all requests have been answered or timed out. An optional `timeout' argument to execute() specifies an overall timeout, regardless of the number and timing of retries. execute() returns a reference to an SNMP::Multi::Response object. This object provides methods to conveniently access the returned data values.

SNMP::Multi::error()

If an error occurs while SNMP::Multi is executing, the caller may retrieve a descriptive string describing the error from the error() method.

The remaining() method produces an SNMP::Multi::VarReq that is populated with the requests for any un-answered or un-sent request hunks. This VarReq may then be passed to another SNMP::Multi object (or the same one). This allows an application to loop on timeouts like this: my $req = SNMP::Multi::VarReq->new( ... ); my $sm = SNMP::Multi->new( ... ); while ($req) { $sm->request($req); my $resp = $sm->execute(); handle_response($resp); print "Timeout - retrying" if ($req = $sm->remaining()); } You can accumulate remaining requests by passing an already existing SNMP::Multi::VarReq object as an argument. Remaining requests will then be added to that object. That allows us to to collect all remaining ones with ease, while looping over huge number of hosts.

Building SNMP::Multi::VarReq Requests

\s-1SNMP\s0 variable requests are composed and passed to the SNMP::Multi object through an auxiliary class called an SNMP::Multi::VarReq. This class simply collects \s-1SNMP\s0 requests for variables and hosts (and optionally validates them). The interface to SNMP::Multi::VarReq is very simple, providing only new() and add() methods. They take the following arguments: 'vars' => [ list of Varbinds to be requested (REQUIRED) ] 'hosts' => [ list of hosts for this variable list ] 'nonrepeaters' => [ GETBULK/BULKWALK "nonrepeaters" parameter ] 'maxrepetitions' => [ GETBULK/BULKWALK "maxrepetitions" parameter ] Every call to new() or add() must contain a list of \s-1SNMP\s0 variables. If the hosts parameter is not specified, the variable list will be requested from all hosts currently known by the SNMP::Multi::VarReq object. If a host list is given, the variables will be requested only from the named hosts. Some simple sanity checks can be performed on the VarReq by calling its validate() method, or by setting $SNMP::Multi::VarReq::autovalidate to 1 before calling the new() method. An example of building up a complicated request using new() and add(): Start with: $r = SNMP::Multi::VarReq->new( hosts => [ qw/ A B C / ], vars => [ qw/ 1 2 3 / ] ); to get: A: 1 2 3 B: 1 2 3 C: 1 2 3 Now add a var to each host: $r->add( vars => [ qw/ 4 / ] ); to get: A: 1 2 3 4 B: 1 2 3 4 C: 1 2 3 4 Add a var to a specific set of hosts: $r->add( hosts => [ qw/ A C / ], vars => [ qw/ 5 / ] ); to get: A: 1 2 3 4 5 B: 1 2 3 4 C: 1 2 3 4 5 Finally, create two new hosts and add a pair of vars to them: $r->add( hosts => [ qw/ D E / ], vars => [ qw/ 6 7 / ] ); to get: A: 1 2 3 4 5 B: 1 2 3 4 C: 1 2 3 4 5 D: 6 7 E: 6 7 The SNMP::Multi::VarReq object also provides a dump() method which generates a simple dump of the current host/var requests.

SNMP PDU Packing Features

SNMP::Multi packs SNMP::Varbind requests into larger request \*(L"hunks\*(R" to reduce the number of request/response pairs required to complete the SNMP::Multi request. This packing is controlled by the SNMP::Multi 'PduPacking' parameter. For instance, assume your application creates an SNMP::Multi object with a 'PduPacking' value of 3. SNMP::Multi will pack 5 single \s-1SNMP\s0 variable requests into two distinct requests. The first request will contain the first 3 variables, the second will get the remaining two variables. \s-1PDU\s0 packing is not done for \s-1SNMP\s0 \s-1GETBULK\s0 and \s-1BULKWALK\s0 requests. The feature may be disabled by setting the 'PduPacking' parameter to '0'.

Accessing SNMP Data From Agent Responses

The SNMP::Multi::execute() method returns the responses from the \s-1SNMP\s0 agents in an SNMP::Multi::Response object. This object, indexed by hostname, consists of per-host response objects (SNMP::Multi::Response::Host's), each of which contains a list of SNMP::Multi::Result objects. The Result objects connect an SNMP::VarList with the error status (if any) from the \s-1SNMP\s0 request. An entry is only made in the Response object if the \s-1SNMP\s0 agent returned some response to SNMP::Multi. This is fairly complicated, but the various objects provide accessor methods to make access to the \s-1SNMP\s0 responses simple. Assume your application is structured something like this example source code: my $req = SNMP::Multi::VarReq->new( hosts => [...], vars => [...] ); my $sm = SNMP::Multi->new( ... requests => $req, ... ); my $response = $sm->execute( $overall_timeout ); die $sm->error() if $sm->error(); Now the data can be accessed through methods on the objects that make up the SNMP::Multi::Response returned by execute(). An SNMP::VarList object is returned for each variable requested. This normalizes the return format across all \s-1SNMP\s0 operations (including bulkwalks). See the \s-1SYNOPSIS\s0 section above for an example of how to access the \s-1SNMP\s0 data values after calling the execute() method.

SNMP::Multi::Response methods
hostnames()

Return a list of the hosts that responded to the \s-1SNMP\s0 queries made by execute().

values()

Return all values returned by the \s-1SNMP\s0 agents, collated into a single list. This method can be used when the application is not concerned with which value was returned by a specific host (i.e. summing up octet counts on router interfaces).

hosts()

Returns a list of SNMP::Multi::Response::Host objects, one per host queried by the SNMP::Multi::execute() method.

SNMP::Multi::Response::Host methods
hostname()

Return the hostname associated with this set of responses. The reference may also be stringified to get the hostname : print "This is the list of results for $host: \n";

values()

Return all values received in response to requests made to the associated host.

results()

Returns a list of SNMP::Multi::Result objects for this host. There is one Result object for each request sent to the \s-1SNMP\s0 agent on this host.

SNMP::Multi::Result methods

The SNMP::Multi::Result object correlates \s-1SNMP\s0 error information with the response to an \s-1SNMP\s0 request.

error()

Return a printable string describing the error encountered for this variable, or undef if no error occurred.

values()

Return a list of the values received for this request.

varlists()

Return an array of SNMP::VarList objects, one per variable requested in the \s-1SNMP\s0 packet. This format is consistent for all \s-1SNMP\s0 operations, and is required to support bulkwalks (in which the number of returned values per variable is not known a priori to the calling application).

EXAMPLES

A complete example is given in the \*(L"\s-1SYNOPSIS\s0\*(R" section above.

CAVEATS

The VarList returned for \s-1GETBULK\s0 requests is \*(L"decoded\*(R" by SNMP::Multi into an array of single VarLists, one for each requested variable. This behavior differs from the return from the getbulk() method in the \s-1SNMP\s0.pm module, but is consistent with the return value of \s-1SNMP\s0.pm's bulkwalk() method.

Note that the V1 \s-1SNMP\s0 protocol has very limited error reporting (the agent returns no values, and the 'errind' is set to the index of the offending \s-1SNMP\s0 variable request). The SNMP::Multi module adjusts the 'errind' index to indicate which of the variables request requested for a host have failed, regardless of the number of actual packets exchanged. This is necessary to support SNMP::Multi's transparent pdu-packing feature.

SNMP::Multi relies on features added to the \s-1SNMP\s0 module by Electric Lightwave, Inc. These features have been incorporated into UCD-SNMP releases 4.2 and later. You must have \s-1SNMP\s0 4.2 or later installed to use this package.

Using SNMP::Multi with large numbers of hosts or large requests may cause network congestion. All targets may send \s-1PDU\s0's to the originating host simultaneously, which could cause heavy traffic and/or dropped packets at the host. Adjusting the Concurrent and PduPacking variables can mitigate this problem.

Network congestion may be a serious problem for bulkwalks, due to multiple packets being exchanged per session. However, network latency and variable target response times cause packets in multiple bulkwalk exchanges to become spread out as the walk progresses. The initial exchange, however, will always cause congestion.

BUGS

There is no interface to specify a different \s-1SNMP\s0 community string for a specific host, although the community is stored on a per-host basis.

RELATED TO SNMP::Multi…

\s-1SNMP\s0, the NetSNMP homepage at http://www.net-snmp.org/.

AUTHOR

Karl (\*(L"Terminator rAT\*(R") Schilke <[email protected]>

CONTRIBUTORS

Joshua Keroes, Todd Caine, Toni Prug <[email protected]>

COPYRIGHT

Developed by Karl \*(L"Terminator rAT\*(R" Schilke for Electric Lightwave, Inc. Copyright (c) 2000-2002 Electric Lightwave, Inc. All rights reserved.

Co-maintained by Toni Prug.

This software is provided ``as is'' and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and/or fitness for a particular purpose.

This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.