Class: SNMP::Open::FileReader

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

Overview

Test data source for SNMP::Open that reads from the filesystem instead of running SNMP commands. Expects a ‘walk’ directory to be present if #walk will be used, and a ‘get’ directory if #get will be used. Within each directory, files named according to the numeric OIDs to be used are expected to be present, containing the output of an snmpwalk or snmpget run over the given OID.

Produces warnings describing an snmpbulkwalk or snmpget command that could be used to generate a needed file, if the file is unavailable. Controlled by the ‘warnings` option.

Constant Summary collapse

DEFAULT_WARNING_FORMATTER =
lambda { |gen, cmd, oid, outfile|
  "#{gen.cli(cmd, oid)} > #{outfile}"
}

Instance Method Summary collapse

Constructor Details

#initialize(directory, options = {}) ⇒ FileReader

Returns a new instance of FileReader.



20
21
22
23
24
25
26
27
28
29
# File 'lib/snmp/open/file_reader.rb', line 20

def initialize(directory, options = {})
  @directory = directory
  @warnings = options.delete(:warnings)
  @make_directories = options.delete(:make_directories)
  if @warnings && !@warnings.respond_to?(:call)
    @warnings = DEFAULT_WARNING_FORMATTER
  end
  options[:host] ||= '$OPTIONS'
  @command_generator = SNMP::Open::CommandReader.new(options)
end

Instance Method Details

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/snmp/open/file_reader.rb', line 31

def capture(cmd, oid, _options = {})
  mkdir(@directory, cmd.to_s) if @make_directories
  outfile = File.join(@directory, cmd.to_s, oid)
  File.read(outfile)
rescue Errno::ENOENT => e
  if @warnings
    warning = @warnings.call(@command_generator, cmd, oid, outfile)
    warn warning
  end
  raise e
end

#mkdir(base, cmd) ⇒ Object



43
44
45
46
# File 'lib/snmp/open/file_reader.rb', line 43

def mkdir(base, cmd)
  Dir.mkdir(base) unless File.exist?(base)
  Dir.mkdir(File.join(base, cmd)) unless File.exist?(File.join(base, cmd))
end