Class: NETSNMP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/netsnmp/client.rb

Overview

Main Entity, provides the user-facing API to communicate with SNMP Agents

Under the hood it creates a “session” (analogous to the net-snmp C session), which will be used to proxy all the communication to the agent. the Client ensures that you only write pure ruby and read pure ruby, not concerning with snmp-speak like PDUs, varbinds and the like.

Constant Summary collapse

RETRIES =
5

Instance Method Summary collapse

Constructor Details

#initialize(**options) {|client| ... } ⇒ Client

Returns a new instance of Client.

Examples:

Yielding a clinet

NETSNMP::Client.new(host: "241.232.22.12") do |client|
  puts client.get(oid: "1.3.6.1.2.1.1.5.0")
end

Options Hash (**options):

  • :version (String, Integer, nil)

    the version of the protocol (defaults to 3). also accepts common known declarations like :v3, “v2c”, etc

  • :retries (Integer)

    number of retries for each failed PDU (after which it raise timeout error. Defaults to RETRIES retries)

Yields:

  • (client)

    the instantiated client, after which it closes it for use.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/netsnmp/client.rb', line 23

def initialize(**options)
  version = options[:version]
  version = case version 
    when Integer then version # assume the use know what he's doing
    when /v?1/ then 0 
    when /v?2c?/ then 1 
    when /v?3/, nil then 3
  end

  @retries = options.fetch(:retries, RETRIES)
  @session ||= version == 3 ? V3Session.new(options) : Session.new(options)
  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Instance Method Details

#closeObject

Closes the inner section



44
45
46
# File 'lib/netsnmp/client.rb', line 44

def close
  @session.close
end

#get(oid_opts) {|response| ... } ⇒ Object

Performs an SNMP GET Request

Yields:

  • (response)

See Also:

  • NETSNMP::Client.{NETSNMP{NETSNMP::Varbind{NETSNMP::Varbind#new}


52
53
54
55
56
57
# File 'lib/netsnmp/client.rb', line 52

def get(oid_opts)
  request = @session.build_pdu(:get, oid_opts)
  response = handle_retries { @session.send(request) }
  yield response if block_given?
  response.varbinds.first.value
end

#get_next(oid_opts) {|response| ... } ⇒ Object

Performs an SNMP GETNEXT Request

Yields:

  • (response)

See Also:

  • NETSNMP::Client.{NETSNMP{NETSNMP::Varbind{NETSNMP::Varbind#new}


63
64
65
66
67
68
69
# File 'lib/netsnmp/client.rb', line 63

def get_next(oid_opts)
  request = @session.build_pdu(:getnext, oid_opts)
  response = handle_retries { @session.send(request) }
  yield response if block_given?
  varbind = response.varbinds.first
  [varbind.oid, varbind.value]
end

#set(oid_opts) {|response| ... } ⇒ Object

Perform a SNMP SET Request

Yields:

  • (response)

See Also:

  • NETSNMP::Client.{NETSNMP{NETSNMP::Varbind{NETSNMP::Varbind#new}


127
128
129
130
131
132
# File 'lib/netsnmp/client.rb', line 127

def set(oid_opts)
  request = @session.build_pdu(:set, oid_opts)
  response = handle_retries { @session.send(request) }
  yield response if block_given? 
  response.varbinds.map(&:value)
end

#walk(oid:) ⇒ Enumerator

Perform a SNMP Walk (issues multiple subsequent GENEXT requests within the subtree rooted on an OID)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/netsnmp/client.rb', line 77

def walk(oid: )
  walkoid = oid
  Enumerator.new do |y|
    code = walkoid
    first_response_code = nil
    catch(:walk) do
      loop do
        get_next(oid: code) do |response|
          response.varbinds.each do |varbind|
            code = varbind.oid
            if !OID.parent?(walkoid, code) or 
                varbind.value.eql?(:endofmibview) or
                code == first_response_code
              throw(:walk)             
            else
              y << [code, varbind.value]
            end
            first_response_code ||= code
          end
        end
      end
    end
  end
end