Class: SNMP::Manager

Class | SNMP::Manager
In: | lib/snmp/manager.rb
Parent: | Object

## SNMP Manager

This class provides a manager for interacting with a single SNMP agent.

# Example

       require 'snmp'

       manager = SNMP::Manager.new(:Host => 'localhost', :Port => 1061)
       response = manager.get(["1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0"])
       response.each_varbind {|vb| puts vb.inspect}
       manager.close

## Symbolic Object Names

Symbolic names for SNMP object IDs can be used as parameters to the APIs in this class if the MIB modules are imported and the names of the MIBs are included in the MibModules configuration parameter.

See MIB.varbind_list for a description of valid parameter formats.

The following modules are loaded by default: "SNMPv2-SMI", "SNMPv2-MIB", "IF-MIB", "IP-MIB", "TCP-MIB", "UDP-MIB". All of the current IETF MIBs have been imported and are available for loading.

Additional modules may be imported using the MIB class. The current implementation of the importing code requires that the external ‘smidump’ tool is available in your PATH. This tool can be obtained from the libsmi website at www.ibr.cs.tu-bs.de/projects/libsmi/ .

# Example

Do this once:

      SNMP::MIB.import_module(MY_MODULE_FILENAME, MIB_OUTPUT_DIR)

Include your module in MibModules each time you create a Manager:

      SNMP::Manager.new(:Host => 'localhost', :MibDir => MIB_OUTPUT_DIR,
                        :MibModules => ["MY-MODULE-MIB", "SNMPv2-MIB", ...])

### Methods

close create_trap_vb_list get get_bulk get_next get_value inform load_module new next_request_id= open set trap_v1 trap_v2 walk

### Constants

DefaultConfig | = | { :Host => 'localhost', :Port => 161, :TrapPort => 162, :Community => 'public', :WriteCommunity => nil, :Version => :SNMPv2c, :Timeout => 1, :Retries => 5, :Transport => UDPTransport, :MaxReceiveBytes => 8000, :MibDir => MIB::DEFAULT_MIB_PATH, :MibModules => ["SNMPv2-SMI", "SNMPv2-MIB", "IF-MIB", "IP-MIB", "TCP-MIB", "UDP-MIB"]} |  | Default configuration. Individual options may be overridden when the Manager is created.

### Attributes

config | [R] | Retrieves the current configuration of this Manager.
mib | [R] | Retrieves the MIB for this Manager.

### Public Class methods

new(config = {})
open(config = {}) {|manager| ...}

Creates a Manager but also takes an optional block and automatically closes the transport connection used by this manager after the block completes.

### Public Instance methods

close()

Close the transport connection for this manager.

create_trap_vb_list(sys_up_time, trap_oid, object_list)

Helper method for building VarBindList for trap and inform requests.

get(object_list)

Sends a get request for the supplied list of ObjectId or VarBind objects.

Returns a Response PDU with the results of the request.

get_bulk(non_repeaters, max_repetitions, object_list)

Sends a get-bulk request. The non_repeaters parameter specifies the number of objects in the object_list to be retrieved once. The remaining objects in the list will be retrieved up to the number of times specified by max_repetitions.

get_next(object_list)

Sends a get-next request for the supplied list of ObjectId or VarBind objects.

Returns a Response PDU with the results of the request.

get_value(object_list)

Sends a get request for the supplied list of ObjectId or VarBind objects.

Returns a list of the varbind values only, not the entire response, in the same order as the initial object_list. This method is useful for retrieving scalar values.

For example:

      SNMP::Manager.open(:Host => "localhost") do |manager|
        puts manager.get_value("sysDescr.0")
      end

inform(sys_up_time, trap_oid, object_list=[])

Sends an inform request using the supplied varbind list.

sys_up_time: An integer respresenting the number of hundredths of a second that this system has been up.

trap_oid: An ObjectId or String with the OID identifier for this inform request.

object_list: A list of additional varbinds to send with the inform.

load_module(name)
next_request_id=(request_id)

Set the next request-id instead of letting it be generated automatically. This method is useful for testing and debugging.

set(object_list)

Sends a set request using the supplied list of VarBind objects.

Returns a Response PDU with the results of the request.

trap_v1(enterprise, agent_addr, generic_trap, specific_trap, timestamp, object_list=[])

Sends an SNMPv1 style trap.

enterprise: The enterprise OID from the IANA assigned numbers (www.iana.org/assignments/enterprise-numbers) as a String or an ObjectId.

agent_addr: The IP address of the SNMP agent as a String or IpAddress.

generic_trap: The generic trap identifier. One of :coldStart, :warmStart, :linkDown, :linkUp, :authenticationFailure, :egpNeighborLoss, or :enterpriseSpecific

specific_trap: An integer representing the specific trap type for an enterprise-specific trap.

timestamp: An integer respresenting the number of hundredths of a second that this system has been up.

object_list: A list of additional varbinds to send with the trap.

For example:

      Manager.open(:Version => :SNMPv1) do |snmp|
        snmp.trap_v1(
          "enterprises.9",
          "10.1.2.3",
          :enterpriseSpecific,
           42,
          12345,
          [VarBind.new("1.3.6.1.2.3.4", Integer.new(1))])
     end

trap_v2(sys_up_time, trap_oid, object_list=[])

Sends an SNMPv2c style trap.

sys_up_time: An integer respresenting the number of hundredths of a second that this system has been up.

trap_oid: An ObjectId or String with the OID identifier for this trap.

object_list: A list of additional varbinds to send with the trap.

walk(object_list, index_column=0) {|index_vb| ...}

Walks a list of ObjectId or VarBind objects using get_next until the response to the first OID in the list reaches the end of its MIB subtree.

The varbinds from each get_next are yielded to the given block as they are retrieved. The result is yielded as a VarBind when walking a single object or as a VarBindList when walking a list of objects.

Normally this method is used for walking tables by providing an ObjectId for each column of the table.

For example:

      SNMP::Manager.open(:Host => "localhost") do |manager|
        manager.walk("ifTable") { |vb| puts vb }
      end

      SNMP::Manager.open(:Host => "localhost") do |manager|
        manager.walk(["ifIndex", "ifDescr"]) do |index, descr|
          puts "#{index.value} #{descr.value}"
        end
      end

The index_column identifies the column that will provide the index for each row. This information is used to deal with "holes" in a table (when a row is missing a varbind for one column). A missing varbind is replaced with a varbind with the value NoSuchInstance.

Note: If you are getting back rows where all columns have a value of NoSuchInstance then your index column is probably missing one of the rows. Choose an index column that includes all indexes for the table.

[Validate]