Class: SNMP::Open::CommandReader

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp/open/command_reader.rb

Overview

Open3-based data source that executes an snmp* command and captures the output

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CommandReader

options accepts options dealing with making connections to the host, including all of the options listed in the Options::MAP constant hash. Other options can be given as strings (or any object with a suitable to_s method), e.g., these are equivalent:

SNMP::Open.new(host: hostname, timeout: 3, '-m' => miblist)
SNMP::Open.new(hostname => nil, '-t' => '3', '-m' => miblist)


17
18
19
20
21
22
23
24
25
# File 'lib/snmp/open/command_reader.rb', line 17

def initialize(options)
  @env = options.delete(:env)
  host = options.delete(:host) ||
         (raise ArgumentError, 'Host expected but not given')
  opts = Options::REQUIRED_BY_PARSER
         .merge(merge_options(options))
         .merge(host => nil)
  @command_options, @host_options = partition_options(opts)
end

Instance Method Details

#capture(cmd, oid, options = {}) ⇒ Object



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

def capture(cmd, oid, options = {})
  out, err = if @env
               Open3.capture3(@env, cli(cmd, oid, options))
             else
               Open3.capture3(cli(cmd, oid, options))
             end
  raise_capture_errors(err)
  out
end

#cli(command, id = nil, options = {}) ⇒ Object

Generate a CLI command string



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

def cli(command, id = nil, options = {})
  command = normalize_command(command)

  [
    command,
    *options.map { |k, v| "#{k}#{v}" },
    *oid_options(id),
    *@host_options.map { |k, v| "#{k}#{v}" },
    *@command_options.fetch(command, {}).map { |k, v| "#{k}#{v}" },
    *id
  ].join(' ')
end