Class: SNMP::Open

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp/open.rb,
lib/snmp/open/parser.rb,
lib/snmp/open/options.rb,
lib/snmp/open/version.rb,
lib/snmp/open/file_reader.rb,
lib/snmp/open/command_reader.rb,
lib/snmp/open/parser/constants.rb,
lib/snmp/open/parser/value_parser.rb

Overview

Open3-based wrapper for SNMP CLI commands

Defined Under Namespace

Classes: CommandError, CommandReader, CommandTimeoutError, FileReader, Options, Parser, UnknownMIBError, UnknownOIDError, Value

Constant Summary collapse

VERSION =
'0.8.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Open

see CommandReader for a description of options



25
26
27
# File 'lib/snmp/open.rb', line 25

def initialize(options = {})
  @reader = options[:reader] || CommandReader.new(options)
end

Instance Attribute Details

#readerObject (readonly)

Returns the value of attribute reader.



22
23
24
# File 'lib/snmp/open.rb', line 22

def reader
  @reader
end

Instance Method Details

#get(oids, &block) ⇒ Object

Perform an SNMP get using the “snmpget” command and parse the output



30
31
32
33
34
35
# File 'lib/snmp/open.rb', line 30

def get(oids, &block)
  return enum_for(:get, oids) unless block_given?

  texts = oids.map { |oid| reader.capture(:get, oid) }
  Parser.new(oids).parse(texts).fetch(0, []).each(&block)
end

#walk(oids, **kwargs) ⇒ Object

Perform an SNMP walk using the “snmpwalk” or “snmpbulkwalk” commands and parse the output



39
40
41
42
43
44
45
46
47
# File 'lib/snmp/open.rb', line 39

def walk(oids, **kwargs)
  return enum_for(:walk, oids, **kwargs) unless block_given?

  bulk = kwargs.fetch(:bulk, true)
  options = walk_options(bulk, **kwargs)
  cmd = bulk ? :bulkwalk : :walk
  texts = oids.map { |oid| reader.capture(cmd, oid, options) }
  Parser.new(oids).parse(texts).each { |*args| yield(*args) }
end

#walk_options(bulk, **kwargs) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/snmp/open.rb', line 49

def walk_options(bulk, **kwargs)
  if bulk
    {
      '-Cn' => kwargs.fetch(:non_repeaters, 0),
      '-Cr' => kwargs.fetch(:max_repetitions, 10)
    }
  else
    {}
  end
end