lib/snmp/open/command_reader.rb in snmp-open-0.3.1 vs lib/snmp/open/command_reader.rb in snmp-open-0.4.0
- old
+ new
@@ -1,52 +1,28 @@
require 'open3'
+require 'snmp/open/options'
module SNMP
class Open
# Open3-based data source that executes an snmp* command and captures the
# output
class CommandReader
- # see snmpcmd(1) for explanation of options
- OPTIONS = {
- version: '-v',
- auth_password: '-A',
- auth_protocol: '-a',
- community: '-c',
- context: '-n',
- no_check_increasing: {
- 'snmpbulkwalk' => '-Cc',
- 'snmpwalk' => '-Cc'
- },
- numeric: '-On', # needed by parser, should always be enabled
- priv_password: '-X', # not recommended, see snmp.conf(5)
- priv_protocol: '-x',
- sec_level: '-l',
- sec_user: '-u',
- retries: '-r',
- timeout: '-t',
- host: nil
- }.freeze
-
- OPTION_VALUES = {
- no_check_increasing: {
- true => ''
- }.freeze
- }.freeze
-
# +options+ accepts options dealing with making connections to the host,
- # including all of the options listed in the +OPTIONS+ constant hash.
+ # 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)
#
def initialize(options)
@env = options.delete(:env)
host = options.delete(:host) ||
(raise ArgumentError, 'Host expected but not given')
- opts = merge_options(options).merge('-On' => nil, host => nil)
+ opts = Options::REQUIRED_BY_PARSER
+ .merge(merge_options(options))
+ .merge(host => nil)
@command_options, @host_options = partition_options(opts)
end
def capture(cmd, oid, options = {})
out, err = if @env
@@ -74,12 +50,12 @@
private
def merge_options(options = {})
options.each_pair.with_object({}) do |(key, value), opts|
- if OPTIONS.key?(key)
- opts[OPTIONS[key]] =
- (OPTION_VALUES.fetch(key, {}).fetch(value, value) || next)
+ if Options::MAP.key?(key)
+ opts[Options::MAP[key]] =
+ (Options::VALUES.fetch(key, {}).fetch(value, value) || next)
elsif key.is_a?(String)
opts[key] = value
else
raise "Unknown option #{key}"
end