\section{NAME\label{NAME}} Net::SNMP - Object oriented interface to SNMP \section{SYNOPSIS\label{SYNOPSIS}\index{synopsis of Net::SNMP}} The Net::SNMP module implements an object oriented interface to the Simple Network Management Protocol. Perl applications can use the module to retrieve or update information on a remote host using the SNMP protocol. The module supports SNMP version-1, SNMP version-2c (Community-Based SNMPv2), and SNMP version-3. The Net::SNMP module assumes that the user has a basic understanding of the Simple Network Management Protocol and related network management concepts. \section{DESCRIPTION\label{DESCRIPTION}\index{description of Net::SNMP}} The Net::SNMP module abstracts the intricate details of the Simple Network Management Protocol by providing a high level programming interface to the protocol. Each Net::SNMP object provides a one-to-one mapping between a Perl object and a remote SNMP agent or manager. Once an object is created, it can be used to perform the basic protocol exchange actions defined by SNMP. A Net::SNMP object can be created such that it has either "blocking" or "non-blocking" properties. By default, the methods used to send SNMP messages do not return until the protocol exchange has completed successfully or a timeout period has expired. This behavior gives the object a "blocking" property because the flow of the code is stopped until the method returns. The optional named argument \textbf{-nonblocking} can be passed to the object constructor with a true value to give the object "non-blocking" behavior. A method invoked by a non-blocking object queues the SNMP message and returns immediately, allowing the flow of the code to continue. The queued SNMP messages are not sent until an event loop is entered by calling the \texttt{snmp\_dispatcher()} method. When the SNMP messages are sent, any response to the messages invokes the subroutine defined by the user when the message was originally queued. The event loop exits when all messages have been removed from the queue by either receiving a response, or by exceeding the number of retries at the Transport Layer. \subsection*{Blocking Objects\label{Blocking_Objects}\index{Blocking Objects}} The default behavior of the methods associated with a Net::SNMP object is to block the code flow until the method completes. For methods that initiate a SNMP protocol exchange requiring a response, a hash reference containing the results of the query is returned. The undefined value is returned by all methods when a failure has occurred. The \texttt{error()} method can be used to determine the cause of the failure. The hash reference returned by a SNMP protocol exchange points to a hash constructed from the VarBindList contained in the SNMP response message. The hash is created using the ObjectName and the ObjectSyntax pairs in the VarBindList. The keys of the hash consist of the OBJECT IDENTIFIERs in dotted notation corresponding to each ObjectName in the VarBindList. The value of each hash entry is set equal to the value of the corresponding ObjectSyntax. This hash reference can also be retrieved using the \texttt{var\_bind\_list()} method. \subsection*{Non-blocking Objects\label{Non-blocking_Objects}\index{Non-blocking Objects}} When a Net::SNMP object is created having non-blocking behavior, the invocation of a method associated with the object returns immediately, allowing the flow of the code to continue. When a method is invoked that would initiate a SNMP protocol exchange requiring a response, either a true value (i.e. 0x1) is returned immediately or the undefined value is returned if there was a failure. The \texttt{error()} method can be used to determine the cause of the failure. The contents of the VarBindList contained in the SNMP response message can be retrieved by calling the \texttt{var\_bind\_list()} method using the object reference passed as the first argument to the callback. The value returned by the \texttt{var\_bind\_list()} method is a hash reference created using the ObjectName and the ObjectSyntax pairs in the VarBindList. The keys of the hash consist of the OBJECT IDENTIFIERs in dotted notation corresponding to each ObjectName in the VarBindList. The value of each hash entry is set equal to the value of the corresponding ObjectSyntax. The undefined value is returned if there has been a failure and the \texttt{error()} method may be used to determine the reason. \section{METHODS\label{METHODS}\index{methods of Net::SNMP}} When named arguments are expected by the methods, two different styles are supported. All examples in this documentation use the dashed-option style: \begin{verbatim} $object->method(-argument => $value); \end{verbatim} However, the IO:: style is also allowed: \begin{verbatim} $object->method(Argument => $value); \end{verbatim} \begin{description} \item[Non-blocking Objects Arguments] \mbox{} When a Net::SNMP object has been created with a "non-blocking" property, most methods that generate a SNMP message take additional arguments to support this property. \begin{description} \item[Callback] \mbox{} Most methods associated with a non-blocking object have an optional named argument called \textbf{-callback}. The \textbf{-callback} argument expects a reference to a subroutine or to an array whose first element must be a reference to a subroutine. The subroutine defined by the \textbf{-callback} option is executed when a response to a SNMP message is received, an error condition has occurred, or the number of retries for the message has been exceeded. When the \textbf{-callback} argument only contains a subroutine reference, the subroutine is evaluated passing a reference to the original Net::SNMP object as the only parameter. If the \textbf{-callback} argument was defined as an array reference, all elements in the array are passed to subroutine after the reference to the Net::SNMP object. The first element, which is required to be a reference to a subroutine, is removed before the remaining arguments are passed to that subroutine. Once one method is invoked with the \textbf{-callback} argument, this argument stays with the object and is used by any further calls to methods using the \textbf{-callback} option if the argument is absent. The undefined value may be passed to the \textbf{-callback} argument to delete the callback. \textbf{NOTE:} The subroutine being passed with the \textbf{-callback} named argument should not cause blocking itself. This will cause all the actions in the event loop to be stopped, defeating the non-blocking property of the Net::SNMP module. \item[Delay] \mbox{} An optional argument \textbf{-delay} can also be passed to non-blocking objects. The \textbf{-delay} argument instructs the object to wait the number of seconds passed to the argument before executing the SNMP protocol exchange. The delay period starts when the event loop is entered. The \textbf{-delay} parameter is applied to all methods associated with the object once it is specified. The delay value must be set back to 0 seconds to disable the delay parameter. \end{description} \item[SNMPv3 Arguments] \mbox{} A SNMP context is a collection of management information accessible by a SNMP entity. An item of management information may exist in more than one context and a SNMP entity potentially has access to many contexts. The combination of a contextEngineID and a contextName unambiguously identifies a context within an administrative domain. In a SNMPv3 message, the contextEngineID and contextName are included as part of the scopedPDU. All methods that generate a SNMP message optionally take a \textbf{-contextengineid} and \textbf{-contextname} argument to configure these fields. \begin{description} \item[Context Engine ID] \mbox{} The \textbf{-contextengineid} argument expects a hexadecimal string representing the desired contextEngineID. The string must be 10 to 64 characters (5 to 32 octets) long and can be prefixed with an optional "0x". Once the \textbf{-contextengineid} is specified it stays with the object until it is changed again or reset to default by passing in the undefined value. By default, the contextEngineID is set to match the authoritativeEngineID of the authoritative SNMP engine. \item[Context Name] \mbox{} The contextName is passed as a string which must be 0 to 32 octets in length using the \textbf{-contextname} argument. The contextName stays with the object until it is changed. The contextName defaults to an empty string which represents the "default" context. \end{description} \end{description} \subsection*{session() - create a new Net::SNMP object\label{session_-_create_a_new_Net::SNMP_object}\index{session() - create a new Net::SNMP object}} \begin{verbatim} ($session, $error) = Net::SNMP->session( [-hostname => $hostname,] [-port => $port,] [-localaddr => $localaddr,] [-localport => $localport,] [-nonblocking => $boolean,] [-version => $version,] [-timeout => $seconds,] [-retries => $count,] [-maxmsgsize => $octets,] [-translate => $translate,] [-debug => $bitmask,] [-community => $community,] # v1/v2c [-username => $username,] # v3 [-authkey => $authkey,] # v3 [-authpassword => $authpasswd,] # v3 [-authprotocol => $authproto,] # v3 [-privkey => $privkey,] # v3 [-privpassword => $privpasswd,] # v3 [-privprotocol => $privproto,] # v3 ); \end{verbatim} This is the constructor for Net::SNMP objects. In scalar context, a reference to a new Net::SNMP object is returned if the creation of the object is successful. In list context, a reference to a new Net::SNMP object and an empty error message string is returned. If a failure occurs, the object reference is returned as the undefined value. The error string may be used to determine the cause of the error. Most of the named arguments passed to the constructor define basic attributes for the object and are not modifiable after the object has been created. The \textbf{-timeout}, \textbf{-retries}, \textbf{-maxmsgsize}, \textbf{-translate}, and \textbf{-debug} arguments are modifiable using an accessor method. See their corresponding method definitions for a complete description of their usage, default values, and valid ranges. \begin{description} \item[Transport Layer Arguments] \mbox{} The Net::SNMP module uses UDP/IP as the Transport Layer to pass SNMP messages between the local and remote devices. The destination device can be specified using the \textbf{-hostname} argument. The \textbf{-hostname} argument accepts either an IP network hostname or an IP address in dotted notation. This argument is optional and defaults to "localhost". The destination UPD port number can be specified using the \textbf{-port} argument. This argument is also optional and defaults to 161, which is the port number on which devices using default values expect to receive SNMP request messages. The \textbf{-port} argument will need to be specified for remote devices expecting to receive SNMP notifications since these device typically default to port 162. By default, the source IP address and port number are assigned dynamically by the local device on which the Net::SNMP module is being used. This dynamic assignment can be overridden by using the \textbf{-localaddr} and \textbf{-localport} arguments. These values default to \textit{INADDR\_ANY} (typically 0.0.0.0) and 0 respectively. The \textbf{-localaddr} argument will accept either an IP network hostname or an IP address in dotted notation. If a hostname is specified, the resolved IP address must be a valid address on the local device. \item[Security Model Arguments] \mbox{} The \textbf{-version} argument controls which other arguments are expected or required by the \texttt{session()} constructor. The Net::SNMP module supports SNMPv1, SNMPv2c, and SNMPv3. The module defaults to SNMPv1 if no \textbf{-version} argument is specified. The \textbf{-version} argument expects either a digit (i.e. '1', '2', or '3') or a string specifying the version (i.e. 'snmpv1', 'snmpv2c', or 'snmpv3') to define the SNMP version. The Security Model used by the Net::SNMP object is based on the SNMP version associated with the object. If the SNMP version is SNMPv1 or SNMPv2c a Community-based Security Model will be used, while the User-based Security Model (USM) will be used if the version is SNMPv3. \begin{description} \item[Community-based Security Model Argument] \mbox{} If the Security Model is Community-based, the only argument available is the \textbf{-community} argument. This argument expects a string that is to be used as the SNMP community name. By default the community name is set to 'public' if the argument is not present. \item[User-based Security Model Arguments] \mbox{} The User-based Security Model (USM) used by SNMPv3 requires that a securityName be specified using the \textbf{-username} argument. The creation of a Net::SNMP object with the version set to SNMPv3 will fail if the \textbf{-username} argument is not present. The \textbf{-username} argument expects a string 1 to 32 octets in length. Different levels of security are allowed by the User-based Security Model which address authentication and privacy concerns. A SNMPv3 Net::SNMP object will derive the security level (securityLevel) based on which of the following arguments are specified. By default a securityLevel of 'noAuthNoPriv' is assumed. If the \textbf{-authkey} or \textbf{-authpassword} arguments are specified, the securityLevel becomes 'authNoPriv'. The \textbf{-authpassword} argument expects a string which is at least 1 octet in length. Optionally, the \textbf{-authkey} argument can be used so that a plain text password does not have to be specified in a script. The \textbf{-authkey} argument expects a hexadecimal string produced by localizing the password with the authoritativeEngineID for the specific destination device. The \texttt{snmpkey} utility included with the distribution can be used to create the hexadecimal string (see \emph{snmpkey}). Two different hash algorithms are defined by SNMPv3 which can be used by the Security Model for authentication. These algorithms are HMAC-MD5-96 "MD5" (RFC 1321) and HMAC-SHA-96 "SHA-1" (NIST FIPS PUB 180-1). The default algorithm used by the module is HMAC-MD5-96. This behavior can be changed by using the \textbf{-authprotocol} argument. This argument expects either the string 'md5' or 'sha' to be passed to modify the hash algorithm. By specifying the arguments \textbf{-privkey} or \textbf{-privpassword} the securityLevel associated with the object becomes 'authPriv'. According to SNMPv3, privacy requires the use of authentication. Therefore, if either of these two arguments are present and the \textbf{-authkey} or \textbf{-authpassword} arguments are missing, the creation of the object fails. The \textbf{-privkey} and \textbf{-privpassword} arguments expect the same input as the \textbf{-authkey} and \textbf{-authpassword} arguments respectively. The User-based Security Model described in RFC 3414 defines a single encryption protocol to be used for privacy. This protocol, CBC-DES "DES" (NIST FIPS PUB 46-1), is used by default or if the string 'des' is passed to the \textbf{-privprotocol} argument. By working with the Extended Security Options Consortium \textsf{http://www.snmp.com/eso/}, the module also supports additional protocols which have been defined in draft specifications. The draft \textsf{http://www.snmp.com/eso/draft-reeder-snmpv3-usm-3desede-00.txt} defines the support of CBC-3DES-EDE "Triple-DES" (NIST FIPS 46-3) in the User-based Security Model. This protocol can be selected using the \textbf{-privprotocol} argument with the string '3desede'. The draft \textsf{http://www.snmp.com/eso/draft-blumenthal-aes-usm-04.txt} describes the use of CFB128-AES-128/192/256 "AES" (NIST FIPS PUB 197) in the USM. The three AES encryption protocols, differentiated by their key sizes, can be selected by passing 'aescfb128', 'aescfb192', or 'aescfb256' to the \textbf{-privprotocol} argument. \end{description} \end{description} \subsection*{close() - clear the Transport Layer associated with the object\label{close_-_clear_the_Transport_Layer_associated_with_the_object}\index{close() - clear the Transport Layer associated with the object}} \begin{verbatim} $session->close; \end{verbatim} This method clears the UDP Transport Layer and any errors associated with the object. Once closed, the Net::SNMP object can no longer be used to send or receive SNMP messages. \subsection*{snmp\_dispatcher() - enter the non-blocking object event loop\label{snmp_dispatcher_-_enter_the_non-blocking_object_event_loop}\index{snmp\ dispatcher() - enter the non-blocking object event loop}} \begin{verbatim} $session->snmp_dispatcher(); \end{verbatim} This method enters the event loop associated with non-blocking Net::SNMP objects. The method exits when all queued SNMP messages have received a response or have timed out at the Transport Layer. This method is also exported as the stand alone function \texttt{snmp\_dispatcher()} by default (see \S\ref{EXPORTS}). \subsection*{get\_request() - send a SNMP get-request to the remote agent\label{get_request_-_send_a_SNMP_get-request_to_the_remote_agent}\index{get\ request() - send a SNMP get-request to the remote agent}} \begin{verbatim} $result = $session->get_request( [-callback => sub {},] # non-blocking [-delay => $seconds,] # non-blocking [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 -varbindlist => \@oids, ); \end{verbatim} This method performs a SNMP get-request query to gather data from the remote agent on the host associated with the Net::SNMP object. The message is built using the list of OBJECT IDENTIFIERs in dotted notation passed to the method as an array reference using the \textbf{-varbindlist} argument. Each OBJECT IDENTIFER is placed into a single SNMP GetRequest-PDU in the same order that it held in the original list. A reference to a hash is returned in blocking mode which contains the contents of the VarBindList. In non-blocking mode, a true value is returned when no error has occurred. In either mode, the undefined value is returned when an error has occurred. The \texttt{error()} method may be used to determine the cause of the failure. \subsection*{get\_next\_request() - send a SNMP get-next-request to the remote agent\label{get_next_request_-_send_a_SNMP_get-next-request_to_the_remote_agent}\index{get\ next\ request() - send a SNMP get-next-request to the remote agent}} \begin{verbatim} $result = $session->get_next_request( [-callback => sub {},] # non-blocking [-delay => $seconds,] # non-blocking [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 -varbindlist => \@oids, ); \end{verbatim} This method performs a SNMP get-next-request query to gather data from the remote agent on the host associated with the Net::SNMP object. The message is built using the list of OBJECT IDENTIFIERs in dotted notation passed to the method as an array reference using the \textbf{-varbindlist} argument. Each OBJECT IDENTIFER is placed into a single SNMP GetNextRequest-PDU in the same order that it held in the original list. A reference to a hash is returned in blocking mode which contains the contents of the VarBindList. In non-blocking mode, a true value is returned when no error has occurred. In either mode, the undefined value is returned when an error has occurred. The \texttt{error()} method may be used to determine the cause of the failure. \subsection*{set\_request() - send a SNMP set-request to the remote agent\label{set_request_-_send_a_SNMP_set-request_to_the_remote_agent}\index{set\ request() - send a SNMP set-request to the remote agent}} \begin{verbatim} $result = $session->set_request( [-callback => sub {},] # non-blocking [-delay => $seconds,] # non-blocking [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 -varbindlist => \@oid_value, ); \end{verbatim} This method is used to modify data on the remote agent that is associated with the Net::SNMP object using a SNMP set-request. The message is built using a list of values consisting of groups of an OBJECT IDENTIFIER, an object type, and the actual value to be set. This list is passed to the method as an array reference using the \textbf{-varbindlist} argument. The OBJECT IDENTIFIERs in each trio are to be in dotted notation. The object type is an octet corresponding to the ASN.1 type of value that is to be set. Each of the supported ASN.1 types have been defined and are exported by the package by default (see \S\ref{EXPORTS}). A reference to a hash is returned in blocking mode which contains the contents of the VarBindList. In non-blocking mode, a true value is returned when no error has occurred. In either mode, the undefined value is returned when an error has occurred. The \texttt{error()} method may be used to determine the cause of the failure. \subsection*{trap() - send a SNMP trap to the remote manager\label{trap_-_send_a_SNMP_trap_to_the_remote_manager}\index{trap() - send a SNMP trap to the remote manager}} \begin{verbatim} $result = $session->trap( [-delay => $seconds,] # non-blocking [-enterprise => $oid,] [-agentaddr => $ipaddress,] [-generictrap => $generic,] [-specifictrap => $specific,] [-timestamp => $timeticks,] -varbindlist => \@oid_value, ); \end{verbatim} This method sends a SNMP trap to the remote manager associated with the Net::SNMP object. All arguments are optional and will be given the following defaults in the absence of a corresponding named argument: \begin{itemize} \item The default value for the trap \textbf{-enterprise} is "1.3.6.1.4.1", which corresponds to ``\path{iso.org.dod.internet.private.enterprises}''. The enterprise value is expected to be an OBJECT IDENTIFER in dotted notation. \item The default value for the trap \textbf{-agentaddr} is the local IP address from the host on which the script is running or the local address specified by the \textbf{-localaddr} option. The agent-addr is expected to be an IpAddress in dotted notation. \item The default value for the \textbf{-generictrap} type is 6 which corresponds to "enterpriseSpecific". The generic-trap types are defined and can be exported upon request (see \S\ref{EXPORTS}). \item The default value for the \textbf{-specifictrap} type is 0. No pre-defined values are available for specific-trap types. \item The default value for the trap \textbf{-timestamp} is the "uptime" of the script. The "uptime" of the script is the number of hundredths of seconds that have elapsed since the script began running. The time-stamp is expected to be a TimeTicks number in hundredths of seconds. \item The default value for the trap \textbf{-varbindlist} is an empty array reference. The variable-bindings are expected to be in an array format consisting of groups of an OBJECT IDENTIFIER, an object type, and the actual value of the object. This is identical to the list expected by the \texttt{set\_request()} method. The OBJECT IDENTIFIERs in each trio are to be in dotted notation. The object type is an octet corresponding to the ASN.1 type for the value. Each of the supported types have been defined and are exported by default (see \S\ref{EXPORTS}). \end{itemize} A true value is returned when the method is successful. The undefined value is returned when a failure has occurred. The \texttt{error()} method can be used to determine the cause of the failure. Since there are no acknowledgements for Trap-PDUs, there is no way to determine if the remote host actually received the trap. \textbf{NOTE:} When the object is in non-blocking mode, the trap is not sent until the event loop is entered and no callback is ever executed. \textbf{NOTE:} This method can only be used when the version of the object is set to SNMPv1. \subsection*{get\_bulk\_request() - send a get-bulk-request to the remote agent\label{get_bulk_request_-_send_a_get-bulk-request_to_the_remote_agent}\index{get\ bulk\ request() - send a get-bulk-request to the remote agent}} \begin{verbatim} $result = $session->get_bulk_request( [-callback => sub {},] # non-blocking [-delay => $seconds,] # non-blocking [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 [-nonrepeaters => $non_reps,] [-maxrepetitions => $max_reps,] -varbindlist => \@oids, ); \end{verbatim} This method performs a SNMP get-bulk-request query to gather data from the remote agent on the host associated with the Net::SNMP object. All arguments are optional except \textbf{-varbindlist} and will be given the following defaults in the absence of a corresponding named argument: \begin{itemize} \item The default value for the get-bulk-request \textbf{-nonrepeaters} is 0. The non-repeaters value specifies the number of variables in the variable-bindings list for which a single successor is to be returned. \item The default value for the get-bulk-request \textbf{-maxrepetitions} is 0. The max-repetitions value specifies the number of successors to be returned for the remaining variables in the variable-bindings list. \item The \textbf{-varbindlist} argument expects an array reference consisting of a list of OBJECT IDENTIFIERs in dotted notation. Each OBJECT IDENTIFER is placed into a single SNMP GetBulkRequest-PDU in the same order that it held in the original list. \end{itemize} A reference to a hash is returned in blocking mode which contains the contents of the VarBindList. In non-blocking mode, a true value is returned when no error has occurred. In either mode, the undefined value is returned when an error has occurred. The \texttt{error()} method may be used to determine the cause of the failure. \textbf{NOTE:} This method can only be used when the version of the object is set to SNMPv2c or SNMPv3. \subsection*{inform\_request() - send an inform-request to the remote manager\label{inform_request_-_send_an_inform-request_to_the_remote_manager}\index{inform\ request() - send an inform-request to the remote manager}} \begin{verbatim} $result = $session->inform_request( [-callback => sub {},] # non-blocking [-delay => $seconds,] # non-blocking [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 -varbindlist => \@oid_value, ); \end{verbatim} This method is used to provide management information to the remote manager associated with the Net::SNMP object using an inform-request. The message is built using a list of values consisting of groups of an OBJECT IDENTIFIER, an object type, and the actual value to be identified. This list is passed to the method as an array reference using the \textbf{-varbindlist} argument. The OBJECT IDENTIFIERs in each trio are to be in dotted notation. The object type is an octet corresponding to the ASN.1 type of value that is to be identified. Each of the supported ASN.1 types have been defined and are exported by the package by default (see \S\ref{EXPORTS}). The first two variable-bindings fields in the inform-request are specified by SNMPv2 and should be: \begin{itemize} \item sysUpTime.0 - ('1.3.6.1.2.1.1.3.0', TIMETICKS, \$timeticks) \item snmpTrapOID.0 - ('1.3.6.1.6.3.1.1.4.1.0', OBJECT\_IDENTIFIER, \$oid) \end{itemize} A reference to a hash is returned in blocking mode which contains the contents of the VarBindList. In non-blocking mode, a true value is returned when no error has occurred. In either mode, the undefined value is returned when an error has occurred. The \texttt{error()} method may be used to determine the cause of the failure. \textbf{NOTE:} This method can only be used when the version of the object is set to SNMPv2c or SNMPv3. \subsection*{snmpv2\_trap() - send a snmpV2-trap to the remote manager\label{snmpv2_trap_-_send_a_snmpV2-trap_to_the_remote_manager}\index{snmpv2\ trap() - send a snmpV2-trap to the remote manager}} \begin{verbatim} $result = $session->snmpv2_trap( [-delay => $seconds,] # non-blocking -varbindlist => \@oid_value, ); \end{verbatim} This method sends a snmpV2-trap to the remote manager associated with the Net::SNMP object. The message is built using a list of values consisting of groups of an OBJECT IDENTIFIER, an object type, and the actual value to be identified. This list is passed to the method as an array reference using the \textbf{-varbindlist} argument. The OBJECT IDENTIFIERs in each trio are to be in dotted notation. The object type is an octet corresponding to the ASN.1 type of value that is to be identified. Each of the supported ASN.1 types have been defined and are exported by the package by default (see \S\ref{EXPORTS}). The first two variable-bindings fields in the snmpV2-trap are specified by SNMPv2 and should be: \begin{itemize} \item sysUpTime.0 - ('1.3.6.1.2.1.1.3.0', TIMETICKS, \$timeticks) \item snmpTrapOID.0 - ('1.3.6.1.6.3.1.1.4.1.0', OBJECT\_IDENTIFIER, \$oid) \end{itemize} A true value is returned when the method is successful. The undefined value is returned when a failure has occurred. The \texttt{error()} method can be used to determine the cause of the failure. Since there are no acknowledgements for SNMPv2-Trap-PDUs, there is no way to determine if the remote host actually received the snmpV2-trap. \textbf{NOTE:} When the object is in non-blocking mode, the snmpV2-trap is not sent until the event loop is entered and no callback is ever executed. \textbf{NOTE:} This method can only be used when the version of the object is set to SNMPv2c. SNMPv2-Trap-PDUs are supported by SNMPv3, but require the sender of the message to be an authoritative SNMP engine which is not currently supported by the Net::SNMP module. \subsection*{get\_table() - retrieve a table from the remote agent\label{get_table_-_retrieve_a_table_from_the_remote_agent}\index{get\ table() - retrieve a table from the remote agent}} \begin{verbatim} $result = $session->get_table( [-callback => sub {},] # non-blocking [-delay => $seconds,] # non-blocking [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 -baseoid => $oid, [-maxrepetitions => $max_reps,] # v2c/v3 ); \end{verbatim} This method performs repeated SNMP get-next-request or get-bulk-request (when using SNMPv2c or SNMPv3) queries to gather data from the remote agent on the host associated with the Net::SNMP object. The first message sent is built using the OBJECT IDENTIFIER in dotted notation passed to the method by the \textbf{-baseoid} argument. Repeated SNMP requests are issued until the OBJECT IDENTIFER in the response is no longer a child of the base OBJECT IDENTIFIER. The \textbf{-maxrepetitions} argument can be used to specify the max-repetitions value that is passed to the get-bulk-requests when using SNMPv2c or SNMPv3. If this argument is not present, a value is calculated based on the maximum message size for the Net::SNMP object. A reference to a hash is returned in blocking mode which contains the contents of the VarBindList. In non-blocking mode, a true value is returned when no error has occurred. In either mode, the undefined value is returned when an error has occurred. The \texttt{error()} method may be used to determine the cause of the failure. \textbf{WARNING:} Results from this method can become very large if the base OBJECT IDENTIFIER is close to the root of the SNMP MIB tree. \subsection*{get\_entries() - retrieve table entries from the remote agent\label{get_entries_-_retrieve_table_entries_from_the_remote_agent}\index{get\ entries() - retrieve table entries from the remote agent}} \begin{verbatim} $result = $session->get_entries( [-callback => sub {},] # non-blocking [-delay => $seconds,] # non-blocking [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 -columns => \@columns, [-startindex => $start,] [-endindex => $end,] [-maxrepetitions => $max_reps,] # v2c/v3 ); \end{verbatim} This method performs repeated SNMP get-next-request or get-bulk-request (when using SNMPv2c or SNMPv3) queries to gather data from the remote agent on the host associated with the Net::SNMP object. Each message specifically requests data for each OBJECT IDENTIFIER specified in the \textbf{-columns} array. The OBJECT IDENTIFIERs must correspond to column entries for a conceptual row in a table. They may however be columns in different tables as long as each table is indexed the same way. The optional \textbf{-startindex} and \textbf{-endindex} arguments may be specified to limit the query to specific rows in the table(s). The \textbf{-startindex} can be specified as a single decimal value or in dotted notation if the index associated with the entry so requires. If the \textbf{-startindex} is specified, it will be include as part of the query results. If no \textbf{-startindex} is specified, the first request message will be sent without an index. To insure that the \textbf{-startindex} is included, the last subidentifier in the index is decremented by one. If the last subidentifier has a value of zero, the subidentifier is removed from the index. The optional \textbf{-endindex} argument can be specified as a single decimal value or in dotted notation. If the \textbf{-endindex} is specified, it will be included as part of the query results. If no \textbf{-endindex} is specified, repeated SNMP requests are issued until the response no longer returns entries matching any of the columns specified in the \textbf{-columns} array. The \textbf{-maxrepetitions} argument can be used to specify the max-repetitions value that is passed to the get-bulk-requests when using SNMPv2c or SNMPv3. If this argument is not present, a value is calculated based on the maximum message size of the object and the number of columns specified in the \textbf{-columns} array. A reference to a hash is returned in blocking mode which contains the contents of the VarBindList. In non-blocking mode, a true value is returned when no error has occurred. In either mode, the undefined value is returned when an error has occurred. The \texttt{error()} method may be used to determine the cause of the failure. \subsection*{version() - get the SNMP version from the object\label{version_-_get_the_SNMP_version_from_the_object}\index{version() - get the SNMP version from the object}} \begin{verbatim} $rfc_version = $session->version; \end{verbatim} This method returns the current value for the SNMP version associated with the object. The returned value is the corresponding version number defined by the RFCs for the protocol version field (i.e. SNMPv1 == 0, SNMPv2c == 1, and SNMPv3 == 3). The RFC versions are defined as constant by the module and can be exported by request (see \S\ref{EXPORTS}). \subsection*{error() - get the current error message from the object\label{error_-_get_the_current_error_message_from_the_object}\index{error() - get the current error message from the object}} \begin{verbatim} $error_message = $session->error; \end{verbatim} This method returns a text string explaining the reason for the last error. An empty string is returned if no error has occurred. \subsection*{hostname() - get the hostname associated with the object\label{hostname_-_get_the_hostname_associated_with_the_object}\index{hostname() - get the hostname associated with the object}} \begin{verbatim} $hostname = $session->hostname; \end{verbatim} This method returns the hostname string that is associated with the object as it was passed to the \texttt{session()} constructor. \subsection*{error\_status() - get the current SNMP error-status from the object\label{error_status_-_get_the_current_SNMP_error-status_from_the_object}\index{error\ status() - get the current SNMP error-status from the object}} \begin{verbatim} $error_status = $session->error_status; \end{verbatim} This method returns the numeric value of the error-status contained in the last SNMP GetResponse-PDU received by the object. \subsection*{error\_index() - get the current SNMP error-index from the object\label{error_index_-_get_the_current_SNMP_error-index_from_the_object}\index{error\ index() - get the current SNMP error-index from the object}} \begin{verbatim} $error_index = $session->error_index; \end{verbatim} This method returns the numeric value of the error-index contained in the last SNMP GetResponse-PDU received by the object. \subsection*{var\_bind\_list() - get the hash reference to the last SNMP response\label{var_bind_list_-_get_the_hash_reference_to_the_last_SNMP_response}\index{var\ bind\ list() - get the hash reference to the last SNMP response}} \begin{verbatim} $response = $session->var_bind_list; \end{verbatim} This method returns a hash reference created using the ObjectName and the ObjectSyntax pairs in the VarBindList of the last SNMP GetResponse-PDU received by the object. The keys of the hash consist of the OBJECT IDENTIFIERs in dotted notation corresponding to each ObjectName in the VarBindList. If any of the OBJECT IDENTIFIERs passed to the request method began with a leading dot, all of the OBJECT IDENTIFIER hash keys will be prefixed with a leading dot. The value of each hash entry is set equal to the value of the corresponding ObjectSyntax. The undefined value is returned if there has been a failure and the \texttt{error()} method may be used to determine the reason. \subsection*{var\_bind\_names() - get the array of the ObjectNames in the last response\label{var_bind_names_-_get_the_array_of_the_ObjectNames_in_the_last_response}\index{var\ bind\ names() - get the array of the ObjectNames in the last response}} \begin{verbatim} @names = $session->var_bind_names; \end{verbatim} This method returns an array containing the OBJECT IDENTIFIERs corresponding to the ObjectNames in the VarBindList in the order that they were received in the last SNMP GetResponse-PDU. The entries in the array will map directly to the keys in the hash reference returned by the methods that perform SNMP message exchanges and by the \texttt{var\_bind\_list()} method. The array returned for the convenience methods \texttt{get\_table()} and \texttt{get\_entries()} will be in lexicographical order. An empty array is returned if there has been a failure and the \texttt{error()} method may be used to determine reason. \subsection*{timeout() - set or get the current timeout period for the object\label{timeout_-_set_or_get_the_current_timeout_period_for_the_object}\index{timeout() - set or get the current timeout period for the object}} \begin{verbatim} $seconds = $session->timeout([$seconds]); \end{verbatim} This method returns the current value for the Transport Layer timeout for the Net::SNMP object. This value is the number of seconds that the object will wait for a response from the agent on the remote host. The default timeout is 5.0 seconds. If a parameter is specified, the timeout for the object is set to the provided value if it falls within the range 1.0 to 60.0 seconds. The undefined value is returned upon an error and the \texttt{error()} method may be used to determine the cause. \subsection*{retries() - set or get the current retry count for the object\label{retries_-_set_or_get_the_current_retry_count_for_the_object}\index{retries() - set or get the current retry count for the object}} \begin{verbatim} $count = $session->retries([$count]); \end{verbatim} This method returns the current value for the number of times to retry sending a SNMP message to the remote host. The default number of retries is 1. If a parameter is specified, the number of retries for the object is set to the provided value if it falls within the range 0 to 20. The undefined value is returned upon an error and the \texttt{error()} method may be used to determine the cause. \subsection*{max\_msg\_size() - set or get the current maxMsgSize for the object\label{max_msg_size_-_set_or_get_the_current_maxMsgSize_for_the_object}\index{max\ msg\ size() - set or get the current maxMsgSize for the object}} \begin{verbatim} $octets = $session->max_msg_size([$octets]); \end{verbatim} This method returns the current value for the maximum message size (maxMsgSize) for the Net::SNMP object. This value is the largest message size in octets that can be prepared or processed by the object. The default maxMsgSize is 1472 octets. If a parameter is specified, the maxMsgSize is set to the provided value if it falls within the range 484 to 2147483647 octets. The undefined value is returned upon an error and the \texttt{error()} method may be used to determine the cause. \textbf{NOTE:} When using SNMPv3, the maxMsgSize is actually contained in the SNMP message (as msgMaxSize). If the value received from a remote device is less than the current maxMsgSize, the size is automatically adjusted to be the lower value. \subsection*{translate() - enable or disable the translation mode for the object\label{translate_-_enable_or_disable_the_translation_mode_for_the_object}\index{translate() - enable or disable the translation mode for the object}} \begin{verbatim} $mask = $session->translate([ $mode | [ # Perl anonymous ARRAY reference ['-all' => $mode0,] ['-none' => $mode1,] ['-octetstring => $mode2,] ['-null' => $mode3,] ['-timeticks' => $mode4,] ['-opaque' => $mode5,] ['-nosuchobject' => $mode6,] ['-nosuchinstance' => $mode7,] ['-endofmibview' => $mode8,] ['-unsigned' => $mode9] ] ]); \end{verbatim} When the object decodes the GetResponse-PDU that is returned in response to a SNMP message, certain values are translated into a more "human readable" form. By default the following translations occur: \begin{itemize} \item OCTET STRINGs and Opaques containing non-printable ASCII characters are converted into a hexadecimal representation prefixed with "0x". \textbf{NOTE:} The following ASCII control characters are considered to be printable by the module: NUL(0x00), HT(0x09), LF(0x0A), FF(0x0C), and CR(0x0D). \item TimeTicks integer values are converted to a time format. \item NULL values return the string "NULL" instead of an empty string. \item noSuchObject exception values return the string "noSuchObject" instead of an empty string. If translation is not enabled, the SNMP error-status field is set to 128 which is equal to the exported definition NOSUCHOBJECT (see \S\ref{EXPORTS}). \item noSuchInstance exception values return the string "noSuchInstance" instead of an empty string. If translation is not enabled, the SNMP error-status field is set to 129 which is equal to the exported definition NOSUCHINSTANCE (see \S\ref{EXPORTS}). \item endOfMibView exception values return the string "endOfMibView" instead of an empty string. If translation is not enabled, the SNMP error-status field is set to 130 which is equal to the exported definition ENDOFMIBVIEW (see \S\ref{EXPORTS}). \item Counter64, Counter, Gauge, and TimeTick values that have been incorrectly encoded as signed negative values are returned as unsigned values. \end{itemize} The \texttt{translate()} method can be invoked with two different types of arguments. If the argument passed is any Perl variable type except an array reference, the translation mode for all ASN.1 types is set to either enabled or disabled, depending on the value of the passed parameter. Any value that Perl would treat as a true value will set the mode to be enabled for all types, while a false value will disable translation for all types. A reference to an array can be passed to the \texttt{translate()} method in order to define the translation mode on a per ASN.1 type basis. The array is expected to contain a list of named argument pairs for each ASN.1 type that is to be modified. The arguments in the list are applied in the order that they are passed in via the array. Arguments at the end of the list supercede those passed earlier in the list. The argument "-all" can be used to specify that the mode is to apply to all ASN.1 types. Only the arguments for the ASN.1 types that are to be modified need to be included in the list. The \texttt{translate()} method returns a bit mask indicating which ASN.1 types are to be translated. Definitions of the bit to ASN.1 type mappings can be exported using the \textit{:translate} tag (see \S\ref{EXPORTS}). The undefined value is returned upon an error and the \texttt{error()} method may be used to determine the cause. \subsection*{debug() - set or get the debug mode for the module\label{debug_-_set_or_get_the_debug_mode_for_the_module}\index{debug() - set or get the debug mode for the module}} \begin{verbatim} $mask = $session->debug([$mask]); \end{verbatim} This method is used to enable or disable debugging for the Net::SNMP module. Debugging can be enabled on a per component level as defined by a bit mask passed to the \texttt{debug()} method. The bit mask is broken up as follows: \begin{itemize} \item 0x02 - Message or PDU encoding and decoding \item 0x04 - Transport Layer \item 0x08 - Dispatcher \item 0x10 - Message Processing \item 0x20 - Security \end{itemize} Symbols representing these bit mask values are defined by the module and can be exported using the \textit{:debug} tag (see \S\ref{EXPORTS}). If a non-numeric value is passed to the \texttt{debug()} method, it is evaluated in boolean context. Debugging for all of the components is then enabled or disabled based on the resulting truth value. The current debugging mask is returned by the method. Debugging can also be enabled using the stand alone function \texttt{snmp\_debug()}. This function can be exported by request (see \S\ref{EXPORTS}). \section{FUNCTIONS\label{FUNCTIONS}\index{FUNCTIONS}} \subsection*{oid\_base\_match() - determine if an OID has a specified OID base\label{oid_base_match_-_determine_if_an_OID_has_a_specified_OID_base}\index{oid\ base\ match() - determine if an OID has a specified OID base}} \begin{verbatim} $value = oid_base_match($base_oid, $oid); \end{verbatim} This function takes two OBJECT IDENTIFIERs in dotted notation and returns a true value (i.e. 0x1) if the second OBJECT IDENTIFIER is equal to or is a child of the first OBJECT IDENTIFIER in the SNMP Management Information Base (MIB). This function can be used in conjunction with the \texttt{get-next-request()} or \texttt{get-bulk-request()} methods to determine when a OBJECT IDENTIFIER in the GetResponse-PDU is no longer in the desired MIB tree branch. \subsection*{oid\_lex\_sort() - sort a list of OBJECT IDENTIFIERs lexicographically\label{oid_lex_sort_-_sort_a_list_of_OBJECT_IDENTIFIERs_lexicographically}\index{oid\ lex\ sort() - sort a list of OBJECT IDENTIFIERs lexicographically}} \begin{verbatim} @sorted_oids = oid_lex_sort(@oids); \end{verbatim} This function takes a list of OBJECT IDENTIFIERs in dotted notation and returns the listed sorted in lexicographical order. \subsection*{ticks\_to\_time() - convert TimeTicks to formatted time\label{ticks_to_time_-_convert_TimeTicks_to_formatted_time}\index{ticks\ to\ time() - convert TimeTicks to formatted time}} \begin{verbatim} $time = ticks_to_time($timeticks); \end{verbatim} This function takes an ASN.1 TimeTicks value and returns a string representing the time defined by the value. The TimeTicks value is expected to be a non-negative integer value representing the time in hundredths of a second since some epoch. The returned string will display the time in days, hours, and seconds format according to the value of the TimeTicks argument. \section{EXPORTS\label{EXPORTS}\index{EXPORTS}} The Net::SNMP module uses the \emph{Exporter} module to export useful constants and subroutines. These exportable symbols are defined below and follow the rules and conventions of the \emph{Exporter} module (see \emph{Exporter}). \begin{description} \item[Default] \mbox{} \&snmp\_dispatcher, INTEGER, INTEGER32, OCTET\_STRING, OBJECT\_IDENTIFIER, IPADDRESS, COUNTER, COUNTER32, GAUGE, GAUGE32, UNSIGNED32, TIMETICKS, OPAQUE, COUNTER64, NOSUCHOBJECT, NOSUCHINSTANCE, ENDOFMIBVIEW \item[Exportable] \mbox{} \sloppypar \&snmp\_debug, \&snmp\_dispatcher, \&oid\_base\_match, \&oid\_lex\_sort, \&ticks\_to\_time, INTEGER, INTEGER32, OCTET\_STRING, NULL, OBJECT\_IDENTIFIER, SEQUENCE, IPADDRESS, COUNTER, COUNTER32, GAUGE, GAUGE32, UNSIGNED32, TIMETICKS, OPAQUE, COUNTER64, NOSUCHOBJECT, NOSUCHINSTANCE, ENDOFMIBVIEW, GET\_REQUEST, GET\_NEXT\_REQUEST, GET\_RESPONSE, SET\_REQUEST, TRAP, GET\_BULK\_REQUEST, INFORM\_REQUEST, SNMPV2\_TRAP, DEBUG\_ALL, DEBUG\_NONE, DEBUG\_MESSAGE, DEBUG\_TRANSPORT, DEBUG\_DISPATCHER, DEBUG\_PROCESSING, DEBUG\_SECURITY, COLD\_START, WARM\_START, LINK\_DOWN, LINK\_UP, AUTHENTICATION\_FAILURE, EGP\_NEIGHBOR\_LOSS, ENTERPRISE\_SPECIFIC, SNMP\_VERSION\_1, SNMP\_VERSION\_2C, SNMP\_VERSION\_3, SNMP\_PORT, SNMP\_TRAP\_PORT, TRANSLATE\_NONE, TRANSLATE\_OCTET\_STRING, TRANSLATE\_NULL, TRANSLATE\_TIMETICKS, TRANSLATE\_OPAQUE, TRANSLATE\_NOSUCHOBJECT, TRANSLATE\_NOSUCHINSTANCE, TRANSLATE\_ENDOFMIBVIEW, TRANSLATE\_UNSIGNED, TRANSLATE\_ALL \item[Tags] \mbox{}\begin{description} \item[:asn1] \mbox{} INTEGER, INTEGER32, OCTET\_STRING, NULL, OBJECT\_IDENTIFIER, SEQUENCE, IPADDRESS, COUNTER, COUNTER32, GAUGE, GAUGE32, UNSIGNED32, TIMETICKS, OPAQUE, COUNTER64, NOSUCHOBJECT, NOSUCHINSTANCE, ENDOFMIBVIEW, GET\_REQUEST, GET\_NEXT\_REQUEST, GET\_RESPONSE, SET\_REQUEST, TRAP, GET\_BULK\_REQUEST, INFORM\_REQUEST, SNMPV2\_TRAP \item[:debug] \mbox{} \&snmp\_debug, DEBUG\_ALL, DEBUG\_NONE, DEBUG\_MESSAGE, DEBUG\_TRANSPORT, DEBUG\_DISPATCHER, DEBUG\_PROCESSING, DEBUG\_SECURITY \item[:generictrap] \mbox{} COLD\_START, WARM\_START, LINK\_DOWN, LINK\_UP, AUTHENTICATION\_FAILURE, EGP\_NEIGHBOR\_LOSS, ENTERPRISE\_SPECIFIC \item[:snmp] \mbox{} \sloppypar \&snmp\_debug, \&snmp\_dispatcher, \&oid\_base\_match, \&oid\_lex\_sort, \&ticks\_to\_time, SNMP\_VERSION\_1, SNMP\_VERSION\_2C, SNMP\_VERSION\_3, SNMP\_PORT, SNMP\_TRAP\_PORT \item[:translate] \mbox{} \sloppypar TRANSLATE\_NONE, TRANSLATE\_OCTET\_STRING, TRANSLATE\_NULL, TRANSLATE\_TIMETICKS, TRANSLATE\_OPAQUE, TRANSLATE\_NOSUCHOBJECT, TRANSLATE\_NOSUCHINSTANCE, TRANSLATE\_ENDOFMIBVIEW, TRANSLATE\_UNSIGNED, TRANSLATE\_ALL \item[:ALL] \mbox{} All of the above exportable items. \end{description} \end{description} \section{EXAMPLES\label{EXAMPLES}\index{examples of Net::SNMP}} \subsection*{1. Blocking SNMPv1 get-request for sysUpTime\label{1_Blocking_SNMPv1_get-request_for_sysUpTime}\index{1. Blocking SNMPv1 get-request for sysUpTime}} This example gets the sysUpTime from a remote host. \begin{verbatim} #! /usr/local/bin/perl \end{verbatim} \begin{verbatim} use strict; \end{verbatim} \begin{verbatim} use Net::SNMP; \end{verbatim} \begin{verbatim} my ($session, $error) = Net::SNMP->session( -hostname => shift || 'localhost', -community => shift || 'public', -port => shift || 161 ); \end{verbatim} \begin{verbatim} if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } \end{verbatim} \begin{verbatim} my $sysUpTime = '1.3.6.1.2.1.1.3.0'; \end{verbatim} \begin{verbatim} my $result = $session->get_request( -varbindlist => [$sysUpTime] ); \end{verbatim} \begin{verbatim} if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } \end{verbatim} \begin{verbatim} printf("sysUpTime for host '%s' is %s\n", $session->hostname, $result->{$sysUpTime} ); \end{verbatim} \begin{verbatim} $session->close; \end{verbatim} \begin{verbatim} exit 0; \end{verbatim} \subsection*{2. Blocking SNMPv3 set-request of sysContact\label{2_Blocking_SNMPv3_set-request_of_sysContact}\index{2. Blocking SNMPv3 set-request of sysContact}} This example sets the sysContact information on the remote host to "Help Desk x911". The named arguments passed to the \texttt{session()} constructor are for the demonstration of syntax only. These parameters will need to be set according to the SNMPv3 parameters of the remote host used by the script. \begin{verbatim} #! /usr/local/bin/perl \end{verbatim} \begin{verbatim} use strict; \end{verbatim} \begin{verbatim} use Net::SNMP; \end{verbatim} \begin{verbatim} my ($session, $error) = Net::SNMP->session( -hostname => 'myv3host.company.com', -version => 'snmpv3', -username => 'myv3Username', -authkey => '0x05c7fbde31916f64da4d5b77156bdfa7', -authprotocol => 'md5', -privkey => '0x93725fd3a02a48ce02df4e065a1c1746' ); \end{verbatim} \begin{verbatim} if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } \end{verbatim} \begin{verbatim} my $sysContact = '1.3.6.1.2.1.1.4.0'; \end{verbatim} \begin{verbatim} my $result = $session->set_request( -varbindlist => [$sysContact, OCTET_STRING, 'Help Desk x911'] ); \end{verbatim} \begin{verbatim} if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } \end{verbatim} \begin{verbatim} printf("sysContact for host '%s' set to '%s'\n", $session->hostname, $result->{$sysContact} ); \end{verbatim} \begin{verbatim} $session->close; \end{verbatim} \begin{verbatim} exit 0; \end{verbatim} \subsection*{3. Non-blocking SNMPv2c get-bulk-request for ifTable\label{3_Non-blocking_SNMPv2c_get-bulk-request_for_ifTable}\index{3. Non-blocking SNMPv2c get-bulk-request for ifTable}} This example gets the contents of the ifTable by sending get-bulk-requests until the responses are no longer part of the ifTable. The ifTable can also be retrieved using the \texttt{get\_table()} method. \begin{verbatim} #! /usr/local/bin/perl \end{verbatim} \begin{verbatim} use strict; \end{verbatim} \begin{verbatim} use Net::SNMP qw(:snmp); \end{verbatim} \begin{verbatim} my ($session, $error) = Net::SNMP->session( -version => 'snmpv2c', -nonblocking => 1, -hostname => shift || 'localhost', -community => shift || 'public', -port => shift || 161 ); \end{verbatim} \begin{verbatim} if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } \end{verbatim} \begin{verbatim} my $ifTable = '1.3.6.1.2.1.2.2'; \end{verbatim} \begin{verbatim} my $result = $session->get_bulk_request( -callback => [\&table_cb, {}], -maxrepetitions => 10, -varbindlist => [$ifTable] ); \end{verbatim} \begin{verbatim} if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } \end{verbatim} \begin{verbatim} snmp_dispatcher(); \end{verbatim} \begin{verbatim} $session->close; \end{verbatim} \begin{verbatim} exit 0; \end{verbatim} \begin{verbatim} sub table_cb { my ($session, $table) = @_; \end{verbatim} \begin{verbatim} if (!defined($session->var_bind_list)) { \end{verbatim} \begin{verbatim} printf("ERROR: %s\n", $session->error); \end{verbatim} \begin{verbatim} } else { \end{verbatim} \begin{verbatim} # Loop through each of the OIDs in the response and assign # the key/value pairs to the anonymous hash that is passed # to the callback. Make sure that we are still in the table # before assigning the key/values. \end{verbatim} \begin{verbatim} my $next; \end{verbatim} \begin{verbatim} foreach my $oid (oid_lex_sort(keys(%{$session->var_bind_list}))) { if (!oid_base_match($ifTable, $oid)) { $next = undef; last; } $next = $oid; $table->{$oid} = $session->var_bind_list->{$oid}; } \end{verbatim} \begin{verbatim} # If $next is defined we need to send another request # to get more of the table. \end{verbatim} \begin{verbatim} if (defined($next)) { \end{verbatim} \begin{verbatim} $result = $session->get_bulk_request( -callback => [\&table_cb, $table], -maxrepetitions => 10, -varbindlist => [$next] ); \end{verbatim} \begin{verbatim} if (!defined($result)) { printf("ERROR: %s\n", $session->error); } \end{verbatim} \begin{verbatim} } else { \end{verbatim} \begin{verbatim} # We are no longer in the table, so print the results. \end{verbatim} \begin{verbatim} foreach my $oid (oid_lex_sort(keys(%{$table}))) { printf("%s => %s\n", $oid, $table->{$oid}); } \end{verbatim} \begin{verbatim} } } } \end{verbatim} \subsection*{4. Non-blocking SNMPv1 get-request for sysUpTime on multiple hosts\label{4_Non-blocking_SNMPv1_get-request_for_sysUpTime_on_multiple_hosts}\index{4. Non-blocking SNMPv1 get-request for sysUpTime on multiple hosts}} This example polls several hosts for their sysUpTime using non-blocking objects and reports a warning if this value is less than the value from the last poll. \begin{verbatim} #! /usr/local/bin/perl \end{verbatim} \begin{verbatim} use strict; \end{verbatim} \begin{verbatim} use Net::SNMP qw(snmp_dispatcher ticks_to_time); \end{verbatim} \begin{verbatim} # List of hosts to poll \end{verbatim} \begin{verbatim} my @HOSTS = qw(1.1.1.1 1.1.1.2 localhost); \end{verbatim} \begin{verbatim} # Poll interval (in seconds). This value should be greater # than the number of retries plus one, times the timeout value. \end{verbatim} \begin{verbatim} my $INTERVAL = 60; \end{verbatim} \begin{verbatim} # Maximum number of polls, including the initial poll. \end{verbatim} \begin{verbatim} my $MAX_POLLS = 10; \end{verbatim} \begin{verbatim} my $sysUpTime = '1.3.6.1.2.1.1.3.0'; \end{verbatim} \begin{verbatim} # Create a session for each host and queue the first get-request. \end{verbatim} \begin{verbatim} foreach my $host (@HOSTS) { \end{verbatim} \begin{verbatim} my ($session, $error) = Net::SNMP->session( -hostname => $host, -nonblocking => 0x1, # Create non-blocking objects -translate => [ -timeticks => 0x0 # Turn off so sysUpTime is numeric ] ); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } \end{verbatim} \begin{verbatim} # Queue the get-request, passing references to variables that # will be used to store the last sysUpTime and the number of # polls that this session has performed. \end{verbatim} \begin{verbatim} my ($last_uptime, $num_polls) = (0, 0); \end{verbatim} \begin{verbatim} $session->get_request( -varbindlist => [$sysUpTime], -callback => [ \&validate_sysUpTime_cb, \$last_uptime, \$num_polls ] ); \end{verbatim} \begin{verbatim} } \end{verbatim} \begin{verbatim} # Define a reference point for all of the polls my $EPOC = time(); \end{verbatim} \begin{verbatim} # Enter the event loop snmp_dispatcher(); \end{verbatim} \begin{verbatim} exit 0; \end{verbatim} \begin{verbatim} sub validate_sysUpTime_cb { my ($session, $last_uptime, $num_polls) = @_; \end{verbatim} \begin{verbatim} if (!defined($session->var_bind_list)) { \end{verbatim} \begin{verbatim} printf("%-15s ERROR: %s\n", $session->hostname, $session->error); \end{verbatim} \begin{verbatim} } else { \end{verbatim} \begin{verbatim} # Validate the sysUpTime \end{verbatim} \begin{verbatim} my $uptime = $session->var_bind_list->{$sysUpTime}; \end{verbatim} \begin{verbatim} if ($uptime < ${$last_uptime}) { printf("%-15s WARNING: %s is less than %s\n", $session->hostname, ticks_to_time($uptime), ticks_to_time(${$last_uptime}) ); } else { printf("%-15s Ok (%s)\n", $session->hostname, ticks_to_time($uptime) ); } \end{verbatim} \begin{verbatim} # Store the new sysUpTime ${$last_uptime} = $uptime; \end{verbatim} \begin{verbatim} } \end{verbatim} \begin{verbatim} # Queue the next message if we have not reached $MAX_POLLS. # Since we do not provide a -callback argument, the same # callback and it's original arguments will be used. \end{verbatim} \begin{verbatim} if (++${$num_polls} < $MAX_POLLS) { my $delay = (($INTERVAL * ${$num_polls}) + $EPOC) - time(); $session->get_request( -delay => ($delay >= 0) ? $delay : 0, -varbindlist => [$sysUpTime] ); } \end{verbatim} \begin{verbatim} $session->error_status; } \end{verbatim} \section{REQUIREMENTS\label{REQUIREMENTS}\index{requirements of Net::SNMP}} \begin{itemize} \item The Net::SNMP module uses syntax that is not supported in versions of Perl earlier than v5.6.0. \item The non-core modules \emph{Crypt::DES}, \emph{Digest::MD5}, \emph{Digest::SHA1}, and \emph{Digest::HMAC} are required to support SNMPv3. \item In order to support the AES Cipher Algorithm as a SNMPv3 privacy protocol, the non-core module \emph{Crypt::Rijndael} is needed. \end{itemize} \section{AUTHOR\label{AUTHOR}} David M. Town $<$dtown@cpan.org$>$ \section{ACKNOWLEDGMENTS\label{ACKNOWLEDGMENTS}} The original concept for this module was based on \emph{SNMP\_Session.pm} written by Simon Leinen $<$simon@switch.ch$>$. The Abstract Syntax Notation One (ASN.1) encode and decode methods were derived by example from the CMU SNMP package whose copyright follows: Copyright (c) 1988, 1989, 1991, 1992 by Carnegie Mellon University. All rights reserved. \section{COPYRIGHT\label{COPYRIGHT}} Copyright (c) 1998-2003 David M. Town. All rights reserved. This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.